From dee435d6b3b9889113ec0ac994e9a44a7954d98b Mon Sep 17 00:00:00 2001 From: lzybetter Date: Sat, 2 Sep 2023 18:08:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=AE=9A=E6=97=B6=E5=99=A8?= =?UTF-8?q?=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 5 +++ .idea/misc.xml | 3 ++ main.py | 7 ---- util/schedule.py | 96 ++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 92 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index c2f924c..4899128 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,10 @@ ### test file test.py +*.log + +### config file +*.yaml +*.db ### venv template # Virtualenv diff --git a/.idea/misc.xml b/.idea/misc.xml index 983b97b..fdc85e3 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,7 @@ + + \ No newline at end of file diff --git a/main.py b/main.py index bd44917..e69de29 100644 --- a/main.py +++ b/main.py @@ -1,7 +0,0 @@ -from util.global_config import GlobalConfig - -http_proxy_config = GlobalConfig() -http_proxy_config.set_globalconfig("http_proxy", '127.0.0.1:8889') - -print(http_proxy_config.get_globalconfig("http_proxy")) -print(http_proxy_config.get_globalconfig("https_proxy")) diff --git a/util/schedule.py b/util/schedule.py index 013b792..d3ea20c 100644 --- a/util/schedule.py +++ b/util/schedule.py @@ -1,14 +1,86 @@ -import datetime -import re -from apscheduler.schedulers.background import BackgroundScheduler +import sqlite3 +from apscheduler.util import undefined +import logging +import uuid -def add_interval_schedule(type='interval', **kwargs): +logger = logging.getLogger("scheduler_logger") +logger.setLevel(logging.DEBUG) - if (type == 'interval'): - params = {} - for k, v in kwargs.items(): - if k not in ('year', 'month', 'day', 'week', 'day_of_week', 'hour', 'minute', 'second', 'start_date', 'end_date'): - pass - else: - params[k] = v - print(params) \ No newline at end of file +fh = logging.FileHandler('mylog.log') +fh.setLevel(logging.DEBUG) + +formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') +fh.setFormatter(formatter) + +logger.addHandler(fh) + +def add_interval_schedule(schedudler, func, **kwargs): + + params = { + 'args': None, + 'kwargs': None, + 'name': None, + 'misfire_grace_time': undefined, + 'coalesce': undefined, + 'max_instances': undefined, + 'next_run_time': undefined, + 'jobstore': 'default', + 'executor': 'default', + 'trigger': 'interval', + 'replace_existing':False} + for k, v in kwargs.items(): + if k not in ('trigger','args', 'kwargs', 'id', 'name', 'misfire_grace_time', 'coalesce' + 'max_instances', 'next_run_time', 'jobstore', 'executor', 'replace_existing', + 'trigger', 'days', 'weeks', 'hours', 'minutes', 'seconds', 'start_date', 'end_date'): + logger.error('%s is not supported'%k) + else: + params[k] = v + + if params['name']: + logger.info('定时器\'%s\'的参数为%s' % (params['name'], params)) + else: + logger.info('定时器\'%s\'的参数为%s'%(func.__name__, params)) + + id = str(uuid.uuid1()) + try: + schedudler.add_job(id=id, func=func, **params) + return "定时器添加成功" + except Exception as e: + logger.error("定时器添加失败,错误信息:%s"%e) + return "定时器添加失败,详细信息请见log文件" + +def del_scheduler(scheduler, id): + if query_scheduler(id): + try: + scheduler.remove_job(id) + return "已删除:%s"%id + except Exception as e: + logger.error("定时器删除失败,错误信息:%s" % e) + return "定时器删除失败,详细信息请见log文件" + else: + return "定时器删除失败,无该id的job: %s"%id + logger.info("无该id的job: %s"%id) + +def query_scheduler(id=None): + db_path = 'config/scheduler/jobstores.db' + conn = sqlite3.connect(db_path) + cur = conn.cursor() + cur.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='apscheduler_jobs';") + is_table_exists = cur.fetchall() + result = [] + if is_table_exists and id: + cur.execute("select * from apscheduler_jobs where id='%s'"%id) + result = cur.fetchall() + else: + cur.execute("select * from apscheduler_jobs") + result = cur.fetchall() + if result: + return True + else: + return False + +def get_scheduler_list(scheduler): + job_infos = [] + for j in scheduler.get_jobs(): + job_infos.append({'id': j.id, 'name': j.name}) + return job_infos \ No newline at end of file