|
@@ -132,3 +132,69 @@ class CeleryBeatObj:
|
|
|
@return:
|
|
|
"""
|
|
|
PeriodicTask.objects.get(name=name).delete()
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def update_task(name, **kwargs):
|
|
|
+ """
|
|
|
+ 更新任务
|
|
|
+
|
|
|
+ @param name: 任务名称
|
|
|
+ @param kwargs: 需要更新的属性及对应的值(可选)
|
|
|
+ - interval: (every, period) 或 IntervalSchedule 对象,用于更新间隔任务
|
|
|
+ - clocked_time: str 或 datetime,用于更新定时任务的时间戳
|
|
|
+ - crontab: (minute, hour, day_of_week, day_of_month, month_of_year, timezone) 或 CrontabSchedule 对象,用于更新周期任务
|
|
|
+ - task: 新的任务函数名
|
|
|
+ - args: 新的参数列表
|
|
|
+ - kwargs: 新的关键字参数字典
|
|
|
+ - enabled: 是否启用任务(True 或 False)
|
|
|
+ @return:
|
|
|
+ None
|
|
|
+ """
|
|
|
+ periodic_task = PeriodicTask.objects.get(name=name)
|
|
|
+
|
|
|
+ # 更新通用属性
|
|
|
+ if 'task' in kwargs:
|
|
|
+ periodic_task.task = kwargs['task']
|
|
|
+ if 'args' in kwargs:
|
|
|
+ periodic_task.args = json.dumps(kwargs['args'])
|
|
|
+ if 'kwargs' in kwargs:
|
|
|
+ periodic_task.kwargs = json.dumps(kwargs['kwargs'])
|
|
|
+ if 'enabled' in kwargs:
|
|
|
+ periodic_task.enabled = kwargs['enabled']
|
|
|
+
|
|
|
+ # 更新不同类型任务的特定属性
|
|
|
+ if 'interval' in kwargs:
|
|
|
+ if isinstance(kwargs['interval'], tuple):
|
|
|
+ every, period = kwargs['interval']
|
|
|
+ schedule, _ = IntervalSchedule.objects.get_or_create(every=every, period=period)
|
|
|
+ else:
|
|
|
+ schedule = kwargs['interval']
|
|
|
+ periodic_task.interval = schedule
|
|
|
+
|
|
|
+ if 'clocked_time' in kwargs:
|
|
|
+ if isinstance(kwargs['clocked_time'], str):
|
|
|
+ clocked_time = CommonService.get_date_from_timestamp(
|
|
|
+ CommonService.convert_to_timestamp(0, kwargs['clocked_time']), 8)
|
|
|
+ else:
|
|
|
+ clocked_time = kwargs['clocked_time']
|
|
|
+ schedule, _ = ClockedSchedule.objects.get_or_create(clocked_time=clocked_time)
|
|
|
+ periodic_task.clocked = schedule
|
|
|
+
|
|
|
+ if 'crontab' in kwargs:
|
|
|
+ if isinstance(kwargs['crontab'], tuple):
|
|
|
+ minute, hour, day_of_week, day_of_month, month_of_year, timezone = kwargs['crontab']
|
|
|
+ schedule, _ = CrontabSchedule.objects.get_or_create(
|
|
|
+ minute=minute,
|
|
|
+ hour=hour,
|
|
|
+ day_of_week=day_of_week,
|
|
|
+ day_of_month=day_of_month,
|
|
|
+ month_of_year=month_of_year,
|
|
|
+ timezone=timezone
|
|
|
+ )
|
|
|
+ else:
|
|
|
+ schedule = kwargs['crontab']
|
|
|
+ periodic_task.crontab = schedule
|
|
|
+
|
|
|
+ periodic_task.save()
|
|
|
+
|
|
|
+
|