feat: scheduler 儲存於 SQLAlchemyJobStore, 並在定時輸入時間時加入判斷.
This commit is contained in:
@@ -2,6 +2,9 @@ import logging
|
|||||||
import os
|
import os
|
||||||
from urllib.parse import quote_plus as urlquote
|
from urllib.parse import quote_plus as urlquote
|
||||||
|
|
||||||
|
from apscheduler.executors.pool import ThreadPoolExecutor
|
||||||
|
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
|
||||||
|
|
||||||
|
|
||||||
class BaseConfig:
|
class BaseConfig:
|
||||||
|
|
||||||
@@ -59,6 +62,19 @@ class BaseConfig:
|
|||||||
# 默认发件人的邮箱,这里填写和MAIL_USERNAME一致即可
|
# 默认发件人的邮箱,这里填写和MAIL_USERNAME一致即可
|
||||||
MAIL_DEFAULT_SENDER = ('pear admin', os.getenv('MAIL_USERNAME') or '123@qq.com')
|
MAIL_DEFAULT_SENDER = ('pear admin', os.getenv('MAIL_USERNAME') or '123@qq.com')
|
||||||
|
|
||||||
|
# 設置 APSCHEDULER 參數
|
||||||
|
SCHEDULER_API_ENABLED = os.getenv('SCHEDULER_API_ENABLED') or False
|
||||||
|
SCHEDULER_JOBSTORES: dict = {
|
||||||
|
'default': SQLAlchemyJobStore(url=f'mysql+pymysql://{MYSQL_USERNAME}:{MYSQL_PASSWORD}@{MYSQL_HOST}:{MYSQL_PORT}/{MYSQL_DATABASE}')
|
||||||
|
}
|
||||||
|
SCHEDULER_EXECUTORS: dict = {
|
||||||
|
'default': ThreadPoolExecutor(20)
|
||||||
|
}
|
||||||
|
SCHEDULER_JOB_DEFAULTS: dict = {
|
||||||
|
'coalesce': False,
|
||||||
|
'max_instances': 3
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class TestingConfig(BaseConfig):
|
class TestingConfig(BaseConfig):
|
||||||
""" 测试配置 """
|
""" 测试配置 """
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
from flask import Blueprint, request, render_template
|
from flask import Blueprint, request, render_template
|
||||||
from flask_apscheduler.utils import job_to_dict
|
from flask_apscheduler.utils import job_to_dict
|
||||||
|
from datetime import datetime as dt
|
||||||
from applications.common.tasks import tasks
|
from applications.common.tasks import tasks
|
||||||
from applications.common.tasks.tasks import task_list
|
from applications.common.tasks.tasks import task_list
|
||||||
from applications.common.utils.http import table_api, fail_api, success_api
|
from applications.common.utils.http import table_api, fail_api, success_api
|
||||||
@@ -56,12 +56,22 @@ def save():
|
|||||||
run_date=datetime,
|
run_date=datetime,
|
||||||
replace_existing=True)
|
replace_existing=True)
|
||||||
elif type == 'interval':
|
elif type == 'interval':
|
||||||
|
# load time
|
||||||
|
time = dt.strptime(time, "%H:%M:%S").time()
|
||||||
|
interval_seconds = 0
|
||||||
|
if time.hour != 0:
|
||||||
|
interval_seconds += time.hour * 60 * 60
|
||||||
|
if time.minute != 0:
|
||||||
|
interval_seconds += time.minute * 60
|
||||||
|
if time.second != 0:
|
||||||
|
interval_seconds += time.second
|
||||||
scheduler.add_job(
|
scheduler.add_job(
|
||||||
func=getattr(tasks, functions),
|
func=getattr(tasks, functions),
|
||||||
id=_id,
|
id=_id,
|
||||||
name=name,
|
name=name,
|
||||||
args=(1, 1),
|
args=(1, 1),
|
||||||
trigger=type,
|
trigger=type,
|
||||||
|
seconds=interval_seconds,
|
||||||
replace_existing=True)
|
replace_existing=True)
|
||||||
elif type == 'cron':
|
elif type == 'cron':
|
||||||
scheduler.add_job(
|
scheduler.add_job(
|
||||||
@@ -71,7 +81,6 @@ def save():
|
|||||||
args=(1, 1),
|
args=(1, 1),
|
||||||
trigger=type,
|
trigger=type,
|
||||||
replace_existing=True)
|
replace_existing=True)
|
||||||
|
|
||||||
return success_api()
|
return success_api()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user