def post(self, *args, **kwargs): seconds = self.request.POST.get('seconds') if seconds: # 几秒后执行任务 # 重复任务 int_sche = IntervalSchedule.objects.filter(every=seconds, period='seconds') if int_sche: int_sche = int_sche[0] else: int_sche = IntervalSchedule() int_sche.every = int(seconds) int_sche.period = 'seconds' int_sche.save() # 定时任务(也可以是重复的,不能精确到秒) # cron_sche = CrontabSchedule() # cron_sche.minute = 1 # cron_sche.hour = 1 # cron_sche.day_of_week = '0,1,2' # cron_sche.day_of_month = '0,1,2' # cron_sche.month_of_year = '0,1,2' # cron_sche.save() pt = PeriodicTask() pt.name = u'每隔 %sS 执行一次任务 %s' % (seconds, str(uuid.uuid1())) # name 是唯一的,故后缀了uuid pt.task = 'demo_celery.tasks.add' pt.interval = int_sche # interval 和 schedule 这两个属性只能选一个 # pt.crontab = cron_sche # pt.expires = datetime.datetime.now() + datetime.timedelta(days=1) pt.save() return HttpResponse('添加成功') else: res = add.delay(2, 3) return HttpResponse(HttpResponse(res.result))
def schedule_every(self, task_name, period, every, crontab='',args=None, kwargs=None, queue=''): main_task = 'optimizer.tasks.base_task' permissible_periods = ['days', 'hours', 'minutes', 'seconds'] # create the periodic task and the interval ptask_name = "%s_%s" % (task_name, datetime.datetime.now()) # create some name for the period task if period and every: if period not in permissible_periods: raise Exception('Invalid period specified') 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=main_task, interval=interval_schedule) elif crontab: ptask = PeriodicTask(name=ptask_name, task=main_task, crontab=crontab) if args: ptask.args = args if kwargs: ptask.kwargs = kwargs ptask.queue = queue ptask.save() return ptask
def update(self, task_name='test', period='', every='', crontab='', queue='', args=None, kwargs=None): pt = self.task permissible_periods = ['days', 'hours', 'minutes', 'seconds'] ptask_name = "%s_%s" % (task_name, datetime.now()) pt.name=ptask_name if period and every: if period not in permissible_periods: raise Exception('Invalid period specified') 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() pt.interval=interval_schedule elif crontab: pt.crontab = crontab if args: pt.args = args if kwargs: pt.kwargs = kwargs pt.queue = queue pt.save(update_fields=['name', 'task', 'queue', 'interval', 'args', 'kwargs',]) print(pt)
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.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 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.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 make_periodic_interval_task(name, task, every_sec, *args, **kwargs): '''creates a new periodic interval task, with name=name, updates fields if already exists''' pt, created = PeriodicTask.objects.get_or_create(name= name) if pt.crontab: print("Warning, old task entry had a crontab") if pt.crontab.periodictask_set.count() == 1: print("Was the only one used , deleting it") ct = pt.crontab ct.delete() pt.crontab = None if not pt.interval: i= IntervalSchedule() else: i= pt.interval i.period = "seconds" i.every = str(every_sec) i.save() pt.interval = i pt.task = task pt.args = anyjson.serialize(args) pt.kwargs = anyjson.serialize(kwargs) pt.save() print ("saved task: %s, created: %s" % (str(pt), str(created)))
def add_task(request, nums): if request.method == 'GET': logging.info(nums) if nums: # 几秒后执行任务 # 重复任务 int_sche = IntervalSchedule.objects.filter(every=nums, period='seconds') if int_sche: int_sche = int_sche[0] else: int_sche = IntervalSchedule() int_sche.every = nums int_sche.period = 'seconds' int_sche.save() # 定时任务(也可以是重复的,不能精确到秒) # cron_sche = CrontabSchedule() # cron_sche.minute = 1 # cron_sche.hour = 1 # cron_sche.day_of_week = '0,1,2' # cron_sche.day_of_month = '0,1,2' # cron_sche.month_of_year = '0,1,2' # cron_sche.save() pt = PeriodicTask() pt.name = u'每隔 %sS 执行一次任务 %s' % (nums, str(uuid.uuid1())) # name 是唯一的,故后缀了uuid pt.task = 'celery_demo.tasks.add' pt.interval= int_sche # interval 和 schedule 这两个属性只能选一个 # pt.crontab = cron_sche # pt.expires = datetime.datetime.now() + datetime.timedelta(days=1) pt.save() else: res = add.delay(2, 3) return HttpResponse(HttpResponse(res.result))
def schedule_every(self, every, period, args=None, kwargs=None): print "Scheduling %s to run every %s %s" % (self.name, every, period) if self.periodic_task: self.periodic_task.delete() print "Cleared all current periodic tasks" permissible_periods = ['days', 'hours', 'minutes', 'seconds'] if period not in permissible_periods: raise Exception('Invalid period specified: %s, must be one of: %s' % (period, ','.join(permissible_periods))) # create the periodic task and the interval ptask_name = "%s_%s" % (self.celery_task_name, self.name) 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: interval_schedule = IntervalSchedule() interval_schedule.every = every interval_schedule.period = period interval_schedule.save() print "Creating new periodic task" periodic_task = PeriodicTask(name=ptask_name, task=self.celery_task_name, interval=interval_schedule) if args: periodic_task.args = args if kwargs: periodic_task.kwargs = kwargs periodic_task.save() print "Attached the periodic task to the project task" self.periodic_task = periodic_task print "Saving project task" self.save()
def schedule_every(task_name, task, period, every, args=None, kwargs=None): permissible_periods = ['days', 'hours', 'minutes', 'seconds'] if period not in permissible_periods: raise Exception('Invalid period specified') # create the periodic task and the interval 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=task_name, task=task, interval=interval_schedule) if args: ptask.args = args if kwargs: ptask.kwargs = kwargs ptask.save() return TaskScheduler.objects.create(periodic_task=ptask)
def schedule_task_every(task_name, every, period, args=None, kwargs=None): 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()) 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: interval_schedule = IntervalSchedule() interval_schedule.every = every 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 ProjectTask.objects.create(periodic_task=ptask, name=task_name)
def manage_interval(period, every): """ adapted from https://groups.google.com/forum/#!msg/celery-users/CZXCh8sCK5Q/ihZgMV2HWWYJ c/o Jean Mark """ permissible_periods = ['days', 'hours', 'minutes', 'seconds'] if period not in permissible_periods: raise Exception('Invalid period specified') # query intervals interval_schedules = IntervalSchedule.objects.filter(period=period, every=every) # just check if interval schedules exist like that already and reuse em if interval_schedules: interval_schedule = interval_schedules[0] # else, create a brand new interval schedule else: interval_schedule = IntervalSchedule() interval_schedule.every = abs(every) interval_schedule.period = period interval_schedule.save() return interval_schedule