@@ -38,7 +38,6 @@ class TaskScheduler(Base):
3838 start_time : Mapped [datetime | None ] = mapped_column (DateTime (timezone = True ), comment = '任务开始触发的时间' )
3939 expire_time : Mapped [datetime | None ] = mapped_column (DateTime (timezone = True ), comment = '任务不再触发的截止时间' )
4040 expire_seconds : Mapped [int | None ] = mapped_column (comment = '任务不再触发的秒数时间差' )
41- last_run_time : Mapped [datetime | None ] = mapped_column (DateTime (timezone = True ), comment = '任务最后触发的时间' )
4241 type : Mapped [int ] = mapped_column (comment = '调度类型(0间隔 1定时)' )
4342 interval_every : Mapped [int | None ] = mapped_column (comment = '任务再次运行前的间隔周期数' )
4443 interval_period : Mapped [str | None ] = mapped_column (String (255 ), comment = '任务运行之间的周期类型' )
@@ -58,6 +57,9 @@ class TaskScheduler(Base):
5857 Boolean ().with_variant (INTEGER , 'postgresql' ), default = True , comment = '是否启用任务'
5958 )
6059 total_run_count : Mapped [int ] = mapped_column (default = 0 , comment = '任务触发的总次数' )
60+ last_run_time : Mapped [datetime | None ] = mapped_column (
61+ DateTime (timezone = True ), default = None , comment = '任务最后触发的时间'
62+ )
6163 remark : Mapped [str | None ] = mapped_column (
6264 LONGTEXT ().with_variant (TEXT , 'postgresql' ), default = None , comment = '备注'
6365 )
@@ -66,23 +68,22 @@ class TaskScheduler(Base):
6668
6769 @staticmethod
6870 def before_insert_or_update (mapper , connection , target ):
69- print ('before_insert_or_update' , mapper , connection , target )
7071 if target .expire_seconds is not None and target .expire_time :
7172 raise errors .ConflictError (msg = 'expires 和 expire_seconds 只能设置一个' )
7273
7374 @classmethod
7475 def changed (cls , mapper , connection , target ):
75- print ('changed' , mapper , connection , target )
7676 if not target .no_changes :
7777 cls .update_changed (mapper , connection , target )
7878
7979 @classmethod
80- def update_changed (cls , mapper , connection , target ):
81- print ('update_changed' , mapper , connection , target )
80+ async def update_changed_async (cls ):
8281 now = timezone .now ()
83- last_update = asyncio .create_task (redis_client .get (f'{ settings .CELERY_REDIS_PREFIX } :last_update' ))
84- if not last_update :
85- asyncio .create_task (redis_client .set (f'{ settings .CELERY_REDIS_PREFIX } :last_update' , timezone .to_str (now )))
82+ await redis_client .set (f'{ settings .CELERY_REDIS_PREFIX } :last_update' , timezone .to_str (now ))
83+
84+ @classmethod
85+ def update_changed (cls , mapper , connection , target ):
86+ asyncio .create_task (cls .update_changed_async ())
8687
8788
8889# 事件监听器
0 commit comments