def post(self, request): ret = {"result": 0} if not request.user.has_perm(self.permission_required): ret["result"] = 1 ret["msg"] = "Sorry,你没有'添加定时任务'的权限,请联系运维!" return JsonResponse(ret) task_add_form = TaskAddForm(request.POST) if not task_add_form.is_valid(): ret["result"] = 1 error_msg = json.loads( task_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) else: del task_add_form.cleaned_data["schedule"] print("task_add_form.cleaned_data: ", task_add_form.cleaned_data) if request.POST.get("expires"): task_add_form.cleaned_data["expires"] = datetime.strptime( request.POST.get("expires"), '%Y/%m/%d %H:%M') try: pt_obj = PeriodicTask(**task_add_form.cleaned_data) pt_obj.save() except Exception as e: ret["result"] = 1 ret["msg"] = "PeriodicTask 保存对象失败,请查看日志..." wslog_error().error("PeriodicTask 保存对象失败, 错误信息: %s" % (e.args)) else: ret["msg"] = "添加 PeriodicTask 对象成功" return JsonResponse(ret)
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(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 save(self, *args, **kwargs): if self.cron_schedule != self._original_cron_schedule and self.cron_schedule: minute, hour, day_of_week = self.cron_schedule.split()[:3] if not self.periodic_task: periodic_task = PeriodicTask(task='humfrey.update.update', kwargs=json.dumps({'slug': self.slug, 'trigger': 'crontab'}), name='Update definition: {0}'.format(self.slug), enabled=True) periodic_task.save() self.periodic_task = periodic_task crontab = self.periodic_task.crontab or CrontabSchedule() crontab.minute = minute crontab.hour = hour crontab.day_of_week = day_of_week crontab.save() self.periodic_task.crontab = crontab self.periodic_task.save() super(UpdateDefinition, self).save(*args, **kwargs) elif self.cron_schedule != self._original_cron_schedule and self.periodic_task: periodic_task, self.periodic_task = self.periodic_task, None super(UpdateDefinition, self).save(*args, **kwargs) periodic_task.crontab.delete() periodic_task.delete() else: super(UpdateDefinition, self).save(*args, **kwargs)
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(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 done(request): if request.facebook.user: facebook = request.facebook if request.method == 'POST': form = SelectOptionForm(request.POST) if form.is_valid(): cd = form.cleaned_data time_interval = TIME_INTERVALS[cd['interval']] if cd['interval'] in TIME_INTERVALS else {} interval = IntervalSchedule.objects.get(pk=int(time_interval['id'])) if time_interval['type'] == 'interval' else CrontabSchedule.objects.get(pk=int(time_interval['id'])) message = Message.objects.get(pk=int(1)) task_name = slug("{0}-{1}-{2}-{3}".format(facebook.user.facebook_username, interval, cd['interval'], message.caption)) interval_interval = None interval_crontab = None if time_interval['type'] == 'interval': interval_interval = interval else: interval_crontab = interval a = PeriodicTask(name=task_name, task='facenew.tasks.tasks.publish', interval=interval_interval, crontab=interval_crontab, enabled=False, args=[facebook.user.id, message.id, time_interval]) a.save() return render_to_response('index.html', { 'user': request.user, 'facebook': facebook, 'form': form }, RequestContext(request)) else: form = SelectOptionForm() return render_to_response('index.html', { 'form': form, 'facebook': facebook, }, RequestContext(request))
def post_save(sender, instance, created, **kwargs): """ Update djcelery beat schedule :param sender: The model class (Settings) :param instance: The actual instance being saved. :param created: A boolean; True if a new record was created :return: """ # for migrations process if created: return from djcelery.models import PeriodicTask, CrontabSchedule try: task_object = PeriodicTask.objects.get( name=Settings.MAILING_TASK_LABEL) cron_object = task_object.crontab except ObjectDoesNotExist: cron_object = CrontabSchedule() task_object = PeriodicTask(name=Settings.MAILING_TASK_LABEL, task=Settings.MAILING_TASK_NAME) cron_object.minute = instance.mailing_time.minute cron_object.hour = instance.mailing_time.hour cron_object.save() task_object.crontab = cron_object task_object.args = "[{}]".format(instance.mailing_threads) task_object.save()
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 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()
def add_task(request): interval = IntervalSchedule.objects.filter(every=30, period='seconds').first() periodic_task = PeriodicTask(name='test', task='demo.tasks.async_demo_task', interval=interval) periodic_task.save() return HttpResponse('任务已经添加')
def scheduleToggleTask(circuit_id, desired_state, crontab_obj): periodic_task = PeriodicTask( name='%s_%s' % ('toggleSwitch', datetime.datetime.now()), task='PbElite.tasks.toggleSwitch', crontab=crontab_obj, args=json.dumps([circuit_id, desired_state]) ) periodic_task.save() return periodic_task
def vacaciones(request): obj = Objeto.objects.all() id_obj_t = {} id_obj_f = {} for objeto in obj: logs_obj = Log.objects.filter(output = objeto) horas_par_t = [] horas_par_f = [] for i in range(len(logs_obj)): for j in range(len(logs_obj)): ta = True if logs_obj[i].hora.hour == logs_obj[j].hora.hour and logs_obj[i].status == logs_obj[j].status and j != i: if logs_obj[i].status: for t in horas_par_t: if t.hora.hour == logs_obj[i].hora.hour: ta = False if ta: horas_par_t.append(logs_obj[i]) else: for f in horas_par_f: if f.hora.hour == logs_obj[i].hora.hour: ta = False if ta: horas_par_f.append(logs_obj[i]) id_obj_t.update({objeto.id:horas_par_t}) id_obj_f.update({objeto.id:horas_par_f}) print id_obj_t print id_obj_f context = RequestContext(request) for i in id_obj_t: for j in range(len(id_obj_t[i])): vaca_cron = CrontabSchedule() vaca_cron.minute = id_obj_f[i][j].hora.minute vaca_cron.hour = id_obj_f[i][j].hora.hour vaca_cron.save() vaca_periodic = PeriodicTask() vaca_periodic.name = 'Vacaciones ' + str(id_obj_f[i][j].output.id) vaca_periodic.task = "module_1.tasks.on" vaca_periodic.crontab = vaca_cron vaca_periodic.args = "[ " +str(id_obj_f[i][j].output.pin)+ " ]" vaca_periodic.save() for i in id_obj_f: for j in range(len(id_obj_f[i])): vaca_cron = CrontabSchedule() vaca_cron.minute = id_obj_f[i][j].hora.minute vaca_cron.hour = id_obj_f[i][j].hora.hour vaca_cron.save() vaca_periodic = PeriodicTask() vaca_periodic.name = 'Vacaciones ' + str(id_obj_f[i][j].output.id) vaca_periodic.task = "module_1.tasks.off" vaca_periodic.crontab = vaca_cron vaca_periodic.args = "[ " +str(id_obj_f[i][j].output.pin)+ " ]" vaca_periodic.save() luces = Luz.objects.all() rule = Regla.objects.all() return render_to_response('luzauto.html',{'luz':luces, 'rules':rule},context)
def save(self, **kwargs): if not self.task: task = PeriodicTask( name='{} Parser Task'.format(self.name.title())) task.task = 'parse.' + self.name.lower() task.enabled = False task.save() self.task = task models.Model.save(self, **kwargs)
def schedule_bill_plan(query, plan_id): if query['period'] == '1': months = '*' else: months = query['months'] cronRecord = CrontabSchedule(minute=random.randint(0, 59), hour=random.randint(0, 20), day_of_month=int(query['generation_date']), month_of_year=months) cronRecord.save() periodicTask = PeriodicTask(name=query['name'], task=BILL_PLANS_TASK, crontab=cronRecord, kwargs=json.dumps({"plan_id": plan_id}), enabled=True) periodicTask.save() return None
def scheduleTest(): crontab_obj = CrontabSchedule() crontab_obj.save() periodic_task = PeriodicTask( name='test', task="PbElite.tasks.testScheduler", crontab=crontab_obj ) periodic_task.save()
def project_set(request): project = Project.objects.get(id=request.GET.get('id')) crontab = CrontabSchedule(minute=request.POST.get('minute'), hour=request.POST.get('hour')) crontab.save() tname = project.name + "_task" try: ptask = PeriodicTask.objects.get(name=tname) except: print("ptask is null") ptask = None if ptask is not None: ptask.crontab = crontab ptask.enabled = request.POST.get('enable') else: ptask = PeriodicTask(name=project.name + "_task", task='rango.script.task.executetest', crontab=crontab, args="[" + request.GET.get('id') + "]", enabled=request.POST.get('enable')) ptask.save() address = request.POST.get("address") username = request.POST.get("username") password = request.POST.get("password") dbname = request.POST.get("dbname") port = request.POST.get("port") file = request.FILES.get('sql', None) sql = None if file is not None: sql = file else: sql = request.POST.get("sql") print(sql) try: dbconfigure = DbConfigure.objects.get(project=project) except: dbconfigure = None if dbconfigure is None: dbconfigure = DbConfigure(project=project, address=address, username=username, password=password, sql=sql, dbname=dbname, port=port) else: dbconfigure.address = address dbconfigure.username = username dbconfigure.password = password dbconfigure.sql = sql dbconfigure.dbname = dbname dbconfigure.port = port dbconfigure.save() return HttpResponseRedirect("/rango/project?id=" + request.GET.get('id'))
def schedule_with_crontab(task_name, crontab_schedule, args=None, kwargs=None): ptask_name = "%s_%s" % (task_name, datetime.datetime.now()) ptask = PeriodicTask(name=ptask_name, task=task.name, crontab=crontab_schedule ) if args: ptask.args = args if kwargs: ptask.kwargs = kwargs ptask.save() return ProjectTask.objects.create(periodic_task=ptask)
def save_task(request): try: # 先获取参数 task_name = request.POST["taskName"].replace(" ", "") crontab_id = request.POST["crontab"] datasource = request.POST["datasource"] sql = request.POST["sql"] operator = request.POST["operator"] threshold = request.POST["threshold"] # 任务模板,默认先取这个 monitor_task = "monitor.tasks.monitor_sql" kwargs = dict() kwargs["name"] = task_name kwargs["datasource"] = datasource kwargs["sql"] = sql kwargs["operator"] = operator kwargs["threshold"] = int(threshold) # 编辑页面 还可以关联文章 if request.POST.has_key("article"): article = request.POST["article"] if article != "" and article is not None: kwargs["article"] = int(article) json_str = json.dumps(kwargs, ensure_ascii=False) periodic_task = PeriodicTask(name=task_name, task=monitor_task, kwargs=json_str, crontab_id=crontab_id) # 如果是编辑就加上id, if request.POST.has_key("taskId"): task_id = request.POST["taskId"] periodic_task.id = task_id if request.POST.has_key("enabled"): enabled = request.POST["enabled"] periodic_task.enabled = enabled else: periodic_task.enabled = 0 # 更新数据库 periodic_task.save() except Exception as e: print e finally: return HttpResponseRedirect("/ocean/monitor/listTask")
def home(request): """ Displays a task enqueuement form and processes it. When schedule is set, an PeriodicTask model instance is created to spawn at a later time. Internally, in Celery there are pre_save and pre_delete signals attached to model, so celerybeat scheduler will be notified about the changes automagically. """ if request.method == "POST": task_form = forms.TaskForm(request.POST) if task_form.is_valid(): data = task_form.cleaned_data fullname = data["task"].fullname name = data["task"].name if data["schedule"] is not None: # Not the prettiest way, but necessary since different schedule # types must be provided as a differently named kwargs. if isinstance(data["schedule"], CrontabSchedule): crontab, interval = data["schedule"], None else: crontab, interval = None, data["schedule"] # This is a task to be scheduled periodically ptask_name = "generated_{0}_{1}".format( name, str(uuid.uuid4())) ptask = PeriodicTask(name=ptask_name, task=fullname, args=json.dumps(data["args"]), crontab=crontab, interval=interval) ptask.save() return render( request, "helloworld/message.html", { "heading": "scheduled", "message": "Created PeriodicTask #{0.id} {0.name}".format(ptask) }) else: # This is a task to be enqueued immediately result = data["task"].task.delay(*data["args"]) return redirect("status", task_name=fullname, task_id=result.id) else: task_form = forms.TaskForm(initial={"args": '["http://example.org/"]'}) return render(request, "helloworld/home.html", {"task_form": task_form})
def index(request): if request.method == 'POST': if 'schedule' in request.POST: # get the values from the form number = request.POST.get('phonenum') zipcode = request.POST.get('zipcode') sendtime = request.POST.get('sendtime') sendtime = sendtime.split(':') user = User.objects.filter(phoneNumber=number) if user: return render(request, 'home/signuperror.html') # add the job to the scheduler schedule = CrontabSchedule(hour=sendtime[0],minute=sendtime[1]) schedule.save() arguments = json.dumps({"number":number,"zipcode":zipcode}) modelData = dict( name=number, task="home.tasks.send", crontab_id=schedule.pk, kwargs=arguments, ) periodicTask = PeriodicTask(**modelData) periodicTask.save() newUser = User(phoneNumber=number,zipCode=zipcode,sendTime=(sendtime[0] + sendtime[1]),cronjobID=schedule.pk) newUser.save() # try: # periodicTask.save() # except: # from django.db import connection # print connection.queries # raise return render(request, 'home/thanks.html') elif "remove" in request.POST: number = request.POST.get('phonenum') user = User.objects.filter(phoneNumber=number) if not user: return render(request, 'home/removeerror.html') CrontabSchedule.objects.get(pk=user[0].cronjobID).delete() user.delete() return render(request, 'home/goodbye.html') return render(request, 'home/index.html')
def home(request): """ Displays a task enqueuement form and processes it. When schedule is set, an PeriodicTask model instance is created to spawn at a later time. Internally, in Celery there are pre_save and pre_delete signals attached to model, so celerybeat scheduler will be notified about the changes automagically. """ if request.method == "POST": task_form = forms.TaskForm(request.POST) if task_form.is_valid(): data = task_form.cleaned_data fullname = data["task"].fullname name = data["task"].name if data["schedule"] is not None: # Not the prettiest way, but necessary since different schedule # types must be provided as a differently named kwargs. if isinstance(data["schedule"], CrontabSchedule): crontab, interval = data["schedule"], None else: crontab, interval = None, data["schedule"] # This is a task to be scheduled periodically ptask_name = "generated_{0}_{1}".format(name, str(uuid.uuid4())) ptask = PeriodicTask( name=ptask_name, task=fullname, args=json.dumps(data["args"]), crontab=crontab, interval=interval ) ptask.save() return render(request, "helloworld/message.html", { "heading": "scheduled", "message": "Created PeriodicTask #{0.id} {0.name}".format(ptask) }) else: # This is a task to be enqueued immediately result = data["task"].task.delay(*data["args"]) return redirect("status", task_name=fullname, task_id=result.id) else: task_form = forms.TaskForm(initial={"args": '["http://example.org/"]'}) return render(request, "helloworld/home.html", {"task_form": task_form})
def schedule(task_path, minutes=0, task_name="", kwargs=None): if minutes < 1: raise Exception("Nope") intervals = IntervalSchedule.objects.filter(period='minutes', every=minutes) if intervals: interval = intervals[0] else: interval = IntervalSchedule.objects.create(period='minutes', every=minutes) task = PeriodicTask(name=task_name, task=task_path, interval=interval) task.kwargs = kwargs if kwargs else '{}' task.save() return TaskScheduler.objects.create(periodic_task=task)
def celery_add(request): task = request.POST.get('task') desc = request.POST.get('desc') shell = request.POST.get('shell') host = request.POST.get('host') crontab = request.POST.get('crontab') types = request.POST.get('type') db = PeriodicTask( name=desc, task=task, args=json.dumps([shell]), kwargs=json.dumps({'host': host}), crontab_id=crontab, ) db.save() return api.response()
def upgrade_partition_deploytask(request, module_name, tag_version): deploytask = DeployTask.objects.filter( project__module_name=module_name).filter(tag_version=tag_version)[0] deploytask.is_upgrade_partition = "yes" deploytask.save() subject = "已升级一个节点" message = "{}-{}已升级一个节点,请及时验证".format(module_name, tag_version) addr = ["{}@zhexinit.com".format(deploytask.principal)] for ep in deploytask.email_person.split(): addr.append("{}@zhexinit.com".format(ep)) addr.append("{}@zhexinit.com".format(deploytask.handle_person)) emailthread = EmailThread(subject, message, addr) emailthread.start() periodictask = PeriodicTask(name="sendemail_partition_to_dev_{}_{}".format( module_name, tag_version), task="deplpy.tasks.sendemail_partition_to_dev", args=[deploytask.id], interval=IntervalSchedule.objects.get(pk=2)) periodictask.save() return HttpResponseRedirect(reverse("deploy_task_list"))
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 deactivate_task(uid, days=14): """ uid:user.id days: 默认两周后关闭账户 """ from djcelery.models import CrontabSchedule, PeriodicTask from django.contrib.auth.models import User import datetime now = datetime.datetime.today() deadline = now + datetime.timedelta(minutes=3) task_date = CrontabSchedule() task_date.minute = deadline.timetuple().tm_min task_date.hour = deadline.timetuple().tm_hour task_date.day_of_month = deadline.timetuple().tm_mday task_date.month_of_year = deadline.timetuple().tm_mon try: from django.db import transaction with transaction.atomic(): task_date.save() except: return False user = User.objects.get(pk=uid) name = "Deactivate_User_%s" % uid new_task = PeriodicTask(name=name) new_task.name = name new_task.task = 'templates.tasks.deactivate_tempuser' new_task.crontab = task_date new_task.args = "[%s]" % uid new_task.enabled = True try: from django.db import transaction with transaction.atomic(): new_task.save() except: return False return True
def upgrade_deploytask(request, id): deploytask = DeployTask.objects.get(pk=id) deploytask.is_upgrade = "yes" dt = datetime.now().strftime("%Y-%m-%d %H:%M:%S") deploytask.upgrade_time = dt deploytask.save() subject = "升级成功" message = "{}-{}已经升级成功,如有问题请及时联系运维".format(deploytask.project.module_name, deploytask.tag_version) addr = ["{}@zhexinit.com".format(deploytask.principal)] for ep in deploytask.email_person.split(): addr.append("{}@zhexinit.com".format(ep)) addr.append("{}@zhexinit.com".format(deploytask.handle_person)) emailthread = EmailThread(subject, message, addr) emailthread.start() periodictask = PeriodicTask(name="sendemail_to_dev_{}_{}".format( deploytask.project.module_name, deploytask.tag_version), task="deploy.tasks.sendemail_to_dev", args=[int(id)], interval=IntervalSchedule.objects.get(pk=2)) periodictask.save() return HttpResponseRedirect(reverse("deploy_task_list"))
def schedule_cron(task_name, minute='*', hour='*', day_of_week='*', day_of_month='*', month_of_year='*' ,args=None, kwargs=None): # create the periodic task and the interval ptask_name = "%s_%s" % (task_name, datetime.now()) # create some name for the period task crontab_schedules = CrontabSchedule.objects.filter(minute=minute, hour=hour, day_of_week=day_of_week, day_of_month=day_of_month, month_of_year=month_of_year) if crontab_schedules: # just check if interval schedules exist like that already and reuse em crontab_schedule = crontab_schedules[0] else: # create a brand new interval schedule crontab_schedule = CrontabSchedule() crontab_schedule.minute = minute crontab_schedule.hour = hour crontab_schedule.day_of_week = day_of_week crontab_schedule.day_of_month = day_of_month crontab_schedule.month_of_year = month_of_year crontab_schedule.save() ptask = PeriodicTask(name=ptask_name, task=task_name, crontab=crontab_schedule) if args: ptask.args = args if kwargs: ptask.kwargs = kwargs ptask.save() return TaskScheduler.objects.create(periodic_task=ptask)
def addTask(request): intervalSchedule = IntervalSchedule.from_schedule(schedule(timedelta(seconds=10))) intervalSchedule.save() modelData = dict( name="dcTestPersist", task="technologytrackerapi.tasks.createRecord", interval_id=intervalSchedule.pk, ) periodicTask = PeriodicTask(**modelData) periodicTask.save() # return periodicTask # n = periodicTask.name me = ModelEntry(periodicTask) try: me.save() except: from django.db import connection print connection.queries raise # return me return render_to_response('taskView.html')
def save(self, *args, **kwargs): # Removes all other entries if there are any self.__class__.objects.exclude(id=self.id).delete() # creates new checkout task if self.checkout_task is None: cron = CrontabSchedule( minute=self.scheduled_verification_time.minute, hour=self.scheduled_verification_time.hour) cron.save() ctask = PeriodicTask( name="scheduled-checkout-%s" % cron.id, task="apps.organization.tasks.automatic_checkout", crontab=cron, queue="celery") ctask.save() self.checkout_task = ctask else: ctask = self.checkout_task cron = ctask.crontab cron.hour = self.scheduled_verification_time.hour cron.minute = self.scheduled_verification_time.minute cron.save() ctask.save() # creates new reminder task if self.reminder_task is None: cron = CrontabSchedule(minute=self.checkout_reminder_time.minute, hour=self.checkout_reminder_time.hour) cron.save() rtask = PeriodicTask( name="scheduled-reminder-%s" % cron.id, task="apps.organization.tasks.checkout_notification", crontab=cron, queue="celery") rtask.save() self.reminder_task = rtask else: rtask = self.reminder_task cron = rtask.crontab cron.hour = self.checkout_reminder_time.hour cron.minute = self.checkout_reminder_time.minute cron.save() rtask.save() super(OrgSettings, self).save(*args, **kwargs)
def create_periodic_task(task_name, period, every, args=None, kwargs=None): """ adapted from https://groups.google.com/forum/#!msg/celery-users/CZXCh8sCK5Q/ihZgMV2HWWYJ c/o Jean Mark Schedules a task by name every "every" "period". So an example call would be: create_periodic_task('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. """ # create the schedule interval_schedule = manage_interval(period, every) # create the periodic task ptask_name = "%s_%s" % (task_name, timezone.now()) ptask = PeriodicTask(name=ptask_name, task=task_name, interval=interval_schedule) if args: ptask.args = args if kwargs: ptask.kwargs = kwargs ptask.save() return ptask
def save(self, *args, **kwargs): if self.cron_schedule != self._original_cron_schedule and self.cron_schedule: minute, hour, day_of_week = self.cron_schedule.split()[:3] if not self.periodic_task: periodic_task = PeriodicTask( task='humfrey.update.update', kwargs=json.dumps({ 'slug': self.slug, 'trigger': 'crontab' }), name='Update definition: {0}'.format(self.slug), enabled=True) periodic_task.save() self.periodic_task = periodic_task crontab = self.periodic_task.crontab or CrontabSchedule() crontab.minute = minute crontab.hour = hour crontab.day_of_week = day_of_week crontab.save() self.periodic_task.crontab = crontab self.periodic_task.save() super(UpdateDefinition, self).save(*args, **kwargs) elif self.cron_schedule != self._original_cron_schedule and self.periodic_task: periodic_task, self.periodic_task = self.periodic_task, None super(UpdateDefinition, self).save(*args, **kwargs) periodic_task.crontab.delete() periodic_task.delete() else: super(UpdateDefinition, self).save(*args, **kwargs)
def add_rule(request): context = RequestContext(request) id= request.POST.get('id') lista_permitidos = Usuario.objects.filter(permisos_luces=id) if lista_permitidos.__str__() != "[]": for permitido in lista_permitidos: if permitido.user.id == request.user.id: if request.method=='POST': print "hola" luz = Luz.objects.get(id = request.POST['id']) dias = eval(request.POST['days']) print "0" print dias,"-",request.POST['days'] hora = eval(request.POST['hours']) print "1" regla = Regla() print "2" regla.relacion = luz print "3" regla.pin = luz.pin print "4" if request.POST['status'] == "false": regla.status=False print request.POST['status'] else: regla.status=True print "true" print "4" days_t = [] for i in dias: if i=="Lunes": print "Lun" regla.lun = True days_t.append(1) elif i=="Martes": print "Mar" regla.mar = True days_t.append(2) elif i=="Miercoles": print "Mie" regla.mie = True days_t.append(3) elif i=="Jueves": print "Jue" regla.jue = True days_t.append(4) elif i=="Viernes": print "Vie" regla.vie = True days_t.append(5) elif i=="Sabado": print "Sab" regla.sab = True days_t.append(6) elif i=="Domingo": print "Dom" regla.dom = True days_t.append(0) regla.from_hour = hora[0] f_h = str(hora[0]) print "from" regla.to_hour = hora[1] t_h = str(hora[1]) print "to" regla.save() print "save" cron = CrontabSchedule() cron.minute = f_h[-2:] cron.hour = f_h[:2] print days_t aux="" for i in range(len(days_t)): if i == len(days_t)-1: aux += str(days_t[i]) else: aux += str(days_t[i])+"," days_t = aux print days_t cron.day_of_week = days_t cron.save() print "cron save" regla = Regla.objects.latest('id') cronT = CrontabSchedule.objects.latest('id') print "regla y cronT" periodic = PeriodicTask() print "P_t" na = str(regla.id)+"_1" print "na" periodic.name = na print "name" if regla.status: periodic.task = "module_1.tasks.on" else: periodic.task = "module_1.tasks.off" print "status" periodic.crontab = cronT print "cron= p_t" periodic.args = "[ " +str(regla.pin)+ " ]" print "arg" periodic.save() print "periodic save" cron = CrontabSchedule() cron.minute = t_h[-2:] cron.hour = t_h[:2] print days_t aux="" for i in range(len(days_t)): if i == len(days_t)-1: aux += str(days_t[i]) else: aux += str(days_t[i])+"," days_t = aux print days_t cron.day_of_week = days_t cron.save() print "cron save" cronT = CrontabSchedule.objects.latest('id') print "regla y cronT" periodic = PeriodicTask() print "P_t" na = str(regla.id)+"_2" print "na" periodic.name = na print "name" if regla.status: periodic.task = "module_1.tasks.off" else: periodic.task = "module_1.tasks.on" print "status" periodic.crontab = cronT print "cron= p_t" periodic.args = "[ " +str(regla.pin)+ " ]" print "arg" periodic.save() print "periodic save" rule = Regla.objects.all() return render_to_response('tab.html',{'rules':rule},context) rule = Regla.objects.all() response = render_to_response('tab.html',{'rules':rule},context) response.status_code = 202 return response
def ExecuteFlow(req, app_uuid, tab, uuid): if tab not in ("plat", "group"): return Response({}, status=status.HTTP_404_NOT_FOUND) try: target_app = Apps.objects.get(pk=app_uuid) except: return Response({"success": False, "msg": _(u'应用不存在!')}) data = ExecuteWorkFlowSerializer(data=req.data) if data.is_valid(): if data.validated_data["kind"] == "zone": if not data.validated_data.get("region", None): return Response({"success": False, "msg": _(u"缺少区域ID!")}) if data.validated_data["region"] != '__all__': if not cache.get( utils.REDIS_REGION % (req.user.uuid, app_uuid, data.validated_data["region"])): return Response({"success": False, "msg": _(u"区域ID不存在!")}) if tab == "plat": if not Zones.objects.safe_range(req.user, app_uuid, data.validated_data["uuids"], plat_uuid=uuid): return Response({}, status=status.HTTP_403_FORBIDDEN) else: if not Zones.objects.safe_range(req.user, app_uuid, data.validated_data["uuids"], group_uuid=uuid): return Response({}, status=status.HTTP_403_FORBIDDEN) target_map = ("Zones", data.validated_data["uuids"]) else: target_map = ("Assets", data.validated_data["uuids"]) if data.validated_data.get("cronExpression", None): crontab = CrontabSchedule.objects.filter( **data.validated_data["cronExpression"]).first() if not crontab: crontab = CrontabSchedule( **data.validated_data["cronExpression"]) crontab.save() periodic_task = PeriodicTask( name=data.validated_data["name"], task='exec_workflow', crontab=crontab, args=json.dumps([ req.user.account, data.validated_data["workflow_uuid"], target_map, ]), exchange='surge', routing_key='senders', kwargs=json.dumps({ "inject_env": data.validated_data.get("inject_env", None), "app_uuid": app_uuid, "first": True, "trigger": 2, "tab": tab, "uuid": uuid, "region": data.validated_data["region"], "timeout": data.validated_data.get("timeout", None) })) periodic_task.save() periodic_task_meta = PeriodicTaskMeta(periodictask=periodic_task, userid=req.user.uuid, user=req.user.name, app_uuid=app_uuid, appalias=target_app.appalias) periodic_task_meta.save() return Response({"success": True, "msg": _(u'定时成功!')}) print data.validated_data celery.send_task('exec_workflow', args=[ req.user.account, data.validated_data["workflow_uuid"], target_map ], exchange='surge', routing_key='senders', kwargs={ "inject_env": data.validated_data.get("inject_env", None), "app_uuid": app_uuid, "first": True, "trigger": 1, "tab": tab, "uuid": uuid, "region": data.validated_data["region"], "timeout": data.validated_data.get("timeout", None) }) else: return Response({ "success": False, "msg": data.errors }, status=status.HTTP_400_BAD_REQUEST) return Response({"success": True, "msg": _(u'指令发送成功,请等候!')})