Commit d1a29e12 authored by 崔为之's avatar 崔为之 💪🏽

Update project

parent 91d7e7bb
......@@ -30,3 +30,10 @@ Logger:
Rotation:
Enqueue: True
Retention:
Redis:
Host: localhost
Port: 6379
Username:
Password:
DB: 13
......@@ -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)
......@@ -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)
......@@ -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
#!/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
#!/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
......@@ -10,5 +10,5 @@
# @Description :
"""
from .dsn import DatabaseUri
from .dsn import DatabaseUri, RedisUri
from .elasticsearch import ElasticsearchUtil
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