Commit d3969b3c authored by 崔为之's avatar 崔为之 💪🏽

Update project

parent d733b070
......@@ -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
......@@ -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)
#!/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()
#!/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
#!/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)
......@@ -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
......@@ -9,3 +9,12 @@
# @File : task.py
# @Description :
"""
def task():
"""
Some Tasks.
:return: None
"""
print(1)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment