feat: scheduler 儲存於 SQLAlchemyJobStore, 並在定時輸入時間時加入判斷.

This commit is contained in:
CHunYenc
2022-09-17 14:44:23 +08:00
parent c811b895d7
commit 8e356923c7
2 changed files with 27 additions and 2 deletions
+16
View File
@@ -2,6 +2,9 @@ import logging
import os
from urllib.parse import quote_plus as urlquote
from apscheduler.executors.pool import ThreadPoolExecutor
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
class BaseConfig:
@@ -59,6 +62,19 @@ class BaseConfig:
# 默认发件人的邮箱,这里填写和MAIL_USERNAME一致即可
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):
""" 测试配置 """
+11 -2
View File
@@ -1,6 +1,6 @@
from flask import Blueprint, request, render_template
from flask_apscheduler.utils import job_to_dict
from datetime import datetime as dt
from applications.common.tasks import tasks
from applications.common.tasks.tasks import task_list
from applications.common.utils.http import table_api, fail_api, success_api
@@ -56,12 +56,22 @@ def save():
run_date=datetime,
replace_existing=True)
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(
func=getattr(tasks, functions),
id=_id,
name=name,
args=(1, 1),
trigger=type,
seconds=interval_seconds,
replace_existing=True)
elif type == 'cron':
scheduler.add_job(
@@ -71,7 +81,6 @@ def save():
args=(1, 1),
trigger=type,
replace_existing=True)
return success_api()