diff --git a/application/config/config.yaml b/application/config/config.yaml index e026586868eb9d50394108878615394f8eaafb4a..162a482cbd11ce00aabf36b8d36cfbeb76dbe0b7 100644 --- a/application/config/config.yaml +++ b/application/config/config.yaml @@ -30,3 +30,10 @@ Logger: Rotation: Enqueue: True Retention: + +Redis: + Host: localhost + Port: 6379 + Username: + Password: + DB: 13 diff --git a/application/extensions/__init__.py b/application/extensions/__init__.py index eabf9170e3b0721a0b1743c952f4cc08da23886e..80de207adda32d0d4525a2d476c41b2ebbde56cc 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_celery import init_celery 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_celery(app) diff --git a/application/extensions/init_celery.py b/application/extensions/init_celery.py index ee4e5a35f48a64d110a01fd0e7a02da0e9accc84..efc886c5c69f2848a578e53733cacf3d42490e8c 100644 --- a/application/extensions/init_celery.py +++ b/application/extensions/init_celery.py @@ -10,43 +10,10 @@ # @Description : """ - from flask import Flask -from celery import Celery -from celery.schedules import crontab - - -def init_celery(app: Flask) -> None: - """ - Initialize Celery for the Flask application. - :param app: The Flask application. - :type app: Flask - """ - celery = Celery( - app.import_name, - backend=app.config['CELERY_RESULT_BACKEND'], - broker=app.config['CELERY_BROKER_URL'] - ) - celery.conf.update(app.config) +from application.libs import make_celery - class ContextTask(celery.Task): - def __call__(self, *args, **kwargs): - with app.app_context(): - return self.run(*args, **kwargs) - celery.Task = ContextTask - - @celery.task() - def update_database(): - # Add your database operations here - pass - - celery.conf.beat_schedule = { - 'update-database-every-day': { - 'task': 'update_database', - 'schedule': crontab(hour=0, minute=0) # Execute daily at midnight - } - } - - app.celery = celery +def init_celery(app: Flask) -> None: + app.celery = make_celery(app) diff --git a/application/libs/__init__.py b/application/libs/__init__.py index 20aa673ab0a5619a9ff27f34cd652fe52a1fa95a..17d193288befd99cb5df359f5295c13eeba02615 100644 --- a/application/libs/__init__.py +++ b/application/libs/__init__.py @@ -14,3 +14,4 @@ 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 new file mode 100644 index 0000000000000000000000000000000000000000..f4e5ebd980d119627d946e7d06bbdb7fe17955ec --- /dev/null +++ b/application/libs/tasks/__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 16:16 +# @File : __init__.py.py +# @Description : +""" + +from .task import make_celery diff --git a/application/libs/tasks/task.py b/application/libs/tasks/task.py new file mode 100644 index 0000000000000000000000000000000000000000..88dc9208134b0ced8c82cc40530781247afc0ce5 --- /dev/null +++ b/application/libs/tasks/task.py @@ -0,0 +1,56 @@ +#!/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 16:16 +# @File : task.py +# @Description : +""" + + +from celery import Celery +from celery.schedules import crontab +from flask import Flask + +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 diff --git a/application/utils/__init__.py b/application/utils/__init__.py index 8c88e8ac0949824d9eae7b81807bead8bf8c4937..6838d438fb881354e8e64a3cac917772ffd002b5 100644 --- a/application/utils/__init__.py +++ b/application/utils/__init__.py @@ -10,5 +10,5 @@ # @Description : """ -from .dsn import DatabaseUri +from .dsn import DatabaseUri, RedisUri from .elasticsearch import ElasticsearchUtil