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)
Esempio n. 2
0
    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))
Esempio n. 3
0
 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(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
Esempio n. 5
0
 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)
Esempio n. 6
0
    def post(self, request):
        ret = {"result": 0}

        if not request.user.has_perm(self.permission_required):
            ret["result"] = 1
            ret["msg"] = "Sorry,你没有'添加 IntervalSchedule '的权限,请联系运维!"
            return JsonResponse(ret)

        interval_schedule_add_form = IntervalScheduleForm(request.POST)
        if not interval_schedule_add_form.is_valid():
            ret["result"] = 1
            error_msg = json.loads(
                interval_schedule_add_form.errors.as_json(escape_html=False))
            ret["msg"] = '\n'.join(
                [i["message"] for v in error_msg.values() for i in v])
            return JsonResponse(ret)

        try:
            cs_obj = IntervalSchedule(
                **interval_schedule_add_form.cleaned_data)
            cs_obj.save()
        except Exception as e:
            ret["result"] = 1
            ret["msg"] = "IntervalSchedule 保存对象失败,请查看日志..."
            wslog_error().error("IntervalSchedule 保存对象失败, 错误信息: %s" % (e.args))
        else:
            ret["msg"] = "添加 IntervalSchedule 对象成功"
        return JsonResponse(ret)
Esempio n. 7
0
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)))
Esempio n. 8
0
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))
Esempio n. 9
0
    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()
Esempio n. 10
0
def add_periodic_task(every, alert_conf):
    '''
        
        ###Add one celery periodic task for this rule###
        Accept interval time and alert_conf_id, and add new periodic task with these details.
        intervals reused if already exist.
    '''

    #1. Set Interval time on IntervalSchedule Table.
    interval = IntervalSchedule.objects.filter(every=every, period='minutes')

    if interval:
        interval = interval[0]
    else:
        'No interval exist, add new one.'
        interval = IntervalSchedule(every=every, period='minutes')
        interval.save()

    #2. Adding Periodic task

    #generate unique name, This make user to freely assign name for Alert Confs.
    name = "%s-%s" % (alert_conf.name, random.randrange(start=100, stop=999))

    periodic_task = PeriodicTask(name=name,
                                 task='signin.tasks.process_an_alert',
                                 args=simplejson.dumps([alert_conf.id]),
                                 interval=interval,
                                 enabled=True)
    periodic_task.save()
Esempio n. 11
0
 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)
Esempio n. 12
0
 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)
Esempio n. 13
0
 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)
Esempio n. 14
0
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
Esempio n. 15
0
def alert_form_save(request):
    '''
        Save the New Alert configurations.
    '''
    clinic = get_object_or_404(Clinic, id=request.POST['clinic_list'])

    location = get_object_or_404(Location, id=request.POST['location_list'])

    from_addr = request.POST['from_addr']
    to_addr = request.POST['to_addr']
    name = request.POST['name']

    event_list = request.POST.getlist('event_names')

    waiting_time = int(request.POST['waiting_time'])
    enabled = request.POST['enabled']

    alert_retry = int(request.POST['alert_retry'])

    alert_interv = int(request.POST['alert_interv'])

    comp_event_list = ','.join(event_list)

    check_dup = AlertConfiguration.objects.filter(
        clinic=clinic,
        event_list=comp_event_list,
    )

    if check_dup:
        '''
            While modifying a alert rule we didn't changed the 
            a) Clinic
            b) Event list
                        
            then we keep the existing rule by updating other fields, otherwise,
            we create new alert rule instead of modifying it.
            
            Here we modifying the existing rule.
        '''

        check_dup = check_dup[0]

        #take old interval time.
        interval_old = check_dup.alert_interv

        check_dup.from_addr = from_addr
        check_dup.to_addr = to_addr
        check_dup.name = name
        check_dup.waiting_time = waiting_time
        check_dup.enabled = enabled
        check_dup.alert_retry = alert_retry
        check_dup.alert_interv = alert_interv
        check_dup.save()

        ###Update the Periodic tasks interval time.###

        if interval_old != alert_interv:
            'If there is any change in the interval we update the periodic task.'
            periodic_task = PeriodicTask.objects.filter(
                args=simplejson.dumps([check_dup.id]))

            if periodic_task:
                #updating the new interval with existing periodic task.
                periodic_task = periodic_task[0]

                #Check for the new interval object associated with this time.
                interval = IntervalSchedule.objects.filter(every=alert_interv,
                                                           period='minutes')
                if interval:
                    interval = interval[0]
                else:
                    interval = IntervalSchedule(every=alert_interv,
                                                period='minutes')
                    interval.save()

                periodic_task.interval = interval
                periodic_task.save()
            else:
                'No Periodic task for this rule... create one..'
                #Adding periodic task.
                add_periodic_task(alert_interv, check_dup)

    else:
        'Adding new rule.'

        alert_conf = AlertConfiguration(clinic=clinic,
                                        from_addr=from_addr,
                                        to_addr=to_addr,
                                        name=name,
                                        waiting_time=waiting_time,
                                        enabled=enabled,
                                        event_list=comp_event_list,
                                        alert_retry=alert_retry,
                                        alert_interv=alert_interv)

        alert_conf.save()

        #Adding new periodic task.
        add_periodic_task(alert_interv, alert_conf)

    return HttpResponseRedirect('/admin/signin/alertconfiguration/')