diff --git a/application/__init__.py b/application/__init__.py index 4b51ca50798999b4319e2b2f52ac49dcabfeafc6..19dfdf17d69394b472b4ca5d8ef56d120fe03002 100644 --- a/application/__init__.py +++ b/application/__init__.py @@ -17,6 +17,8 @@ from flask import Flask from application.extensions import init_plugs from application.views import init_views from application.script import init_script +from application.utils import make_celery +from celery.schedules import crontab def create_app() -> Flask: @@ -36,6 +38,23 @@ def create_app() -> Flask: # This could include things like database connectors, authentication systems, etc. init_plugs(app) + celery = make_celery(app) + celery.conf.update(app.config) + + @celery.task(bind=True) + def update_database(self): + # Add your database operations here + print("update database") + + celery.conf.beat_schedule = { + 'update-database-every-minute': { + 'task': 'application.__init__.update_database', + 'schedule': crontab(minute='*') # Execute every minute + } + } + + app.celery = celery + # Register the routes that this application will respond to. # This includes both the route URLs and the handlers for each route. init_views(app) diff --git a/application/extensions/__init__.py b/application/extensions/__init__.py index 80de207adda32d0d4525a2d476c41b2ebbde56cc..45921e759701391ba8b20a86e22bd44992f44cf7 100644 --- a/application/extensions/__init__.py +++ b/application/extensions/__init__.py @@ -21,7 +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_celery import init_celery +# from .init_celery import init_celery def init_plugs(app: Flask) -> None: @@ -34,4 +34,4 @@ def init_plugs(app: Flask) -> None: init_marshmallow(app) init_elasticsearch(app) init_cors(app) - init_celery(app) + # init_celery(app) diff --git a/application/extensions/init_celery.py b/application/extensions/init_celery.py index efc886c5c69f2848a578e53733cacf3d42490e8c..8df4e59e47b701b76437f5b396e9cd4c8c3777d0 100644 --- a/application/extensions/init_celery.py +++ b/application/extensions/init_celery.py @@ -11,9 +11,18 @@ """ from flask import Flask +from celery.schedules import crontab -from application.libs import make_celery +from application.utils import make_celery def init_celery(app: Flask) -> None: - app.celery = make_celery(app) + celery = make_celery(app) + app.celery = celery + + celery.conf.beat_schedule = { + 'update-database-every-day': { + 'task': 'application.libs.tasks.task.update_database', + 'schedule': crontab(minute='*') # Execute every minute + } + } diff --git a/application/libs/__init__.py b/application/libs/__init__.py index 17d193288befd99cb5df359f5295c13eeba02615..20aa673ab0a5619a9ff27f34cd652fe52a1fa95a 100644 --- a/application/libs/__init__.py +++ b/application/libs/__init__.py @@ -14,4 +14,3 @@ from .config import ConsulConfig, LocalConfig from .error import ConfigKeyError from .flask_elasticsearch import FlaskElasticsearch from .flask_loguru import FlaskLoguru -from .tasks import make_celery diff --git a/application/libs/tasks/__init__.py b/application/libs/tasks/__init__.py index f4e5ebd980d119627d946e7d06bbdb7fe17955ec..b38ba2452cf346586de94264e83367006f8a8799 100644 --- a/application/libs/tasks/__init__.py +++ b/application/libs/tasks/__init__.py @@ -9,5 +9,3 @@ # @File : __init__.py.py # @Description : """ - -from .task import make_celery diff --git a/application/libs/tasks/task.py b/application/libs/tasks/task.py index 88dc9208134b0ced8c82cc40530781247afc0ce5..2116426f31e4f176e44e39ff853d75bdf32cf2b0 100644 --- a/application/libs/tasks/task.py +++ b/application/libs/tasks/task.py @@ -11,46 +11,11 @@ """ -from celery import Celery -from celery.schedules import crontab -from flask import Flask +from flask import current_app -from application.utils import RedisUri - -def make_celery(app: Flask) -> Celery: - cfg = app.config.Redis - uri = RedisUri( - username=cfg.Username, - password=cfg.Password, - host=cfg.Host, - port=int(cfg.Port), - db=int(cfg.DB), - ) - print(uri) - - celery = Celery( - app.import_name, - backend='redis://localhost:6379/0', - broker='redis://localhost:6379/0' - ) - celery.conf.update(app.config) - - class ContextTask(celery.Task): - def __call__(self, *args, **kwargs): - with app.app_context(): - return self.run(*args, **kwargs) - - @celery.task(base=ContextTask) - def update_database(): - # Add your database operations here - print(1232141241421) - - celery.conf.beat_schedule = { - 'update-database-every-day': { - 'task': 'update_database', - 'schedule': crontab(minute='*') # Execute every minute - } - } - - return celery +@current_app.celery.task +def update_database(): + # Add your database operations here + print(123312312) + pass diff --git a/application/utils/__init__.py b/application/utils/__init__.py index 6838d438fb881354e8e64a3cac917772ffd002b5..1f76f478889b77eea40bcd6bee29a147a215b2e6 100644 --- a/application/utils/__init__.py +++ b/application/utils/__init__.py @@ -12,3 +12,4 @@ from .dsn import DatabaseUri, RedisUri from .elasticsearch import ElasticsearchUtil +from .celery import make_celery diff --git a/application/utils/celery/__init__.py b/application/utils/celery/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..daba8b668c0c0271dceab7929a713316076aaecd --- /dev/null +++ b/application/utils/celery/__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/7 17:15 +# @File : __init__.py.py +# @Description : +""" + +from .celery import make_celery diff --git a/application/utils/celery/celery.py b/application/utils/celery/celery.py new file mode 100644 index 0000000000000000000000000000000000000000..22ee2c5aba7909c5090fc1e680bacc35ec25cc1e --- /dev/null +++ b/application/utils/celery/celery.py @@ -0,0 +1,35 @@ +#!/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/7 17:15 +# @File : celery.py +# @Description : +""" + +from celery import Celery, Task +from flask import Flask + + +def make_celery(app: Flask): + celery = Celery( + app.import_name, + backend='redis://localhost:6379/13', + broker='redis://localhost:6379/13' + ) + celery.conf.update(app.config) + + class ContextTask(celery.Task): + def __call__(self, *args, **kwargs): + with app.app_context(): + return self.run(*args, **kwargs) + + celery.conf.update({ + 'task_cls': ContextTask, + }) + + return celery +