File tree Expand file tree Collapse file tree 6 files changed +28
-15
lines changed Expand file tree Collapse file tree 6 files changed +28
-15
lines changed Original file line number Diff line number Diff 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 中定义的队列' )
Original file line number Diff line number Diff line change 11#!/usr/bin/env python3
22# -*- coding: utf-8 -*-
33from datetime import datetime
4+ from typing import Any
45
56from 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 = '任务名称' )
Original file line number Diff line number Diff 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 = '更新时间' )
Original file line number Diff line number Diff line change 55from backend .app .task .utils .tzcrontab import TzAwareCrontab
66
77LOCAL_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}
Original file line number Diff line number Diff line change 55from backend .app .task .celery import celery_app
66
77
8- @celery_app .task ( name = 'delete_db_opera_log' )
8+ @celery_app .task
99async 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
1616async def delete_db_login_log () -> int :
1717 """自动删除数据库登录日志"""
1818 result = await login_log_service .delete_all ()
Original file line number Diff line number Diff line change 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
57from 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' )
918async def task_demo_async () -> str :
1019 """异步示例任务,模拟耗时操作"""
11- await sleep (20 )
20+ await asleep (20 )
1221 return 'test async'
You can’t perform that action at this time.
0 commit comments