init
This commit is contained in:
21
web/tasks/crond/__init__.py
Normal file
21
web/tasks/crond/__init__.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import os
|
||||
import glob
|
||||
from importlib import import_module
|
||||
|
||||
from apscheduler.schedulers.blocking import BlockingScheduler
|
||||
from apscheduler.schedulers.gevent import GeventScheduler
|
||||
|
||||
from worker import ASYNC_MODE, MODE_GEVENT
|
||||
|
||||
|
||||
if ASYNC_MODE == MODE_GEVENT:
|
||||
sched = GeventScheduler(timezone="Asia/Shanghai")
|
||||
else:
|
||||
sched = BlockingScheduler(timezone="Asia/Shanghai")
|
||||
|
||||
# 定时任放到jobs里统一加载
|
||||
path = os.path.dirname(__file__)
|
||||
file_names = glob.glob(os.path.join(path, 'jobs', '*.py'))
|
||||
for filename in file_names:
|
||||
module = os.path.basename(filename)[:-3]
|
||||
import_module('.' + module, "tasks.crond.jobs")
|
1
web/tasks/crond/jobs/__init__.py
Normal file
1
web/tasks/crond/jobs/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# -*- coding: utf-8 -*-
|
35
web/tasks/crond/jobs/create_last_month_publish_data.py
Normal file
35
web/tasks/crond/jobs/create_last_month_publish_data.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import datetime
|
||||
|
||||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
from commons.models.price_publish import PricePublish
|
||||
from commons.models.price_result import PriceResult
|
||||
from core.factory import ClientApp
|
||||
from tasks.crond import sched
|
||||
from tasks.once.calculate import calculate
|
||||
from tasks.once.collect import collect
|
||||
|
||||
|
||||
@sched.scheduled_job(trigger='cron', day='*/1', hour='2', minute="30")
|
||||
def create_last_month_publish_data():
|
||||
"""
|
||||
:return:
|
||||
"""
|
||||
today = datetime.date.today()
|
||||
last_month = today - relativedelta(months=1)
|
||||
year, month = last_month.year, last_month.month
|
||||
|
||||
with ClientApp().app_context:
|
||||
# 检查是否生成上月数据
|
||||
query = PriceResult.get_query(year=year, month=month)
|
||||
result = PriceResult.get_list(query)
|
||||
if not result:
|
||||
calculate(year=year, month=month)
|
||||
|
||||
# 检查是否生成上月数据
|
||||
query = PricePublish.get_query(year=last_month.year, month=last_month.month)
|
||||
result = PricePublish.get_list(query)
|
||||
if not result:
|
||||
collect(year=year, month=month)
|
||||
|
||||
return
|
18
web/tasks/crond/scheduler.py
Normal file
18
web/tasks/crond/scheduler.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
定时任务入口
|
||||
"""
|
||||
from core.factory import ClientApp
|
||||
from worker import ASYNC_MODE, MODE_GEVENT
|
||||
from tasks.crond import sched
|
||||
|
||||
|
||||
app = ClientApp()
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
g = sched.start()
|
||||
if ASYNC_MODE == MODE_GEVENT:
|
||||
g.join()
|
||||
except (KeyboardInterrupt, SystemExit):
|
||||
pass
|
Reference in New Issue
Block a user