diff --git a/application/config/config.yaml b/application/config/config.yaml index 162a482cbd11ce00aabf36b8d36cfbeb76dbe0b7..ff912a3090490c348257e3f4df4e2c0a423a5ffb 100644 --- a/application/config/config.yaml +++ b/application/config/config.yaml @@ -37,3 +37,8 @@ Redis: Username: Password: DB: 13 + +Scheduler: + Start: 2023-11-11 15:20:00 + End: 2099-11-11 16:00:00 + Timezone: Asia/Shanghai diff --git a/application/extensions/__init__.py b/application/extensions/__init__.py index eabf9170e3b0721a0b1743c952f4cc08da23886e..d838186448d16e8b4730a7e3a0796882532a4737 100644 --- a/application/extensions/__init__.py +++ b/application/extensions/__init__.py @@ -21,6 +21,7 @@ from .init_migrate import init_migrate from .init_apispec import init_apispec from .init_marshmallow import init_marshmallow from .init_elasticsearch import init_elasticsearch +from .init_apscheduler import init_tasks def init_plugs(app: Flask) -> None: @@ -33,3 +34,4 @@ def init_plugs(app: Flask) -> None: init_marshmallow(app) init_elasticsearch(app) init_cors(app) + init_tasks(app) diff --git a/application/extensions/init_apscheduler.py b/application/extensions/init_apscheduler.py new file mode 100644 index 0000000000000000000000000000000000000000..9445c2e554d6a1f9c20d98233a25ace6d9a933b3 --- /dev/null +++ b/application/extensions/init_apscheduler.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +# @Version : Python 3.11.4 +# @Software : Sublime Text 4 +# @Author : StudentCWZ +# @Email : StudentCWZ@outlook.com +# @Date : 2023/11/11 14:50 +# @File : init_apscheduler.py +# @Description : +""" + +from apscheduler.triggers.interval import IntervalTrigger +from flask import Flask + +from application.libs.tasks import task +from application.libs.flask_apscheduler import APScheduler + + +scheduler = APScheduler() + + +def init_tasks(app: Flask) -> None: + """ + Initialize the task extension. + + :param app: The Flask application instance. + :return: None + """ + if not isinstance(app, Flask): + raise TypeError(f'Expected a Flask application instance, got {type(app).__name__}') + + scheduler.api_enabled = True + scheduler.init_app(app) + + # Create an interval trigger + interval = IntervalTrigger( + days=1, + start_date=app.config.Scheduler.Start, + end_date=app.config.Scheduler.End, + timezone=app.config.Scheduler.Timezone) + + # Add a job to the scheduler + scheduler.add_job(func=task, trigger=interval, id='task_one') + + # Start the scheduler + scheduler.start() diff --git a/application/libs/flask_apscheduler/__init__.py b/application/libs/flask_apscheduler/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e572e2bf81fd5aad3aece5e50edc7c199646a4c2 --- /dev/null +++ b/application/libs/flask_apscheduler/__init__.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +# @Version : Python 3.11.4 +# @Software : Sublime Text 4 +# @Author : StudentCWZ +# @Email : StudentCWZ@outlook.com +# @Date : 2023/11/11 15:15 +# @File : __init__.py.py +# @Description : +""" + +from .apscheduler import APScheduler diff --git a/application/libs/flask_apscheduler/apscheduler.py b/application/libs/flask_apscheduler/apscheduler.py new file mode 100644 index 0000000000000000000000000000000000000000..987935df942b45fd2dc06f50120c8a62e641bad4 --- /dev/null +++ b/application/libs/flask_apscheduler/apscheduler.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +# @Version : Python 3.11.4 +# @Software : Sublime Text 4 +# @Author : StudentCWZ +# @Email : StudentCWZ@outlook.com +# @Date : 2023/11/11 15:15 +# @File : apscheduler.py +# @Description : +""" + +from flask_apscheduler import APScheduler as BaseAPScheduler + + +class APScheduler(BaseAPScheduler): + """ + A custom APScheduler with context management. + This allows the scheduler to work with Flask's application context, + which is necessary for tasks that interact with the database. + """ + + def run_job(self, _id, _job=None): + """ + Run a job with Flask's application context. + + :param _id: The ID of the job to run. + :param _job: The job store alias. + """ + with self.app.app_context(): + super().run_job(id=_id, jobstore=_job) diff --git a/application/libs/tasks/__init__.py b/application/libs/tasks/__init__.py index b38ba2452cf346586de94264e83367006f8a8799..358baa78c9c001d0dc0cd460981b62fc29a54ca2 100644 --- a/application/libs/tasks/__init__.py +++ b/application/libs/tasks/__init__.py @@ -6,6 +6,8 @@ # @Author : StudentCWZ # @Email : StudentCWZ@outlook.com # @Date : 2023/11/7 16:16 -# @File : __init__.py.py +# @File : __init__.py # @Description : """ + +from .task import task diff --git a/application/libs/tasks/task.py b/application/libs/tasks/task.py index bc0c3bbcd2541dae9ac7e75d1e0fc863cfa26146..098507d43493f8d54f93129877ddedee86fd2339 100644 --- a/application/libs/tasks/task.py +++ b/application/libs/tasks/task.py @@ -9,3 +9,12 @@ # @File : task.py # @Description : """ + + +def task(): + """ + Some Tasks. + + :return: None + """ + print(1)