From 9bdb96fb7e622eb4c6702b8cda56c462f9e57eb9 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 19:28:25 +0800 Subject: [PATCH] Update project --- application/__init__.py | 19 +++++++++++ application/extensions/__init__.py | 4 +-- application/extensions/init_celery.py | 13 ++++++-- application/libs/__init__.py | 1 - application/libs/tasks/__init__.py | 2 -- application/libs/tasks/task.py | 47 ++++----------------------- application/utils/__init__.py | 1 + application/utils/celery/__init__.py | 13 ++++++++ application/utils/celery/celery.py | 35 ++++++++++++++++++++ 9 files changed, 87 insertions(+), 48 deletions(-) create mode 100644 application/utils/celery/__init__.py create mode 100644 application/utils/celery/celery.py diff --git a/application/__init__.py b/application/__init__.py index 4b51ca5..19dfdf1 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 80de207..45921e7 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 efc886c..8df4e59 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 17d1932..20aa673 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 f4e5ebd..b38ba24 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 88dc920..2116426 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 6838d43..1f76f47 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 0000000..daba8b6 --- /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 0000000..22ee2c5 --- /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 + -- GitLab