mirror of
https://github.com/fastapi-practices/fastapi_best_architecture.git
synced 2025-08-26 04:33:09 +08:00
Update the celery task comment and name (#720)
This commit is contained in:
@ -29,7 +29,7 @@ class TaskScheduler(Base):
|
|||||||
|
|
||||||
id: Mapped[id_key] = mapped_column(init=False)
|
id: Mapped[id_key] = mapped_column(init=False)
|
||||||
name: Mapped[str] = mapped_column(String(50), unique=True, comment='任务名称')
|
name: Mapped[str] = mapped_column(String(50), unique=True, comment='任务名称')
|
||||||
task: Mapped[str] = mapped_column(String(255), comment='要运行的 Celery 任务(模块化字符串)')
|
task: Mapped[str] = mapped_column(String(255), comment='要运行的 Celery 任务')
|
||||||
args: Mapped[str | None] = mapped_column(JSON(), comment='任务可接收的位置参数')
|
args: Mapped[str | None] = mapped_column(JSON(), comment='任务可接收的位置参数')
|
||||||
kwargs: Mapped[str | None] = mapped_column(JSON(), comment='任务可接收的关键字参数')
|
kwargs: Mapped[str | None] = mapped_column(JSON(), comment='任务可接收的关键字参数')
|
||||||
queue: Mapped[str | None] = mapped_column(String(255), comment='CELERY_TASK_QUEUES 中定义的队列')
|
queue: Mapped[str | None] = mapped_column(String(255), comment='CELERY_TASK_QUEUES 中定义的队列')
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from pydantic import ConfigDict, Field
|
from pydantic import ConfigDict, Field
|
||||||
|
|
||||||
@ -12,7 +13,7 @@ class TaskResultSchemaBase(SchemaBase):
|
|||||||
|
|
||||||
task_id: str = Field(description='任务 ID')
|
task_id: str = Field(description='任务 ID')
|
||||||
status: str = Field(description='执行状态')
|
status: str = Field(description='执行状态')
|
||||||
result: bytes | None = Field(description='执行结果')
|
result: Any | None = Field(description='执行结果')
|
||||||
date_done: datetime | None = Field(description='结束时间')
|
date_done: datetime | None = Field(description='结束时间')
|
||||||
traceback: str | None = Field(description='错误回溯')
|
traceback: str | None = Field(description='错误回溯')
|
||||||
name: str | None = Field(description='任务名称')
|
name: str | None = Field(description='任务名称')
|
||||||
|
@ -13,7 +13,7 @@ class TaskSchedulerSchemeBase(SchemaBase):
|
|||||||
"""任务调度参数"""
|
"""任务调度参数"""
|
||||||
|
|
||||||
name: str = Field(description='任务名称')
|
name: str = Field(description='任务名称')
|
||||||
task: str = Field(description='要运行的 Celery 任务(模块化字符串)')
|
task: str = Field(description='要运行的 Celery 任务')
|
||||||
args: JsonValue | None = Field(default='[]', description='任务可接收的位置参数')
|
args: JsonValue | None = Field(default='[]', description='任务可接收的位置参数')
|
||||||
kwargs: JsonValue | None = Field(default='{}', description='任务可接收的关键字参数')
|
kwargs: JsonValue | None = Field(default='{}', description='任务可接收的关键字参数')
|
||||||
queue: str | None = Field(default=None, description='CELERY_TASK_QUEUES 中定义的队列')
|
queue: str | None = Field(default=None, description='CELERY_TASK_QUEUES 中定义的队列')
|
||||||
@ -48,5 +48,8 @@ class GetTaskSchedulerDetail(TaskSchedulerSchemeBase):
|
|||||||
model_config = ConfigDict(from_attributes=True)
|
model_config = ConfigDict(from_attributes=True)
|
||||||
|
|
||||||
id: int = Field(description='任务调度 ID')
|
id: int = Field(description='任务调度 ID')
|
||||||
|
enabled: bool = Field(description='是否启用任务')
|
||||||
|
total_run_count: int = Field(description='已运行总次数')
|
||||||
|
last_run_time: datetime | None = Field(None, description='最后运行时间')
|
||||||
created_time: datetime = Field(description='创建时间')
|
created_time: datetime = Field(description='创建时间')
|
||||||
updated_time: datetime | None = Field(None, description='更新时间')
|
updated_time: datetime | None = Field(None, description='更新时间')
|
||||||
|
@ -5,20 +5,20 @@ from celery.schedules import schedule
|
|||||||
from backend.app.task.utils.tzcrontab import TzAwareCrontab
|
from backend.app.task.utils.tzcrontab import TzAwareCrontab
|
||||||
|
|
||||||
LOCAL_BEAT_SCHEDULE = {
|
LOCAL_BEAT_SCHEDULE = {
|
||||||
'exec_every_10_seconds': {
|
'测试同步任务': {
|
||||||
'task': 'task_demo_async',
|
'task': 'task_demo',
|
||||||
'schedule': schedule(10),
|
'schedule': schedule(5),
|
||||||
},
|
},
|
||||||
'exec_every_1_minute_of_hour': {
|
'测试异步任务': {
|
||||||
'task': 'task_demo_async',
|
'task': 'task_demo_async',
|
||||||
'schedule': TzAwareCrontab('1'),
|
'schedule': TzAwareCrontab('1'),
|
||||||
},
|
},
|
||||||
'exec_every_sunday': {
|
'清理操作日志': {
|
||||||
'task': 'delete_db_opera_log',
|
'task': 'app.task.tasks.db_log.tasks.delete_db_opera_log',
|
||||||
'schedule': TzAwareCrontab('0', '0', day_of_week='6'),
|
'schedule': TzAwareCrontab('0', '0', day_of_week='6'),
|
||||||
},
|
},
|
||||||
'exec_every_15_of_month': {
|
'清理登录日志': {
|
||||||
'task': 'delete_db_login_log',
|
'task': 'app.task.tasks.db_log.tasks.delete_db_login_log',
|
||||||
'schedule': TzAwareCrontab('0', '0', day_of_month='15'),
|
'schedule': TzAwareCrontab('0', '0', day_of_month='15'),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -5,14 +5,14 @@ from backend.app.admin.service.opera_log_service import opera_log_service
|
|||||||
from backend.app.task.celery import celery_app
|
from backend.app.task.celery import celery_app
|
||||||
|
|
||||||
|
|
||||||
@celery_app.task(name='delete_db_opera_log')
|
@celery_app.task
|
||||||
async def delete_db_opera_log() -> int:
|
async def delete_db_opera_log() -> int:
|
||||||
"""自动删除数据库操作日志"""
|
"""自动删除数据库操作日志"""
|
||||||
result = await opera_log_service.delete_all()
|
result = await opera_log_service.delete_all()
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@celery_app.task(name='delete_db_login_log')
|
@celery_app.task
|
||||||
async def delete_db_login_log() -> int:
|
async def delete_db_login_log() -> int:
|
||||||
"""自动删除数据库登录日志"""
|
"""自动删除数据库登录日志"""
|
||||||
result = await login_log_service.delete_all()
|
result = await login_log_service.delete_all()
|
||||||
|
@ -1,12 +1,21 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from anyio import sleep
|
from time import sleep
|
||||||
|
|
||||||
|
from anyio import sleep as asleep
|
||||||
|
|
||||||
from backend.app.task.celery import celery_app
|
from backend.app.task.celery import celery_app
|
||||||
|
|
||||||
|
|
||||||
|
@celery_app.task(name='task_demo')
|
||||||
|
def task_demo() -> str:
|
||||||
|
"""示例任务,模拟耗时操作"""
|
||||||
|
sleep(20)
|
||||||
|
return 'test async'
|
||||||
|
|
||||||
|
|
||||||
@celery_app.task(name='task_demo_async')
|
@celery_app.task(name='task_demo_async')
|
||||||
async def task_demo_async() -> str:
|
async def task_demo_async() -> str:
|
||||||
"""异步示例任务,模拟耗时操作"""
|
"""异步示例任务,模拟耗时操作"""
|
||||||
await sleep(20)
|
await asleep(20)
|
||||||
return 'test async'
|
return 'test async'
|
||||||
|
Reference in New Issue
Block a user