增加定时任务
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
from apscheduler.events import (
|
||||
EVENT_JOB_ADDED,
|
||||
EVENT_JOB_ERROR,
|
||||
EVENT_JOB_EXECUTED,
|
||||
EVENT_JOB_MISSED,
|
||||
EVENT_JOB_REMOVED,
|
||||
EVENT_JOB_SUBMITTED,
|
||||
)
|
||||
|
||||
from applications.extensions.init_apscheduler import scheduler
|
||||
|
||||
|
||||
def job_missed(event):
|
||||
"""Job missed event."""
|
||||
with scheduler.app.app_context():
|
||||
print(event) # noqa: T001
|
||||
|
||||
|
||||
def job_error(event):
|
||||
"""Job error event."""
|
||||
with scheduler.app.app_context():
|
||||
print(event) # noqa: T001
|
||||
|
||||
|
||||
def job_executed(event):
|
||||
"""Job executed event."""
|
||||
with scheduler.app.app_context():
|
||||
print(event) # noqa: T001
|
||||
|
||||
|
||||
def job_added(event):
|
||||
"""Job added event."""
|
||||
with scheduler.app.app_context():
|
||||
print(event) # noqa: T001
|
||||
|
||||
|
||||
def job_removed(event):
|
||||
"""Job removed event."""
|
||||
with scheduler.app.app_context():
|
||||
print(event) # noqa: T001
|
||||
|
||||
|
||||
def job_submitted(event):
|
||||
"""Job scheduled to run event."""
|
||||
with scheduler.app.app_context():
|
||||
print(event) # noqa: T001
|
||||
|
||||
|
||||
scheduler.add_listener(job_missed, EVENT_JOB_MISSED)
|
||||
scheduler.add_listener(job_error, EVENT_JOB_ERROR)
|
||||
scheduler.add_listener(job_executed, EVENT_JOB_EXECUTED)
|
||||
scheduler.add_listener(job_added, EVENT_JOB_ADDED)
|
||||
scheduler.add_listener(job_removed, EVENT_JOB_REMOVED)
|
||||
scheduler.add_listener(job_submitted, EVENT_JOB_SUBMITTED)
|
||||
@@ -0,0 +1,51 @@
|
||||
import datetime
|
||||
from flask import current_app
|
||||
|
||||
from applications.extensions import db
|
||||
from applications.extensions.init_apscheduler import scheduler
|
||||
from applications.models import Dept
|
||||
|
||||
|
||||
# @scheduler.task(
|
||||
# "interval",
|
||||
# id="job_sync",
|
||||
# seconds=10,
|
||||
# max_instances=1,
|
||||
# start_date="2000-01-01 12:19:00",
|
||||
# )
|
||||
# def task1():
|
||||
# # oh, do you need something from config?
|
||||
# with scheduler.app.app_context():
|
||||
# print(scheduler.app.config) # noqa: T001
|
||||
# address = "ces"
|
||||
# deptName = "测试"
|
||||
# email = "123@qq.com"
|
||||
# leader = "测试的"
|
||||
# parentId = "1"
|
||||
# phone = "123456"
|
||||
# sort = "5"
|
||||
# status = 1
|
||||
# dept = Dept(
|
||||
# parent_id=parentId,
|
||||
# dept_name=deptName,
|
||||
# sort=sort,
|
||||
# leader=leader,
|
||||
# phone=phone,
|
||||
# email=email,
|
||||
# status=status,
|
||||
# address=address
|
||||
# )
|
||||
# r = db.session.add(dept)
|
||||
# db.session.commit()
|
||||
|
||||
|
||||
def task2(a, b):
|
||||
print(f'定时任务_1_{a},{b},{datetime.datetime.now()}')
|
||||
|
||||
|
||||
def task3(a, b):
|
||||
print(f'定时任务_2_{a}{b}{datetime.datetime.now()}')
|
||||
|
||||
|
||||
def task4(a, b):
|
||||
print(f'定时任务_4_{a}{b}{datetime.datetime.now()}')
|
||||
@@ -6,6 +6,7 @@ from .init_debug_tool import init_debug_tool
|
||||
from .init_template_directives import init_template_directives
|
||||
from .init_error_views import init_error_views
|
||||
from .init_mail import init_mail
|
||||
from .init_apscheduler import init_scheduler
|
||||
|
||||
|
||||
def init_plugs(app: Flask) -> None:
|
||||
@@ -15,3 +16,4 @@ def init_plugs(app: Flask) -> None:
|
||||
init_template_directives(app)
|
||||
init_error_views(app)
|
||||
init_mail(app)
|
||||
init_scheduler(app)
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
from flask import Flask
|
||||
from flask_apscheduler import APScheduler
|
||||
|
||||
scheduler = APScheduler()
|
||||
|
||||
|
||||
def init_scheduler(app: Flask):
|
||||
scheduler.init_app(app)
|
||||
with app.app_context():
|
||||
from applications.common.tasks import tasks
|
||||
scheduler.start()
|
||||
from applications.common.tasks import events
|
||||
@@ -8,6 +8,7 @@ from applications.view.admin.power import admin_power
|
||||
from applications.view.admin.role import admin_role
|
||||
from applications.view.admin.user import admin_user
|
||||
from applications.view.admin.monitor import admin_monitor_bp
|
||||
from applications.view.admin.task import admin_task
|
||||
|
||||
|
||||
def register_admin_views(app: Flask):
|
||||
@@ -19,3 +20,4 @@ def register_admin_views(app: Flask):
|
||||
app.register_blueprint(admin_power)
|
||||
app.register_blueprint(admin_role)
|
||||
app.register_blueprint(admin_dict)
|
||||
app.register_blueprint(admin_task)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
from flask import Blueprint, request, render_template
|
||||
from sqlalchemy import desc
|
||||
|
||||
from applications.common.utils.http import table_api
|
||||
from applications.common.utils.rights import authorize
|
||||
from applications.models import AdminLog
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
from flask import Blueprint, request, jsonify
|
||||
from applications.common.tasks.tasks import task4
|
||||
from applications.extensions.init_apscheduler import scheduler
|
||||
from flask_apscheduler.utils import job_to_dict
|
||||
|
||||
admin_task = Blueprint('adminTask', __name__, url_prefix='/admin/task')
|
||||
|
||||
|
||||
# 暂停任务
|
||||
# scheduler.pause_job('third')
|
||||
# 恢复任务
|
||||
# time.sleep(10)
|
||||
# scheduler.resume_job('third')
|
||||
# 删除任务
|
||||
# scheduler.remove_job('first')
|
||||
|
||||
@admin_task.route('/pause', methods=['GET'])
|
||||
def pause_job(): # 暂停
|
||||
job_id = request.args.get('id')
|
||||
scheduler.pause_job(str(job_id))
|
||||
return "pause success!"
|
||||
|
||||
|
||||
@admin_task.route('/resume', methods=['GET'])
|
||||
def resume_job(): # 恢复
|
||||
job_id = request.args.get('id')
|
||||
scheduler.resume_job(str(job_id))
|
||||
return "Success!"
|
||||
|
||||
|
||||
@admin_task.route('/get_jobs', methods=['GET'])
|
||||
def get_task(): # 获取
|
||||
jobs = scheduler.get_jobs()
|
||||
jobs_list = []
|
||||
for job in jobs:
|
||||
jobs_list.append(job_to_dict(job))
|
||||
return jsonify(jobs_list)
|
||||
|
||||
|
||||
@admin_task.route('/remove_job', methods=['GET'])
|
||||
def remove_job(): # 移除
|
||||
job_id = request.args.get('id')
|
||||
scheduler.remove_job(str(job_id))
|
||||
return 'remove success'
|
||||
|
||||
|
||||
@admin_task.route('/add_job', methods=['GET'])
|
||||
def add_task():
|
||||
scheduler.add_job(func=task4, id='4', args=(1, 1), trigger='interval', seconds=3,
|
||||
replace_existing=True)
|
||||
return '6'
|
||||
|
||||
# scheduler.add_job(func=task1, id='2', args=(1, 1), trigger='cron', day_of_week='0-6', hour=18, minute=24,
|
||||
# second=10, replace_existing=True)
|
||||
|
||||
# scheduler.add_job(func=task4, id='4', args=(2, 2), trigger='interval', seconds=3,
|
||||
# replace_existing=True, misfire_grace_time=3)
|
||||
|
||||
# # trigger='interval' 表示是一个循环任务,每隔多久执行一次
|
||||
# scheduler.add_job(func=task2, id='3', args=(2, 2), trigger='interval', seconds=3,
|
||||
# replace_existing=True)
|
||||
Reference in New Issue
Block a user