Skip to content

Commit 0214407

Browse files
committed
Update the celery task comment and name
1 parent ce3be1d commit 0214407

File tree

6 files changed

+28
-15
lines changed

6 files changed

+28
-15
lines changed

backend/app/task/model/scheduler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class TaskScheduler(Base):
2929

3030
id: Mapped[id_key] = mapped_column(init=False)
3131
name: Mapped[str] = mapped_column(String(50), unique=True, comment='任务名称')
32-
task: Mapped[str] = mapped_column(String(255), comment='要运行的 Celery 任务(模块化字符串)')
32+
task: Mapped[str] = mapped_column(String(255), comment='要运行的 Celery 任务')
3333
args: Mapped[str | None] = mapped_column(JSON(), comment='任务可接收的位置参数')
3434
kwargs: Mapped[str | None] = mapped_column(JSON(), comment='任务可接收的关键字参数')
3535
queue: Mapped[str | None] = mapped_column(String(255), comment='CELERY_TASK_QUEUES 中定义的队列')

backend/app/task/schema/result.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33
from datetime import datetime
4+
from typing import Any
45

56
from pydantic import ConfigDict, Field
67

@@ -12,7 +13,7 @@ class TaskResultSchemaBase(SchemaBase):
1213

1314
task_id: str = Field(description='任务 ID')
1415
status: str = Field(description='执行状态')
15-
result: bytes | None = Field(description='执行结果')
16+
result: Any | None = Field(description='执行结果')
1617
date_done: datetime | None = Field(description='结束时间')
1718
traceback: str | None = Field(description='错误回溯')
1819
name: str | None = Field(description='任务名称')

backend/app/task/schema/scheduler.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class TaskSchedulerSchemeBase(SchemaBase):
1313
"""任务调度参数"""
1414

1515
name: str = Field(description='任务名称')
16-
task: str = Field(description='要运行的 Celery 任务(模块化字符串)')
16+
task: str = Field(description='要运行的 Celery 任务')
1717
args: JsonValue | None = Field(default='[]', description='任务可接收的位置参数')
1818
kwargs: JsonValue | None = Field(default='{}', description='任务可接收的关键字参数')
1919
queue: str | None = Field(default=None, description='CELERY_TASK_QUEUES 中定义的队列')
@@ -48,5 +48,8 @@ class GetTaskSchedulerDetail(TaskSchedulerSchemeBase):
4848
model_config = ConfigDict(from_attributes=True)
4949

5050
id: int = Field(description='任务调度 ID')
51+
enabled: bool = Field(description='是否启用任务')
52+
total_run_count: int = Field(description='已运行总次数')
53+
last_run_time: datetime | None = Field(None, description='最后运行时间')
5154
created_time: datetime = Field(description='创建时间')
5255
updated_time: datetime | None = Field(None, description='更新时间')

backend/app/task/tasks/beat.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
from backend.app.task.utils.tzcrontab import TzAwareCrontab
66

77
LOCAL_BEAT_SCHEDULE = {
8-
'exec_every_10_seconds': {
9-
'task': 'task_demo_async',
10-
'schedule': schedule(10),
8+
'测试同步任务': {
9+
'task': 'task_demo',
10+
'schedule': schedule(5),
1111
},
12-
'exec_every_1_minute_of_hour': {
12+
'测试异步任务': {
1313
'task': 'task_demo_async',
1414
'schedule': TzAwareCrontab('1'),
1515
},
16-
'exec_every_sunday': {
17-
'task': 'delete_db_opera_log',
16+
'清理操作日志': {
17+
'task': 'app.task.tasks.db_log.tasks.delete_db_opera_log',
1818
'schedule': TzAwareCrontab('0', '0', day_of_week='6'),
1919
},
20-
'exec_every_15_of_month': {
21-
'task': 'delete_db_login_log',
20+
'清理登录日志': {
21+
'task': 'app.task.tasks.db_log.tasks.delete_db_login_log',
2222
'schedule': TzAwareCrontab('0', '0', day_of_month='15'),
2323
},
2424
}

backend/app/task/tasks/db_log/tasks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
from backend.app.task.celery import celery_app
66

77

8-
@celery_app.task(name='delete_db_opera_log')
8+
@celery_app.task
99
async def delete_db_opera_log() -> int:
1010
"""自动删除数据库操作日志"""
1111
result = await opera_log_service.delete_all()
1212
return result
1313

1414

15-
@celery_app.task(name='delete_db_login_log')
15+
@celery_app.task
1616
async def delete_db_login_log() -> int:
1717
"""自动删除数据库登录日志"""
1818
result = await login_log_service.delete_all()

backend/app/task/tasks/tasks.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3-
from anyio import sleep
3+
from time import sleep
4+
5+
from anyio import sleep as asleep
46

57
from backend.app.task.celery import celery_app
68

79

10+
@celery_app.task(name='task_demo')
11+
def task_demo() -> str:
12+
"""示例任务,模拟耗时操作"""
13+
sleep(20)
14+
return 'test async'
15+
16+
817
@celery_app.task(name='task_demo_async')
918
async def task_demo_async() -> str:
1019
"""异步示例任务,模拟耗时操作"""
11-
await sleep(20)
20+
await asleep(20)
1221
return 'test async'

0 commit comments

Comments
 (0)