Commit 9bdb96fb authored by 崔为之's avatar 崔为之 💪🏽

Update project

parent d1a29e12
......@@ -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)
......
......@@ -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)
......@@ -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
}
}
......@@ -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
......@@ -9,5 +9,3 @@
# @File : __init__.py.py
# @Description :
"""
from .task import make_celery
......@@ -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
......@@ -12,3 +12,4 @@
from .dsn import DatabaseUri, RedisUri
from .elasticsearch import ElasticsearchUtil
from .celery import make_celery
#!/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
#!/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
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