Replacing Celerybeat with Chronos

风格不统一 提交于 2019-12-04 03:29:26
Árni St. Sigurðsson

This SO has solutions to your dynamic periodic task problem. It's not the accepted answer at the moment:

 from djcelery.models import PeriodicTask, IntervalSchedule
 from datetime import datetime

 class TaskScheduler(models.Model):

     periodic_task = models.ForeignKey(PeriodicTask)

     @staticmethod
     def schedule_every(task_name, period, every, args=None, kwargs=None):
     """ schedules a task by name every "every" "period". So an example call would be:
          TaskScheduler('mycustomtask', 'seconds', 30, [1,2,3]) 
          that would schedule your custom task to run every 30 seconds with the arguments 1 ,2 and 3 passed to the actual task. 
     """
         permissible_periods = ['days', 'hours', 'minutes', 'seconds']
         if period not in permissible_periods:
             raise Exception('Invalid period specified')
         # create the periodic task and the interval
         ptask_name = "%s_%s" % (task_name, datetime.datetime.now()) # create some name for the period task
         interval_schedules = IntervalSchedule.objects.filter(period=period, every=every)
         if interval_schedules: # just check if interval schedules exist like that already and reuse em
             interval_schedule = interval_schedules[0]
         else: # create a brand new interval schedule
             interval_schedule = IntervalSchedule()
             interval_schedule.every = every # should check to make sure this is a positive int
             interval_schedule.period = period 
             interval_schedule.save()
         ptask = PeriodicTask(name=ptask_name, task=task_name, interval=interval_schedule)
         if args:
             ptask.args = args
         if kwargs:
             ptask.kwargs = kwargs
         ptask.save()
         return TaskScheduler.objects.create(periodic_task=ptask)

     def stop(self):
         """pauses the task"""
         ptask = self.periodic_task
         ptask.enabled = False
         ptask.save()

     def start(self):
         """starts the task"""
         ptask = self.periodic_task
         ptask.enabled = True
         ptask.save()

     def terminate(self):
         self.stop()
         ptask = self.periodic_task
         self.delete()
         ptask.delete()

I haven't used djcelery yet, but it supposedly has an admin interface for dynamic periodic tasks.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!