Skip to content

Commit

Permalink
perf(server): improve server rule
Browse files Browse the repository at this point in the history
  • Loading branch information
JinnLynn committed Jul 22, 2024
1 parent 227e4b3 commit 703456a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 20 deletions.
11 changes: 5 additions & 6 deletions src/genpac/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def parse_options(self, cli=True, **kwargs):
for fmter in self.__class__._formaters.values():
opts.update(fmter['cls']._predefined_options)

self.clear_jobs()
self.jobs.clear()

job_cfgs, common_cfgs = self.read_config(config_file)

Expand All @@ -235,16 +235,15 @@ def parse_options(self, cli=True, **kwargs):
self.jobs.sort(key=lambda j: j._order)

def add_rule(self, rule):
rule = rule.strip()
if not isinstance(rule, list):
rule = [str(rule)]
rule = [r.strip() for r in rule if r.strip()]
if rule:
self.extra_rules.append(rule)
self.extra_rules.extend(rule)

def add_job(self, job_cfgs):
self.extra_jobs.append(job_cfgs)

def clear_jobs(self):
self.jobs = []

def walk_jobs(self):
for job in self.jobs:
yield job
Expand Down
8 changes: 1 addition & 7 deletions src/genpac/server/base.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
from flask import current_app

from .. import GenPAC, formater, FmtBase


def init_genpac(options):
gp = GenPAC(config_file=options.config_file)
gp.parse_options(cli=False, workdir=options.target)
return gp
from .. import formater, FmtBase


@formater('genpac-server-domains')
Expand Down
9 changes: 4 additions & 5 deletions src/genpac/server/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
from watchdog.events import FileSystemEventHandler, \
EVENT_TYPE_CLOSED, EVENT_TYPE_OPENED

from .. import Generator
from .. import GenPAC, Generator
from ..util import logger
from .base import init_genpac


# ====
Expand All @@ -32,7 +31,7 @@ def build(app):

try:
Generator.clear_cache()
gp = init_genpac(options)
gp = GenPAC(config_file=options.config_file)
gp.add_job({'format': 'genpac-server-domains',
'output': options._private.domain_file,
'_order': -100})
Expand All @@ -41,8 +40,8 @@ def build(app):
'_order': -100})
if options.server_rule_enabled:
with open(options._private.server_rule_file, 'r') as fp:
for line in fp.readlines():
gp.add_rule(line.strip())
gp.add_rule(fp.readlines())
gp.parse_options(cli=False, workdir=options.target)
gp.generate_all()
except Exception:
logger.error('GenPAC build fail.', exc_info=True)
Expand Down
5 changes: 3 additions & 2 deletions src/genpac/server/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
from flask import Flask, Blueprint
from flask_apscheduler import APScheduler

from .. import GenPAC
from ..config import Config
from ..util import get_version
from ..util import Namespace
from ..util import exit_error, FatalError, mktemp
from ..util import logger, conv_bool, conv_list, conv_path
from .base import init_genpac
from .build import start_watch, autobuild_task

_SERVER_RULE_FILENAME = '_server_rules.txt'
Expand Down Expand Up @@ -138,7 +138,8 @@ def _update(attr, *convs, **kwargs):

# 如果允许监控文件更改
if options.watch_enabled:
gp = init_genpac(options)
gp = GenPAC(config_file=options.config_file)
gp.parse_options(cli=False, workdir=options.target)
# 添加config_file到监控列表
options.watch_files.add(options.config_file)
if options.server_rule_enabled:
Expand Down

0 comments on commit 703456a

Please sign in to comment.