def make_periodic_crontab_task(name, task, minute, hour, day_of_week, *args, **kwargs): '''creates a new periodic crontab task, with name=name, updates fields if already exists''' pt, created = PeriodicTask.objects.get_or_create(name= name) if pt.interval: print("Warning, old task entry had an interval") if pt.interval.periodictask_set.count() == 1: print("Was the only one, deleting it") i = pt.interval i.delete() pt.interval = None if not pt.crontab: ct= CrontabSchedule() else: ct= pt.crontab ct.minute = minute ct.hour = hour ct.day_of_week = day_of_week ct.save() pt.crontab = ct 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 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 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