From d1a29e129eed8b24abc2127a4fab2430960e3ce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=94=E4=B8=BA=E4=B9=8B?= <560397@gree.com.cn> Date: Tue, 7 Nov 2023 17:10:14 +0800 Subject: [PATCH] Update project --- application/config/config.yaml | 7 ++++ application/extensions/__init__.py | 2 + application/extensions/init_celery.py | 39 ++----------------- application/libs/__init__.py | 1 + application/libs/tasks/__init__.py | 13 +++++++ application/libs/tasks/task.py | 56 +++++++++++++++++++++++++++ application/utils/__init__.py | 2 +- 7 files changed, 83 insertions(+), 37 deletions(-) create mode 100644 application/libs/tasks/__init__.py create mode 100644 application/libs/tasks/task.py diff --git a/application/config/config.yaml b/application/config/config.yaml index e026586..162a482 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 eabf917..80de207 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 ee4e5a3..efc886c 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 20aa673..17d1932 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 0000000..f4e5ebd --- /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 0000000..88dc920 --- /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 8c88e8a..6838d43 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 -- GitLab