def test_getFirstDayWithSundayDOW(self): """ Test next day with Sunday DOW entry """ schedule = CronSchedule('* * * * 0,3,5') self.assertEqual( schedule.getNextEntry(datetime(2008, 8, 31, 23, 59, 00, 00)), datetime(2008, 9, 3, 00, 00, 00, 00))
def task(cronformat): cron = CronSchedule(cronformat) delay_sec = (cron.getNextEntry() - datetime.now()).seconds print "on decorator", cron.getNextEntry() def decorator(f): gargs = [] gkargs = {} def wrapper(*args, **kargs): gargs = args gkargs = kargs print delay_sec, gargs, gkargs reactor.callLater(delay_sec, wrapper, gargs, gkargs) return wrapper return decorator
def _start_schedule(self, id, schedule, period): # check if the schedule is disabled or not if period["secs"] != 0: if not self._scheduled_tasks.has_key(id): self._scheduled_tasks[id] = [] val_type = self._resolve_type(schedule) for p in self._agg_periods: cron = CronSchedule(self._agg_periods[p]) if p == "day": t = ScheduledCall(self._aggregate_day, id, val_type) t.start(cron) self._scheduled_tasks[id].append(t) elif p == "month": t = ScheduledCall(self._aggregate_month, id) t.start(cron) self._scheduled_tasks[id].append(t) elif p == "year": t = ScheduledCall(self._aggregate_year, id) t.start(cron) self._scheduled_tasks[id].append(t) else: self.log.warning("Unsupported period (%s)" % p) self.log.debug("Scheduled tasks: %s" % self._scheduled_tasks)
def _init(cls,tid,initcode=True): try: tid=int(tid) result=yield run_conn_fun("runQuery","select ip,svnpath,svnuser,svnpasswd,version,rule from cron_task WHERE tid=%s",(tid,)) ip,svnpath,svnuser,svnpasswd,svnversion,rule=result[0] if initcode==True: _defer =SubRpc().xmlrpc_init(int(tid),svnpath,int(svnversion),svnuser,svnpasswd) set_time_out(2,_defer) try: yield _defer except defer.CancelledError : pass else: pass if not cls.BUFF.has_key(tid): pass else: if cls.BUFF[tid].running: cls.BUFF[tid].stop() schedule=CronSchedule(rule) sc=ScheduledCall(cls._run,tid) sc.start(schedule) cls.BUFF[tid]=sc defer.returnValue(True) except Exception as e: defer.returnValue((False,str(e)))
class AllDOWTestCase(TestCase): def setUp(self): self.schedule = CronSchedule('*/15 * */5 * *') def testNextDay(self): """ Test all days of the week get next day """ for i in range(1,30): self.assertEqual(self.schedule.getNextEntry(datetime(2008, 01, i, 23, 59, 00, 00)), datetime(2008,01,(i/5)*5+5,00,00,00,00)) def testNextMonth(self): """ Test all days of the week get next month """ self.assertEqual(self.schedule.getNextEntry(datetime(2008, 01, 31, 23, 59, 00, 00)), datetime(2008,02,05,00,00,00,00))
def valid_cronrule(self,rule): rule=rule.strip() try: CronSchedule(rule) except: return (False,"时间规则不符合要求") else: return True
class AllDOMTestCase(TestCase): def setUp(self): self.schedule = CronSchedule('*/15 * * * 1,3,5') def testNextDay(self): """ Test all days of the month get next day """ self.assertEqual( self.schedule.getNextEntry(datetime(2008, 9, 1, 23, 59, 00, 00)), datetime(2008, 9, 3, 00, 00, 00, 00)) self.assertEqual( self.schedule.getNextEntry(datetime(2008, 9, 15, 23, 59, 00, 00)), datetime(2008, 9, 17, 00, 00, 00, 00)) self.assertEqual( self.schedule.getNextEntry(datetime(2008, 9, 16, 23, 59, 00, 00)), datetime(2008, 9, 17, 00, 00, 00, 00)) self.assertEqual( self.schedule.getNextEntry(datetime(2008, 9, 17, 23, 59, 00, 00)), datetime(2008, 9, 19, 00, 00, 00, 00)) def testNextMonth(self): """ Test all days of the month get next month """ self.assertEqual( self.schedule.getNextEntry(datetime(2008, 6, 30, 23, 59, 00, 00)), datetime(2008, 7, 2, 00, 00, 00, 00)) self.assertEqual( self.schedule.getNextEntry(datetime(2008, 8, 29, 23, 59, 00, 00)), datetime(2008, 9, 1, 00, 00, 00, 00)) self.assertEqual( self.schedule.getNextEntry(datetime(2008, 9, 29, 23, 59, 00, 00)), datetime(2008, 10, 1, 00, 00, 00, 00))
class AllDOMTestCase(TestCase): def setUp(self): self.schedule = CronSchedule('*/15 * * * 1,3,5') def testNextDay(self): """ Test all days of the month get next day """ self.assertEqual(self.schedule.getNextEntry(datetime(2008, 9, 1, 23, 59, 00, 00)), datetime(2008,9,3,00,00,00,00)) self.assertEqual(self.schedule.getNextEntry(datetime(2008, 9, 15, 23, 59, 00, 00)), datetime(2008,9,17,00,00,00,00)) self.assertEqual(self.schedule.getNextEntry(datetime(2008, 9, 16, 23, 59, 00, 00)), datetime(2008,9,17,00,00,00,00)) self.assertEqual(self.schedule.getNextEntry(datetime(2008, 9, 17, 23, 59, 00, 00)), datetime(2008,9,19,00,00,00,00)) def testNextMonth(self): """ Test all days of the month get next month """ self.assertEqual(self.schedule.getNextEntry(datetime(2008, 6, 30, 23, 59, 00, 00)), datetime(2008,7,2,00,00,00,00)) self.assertEqual(self.schedule.getNextEntry(datetime(2008, 8, 29, 23, 59, 00, 00)), datetime(2008,9,1,00,00,00,00)) self.assertEqual(self.schedule.getNextEntry(datetime(2008, 9, 29, 23, 59, 00, 00)), datetime(2008,10,1,00,00,00,00))
def _load_triggers(self): ''' This function loads all the triggers from the database. ''' # Stop any outstanding absolute time triggers in case of a reload. for schedule in self._absolute_time_schedule_calls: schedule.stop() self._absolute_time_schedule_calls = [] triggers = yield self.db.query_triggers() for trigger in triggers: t = Trigger(trigger[1], trigger[2], trigger[3]) # get trigger parameters trigger_parameters = yield self.db.query_trigger_parameters( trigger[0]) for param in trigger_parameters: if param[0] == "cron": t.cron = param[1] elif param[0] == "current_value_id": t.current_value_id = param[1] elif param[0] == "condition": t.condition = param[1] elif param[0] == "condition_value": t.condition_value = param[1] # Handle absolute time directly, and schedule. No need to keep track of this. if trigger[1] == "Absolute time": c = CronSchedule(t.cron) print c.getNextEntry() s = ScheduledCall(f=self._absolute_time_triggered, eventid=trigger[2], conditions=trigger[3]) s.start(c) self._absolute_time_schedule_calls.append(s) continue self._triggers.append(t)
def _load_triggers(self): ''' This function loads all the triggers from the database. ''' # Stop any outstanding absolute time triggers in case of a reload. for schedule in self._absolute_time_schedule_calls: schedule.stop() self._absolute_time_schedule_calls = [] triggers = yield self.db.query_triggers() for trigger in triggers: t = Trigger(trigger[1], trigger[2], trigger[3]) # get trigger parameters trigger_parameters = yield self.db.query_trigger_parameters(trigger[0]) for param in trigger_parameters: if param[0] == "cron": t.cron = param[1] elif param[0] == "current_value_id": t.current_value_id = param[1] elif param[0] == "condition": t.condition = param[1] elif param[0] == "condition_value": t.condition_value = param[1] # Handle absolute time directly, and schedule. No need to keep track of this. if trigger[1] == "Absolute time": c = CronSchedule(t.cron) print c.getNextEntry() s = ScheduledCall(f=self._absolute_time_triggered, eventid=trigger[2], conditions=trigger[3]) s.start(c) self._absolute_time_schedule_calls.append(s) continue self._triggers.append(t)
def xmlrpc_cron_set(self, key, cron_mode, picked_function, picked_callback, picked_errback, stdout_callback, stderr_callback): print "cron server _ cron set" try: self.xmlrpc_cron_del(key) if self.valid_cronrule(cron_mode): pass else: raise Exception("cron时间规则不符合要求") try: target = cloudpickle.loads(picked_function.data) except: traceback.print_exc() raise Exception("目标函数picked反序列化失败!") try: callback = cloudpickle.loads(picked_callback.data) except: raise Exception("success回调函数picked反序列化失败!") try: errback = cloudpickle.loads(picked_errback.data) except: raise Exception("error回调函数picked反序列化失败!") if stdout_callback: try: stdout_callback = cloudpickle.loads(stdout_callback.data) except: raise Exception("stdout回调函数picked反序列化失败!") else: stdout_callback = None if stderr_callback: try: stderr_callback = cloudpickle.loads(stderr_callback.data) except: raise Exception("stderr回调函数picked反序列化失败!") else: stderr_callback = None cron = CronSchedule(cron_mode) _process = Process(target, [], {}, callback, errback, stdout_callback, stderr_callback) _task = ScheduledCall(_process.restart) _task.start(cron) CRON_BUFF[key] = (_task, _process) except Exception as e: traceback.print_exc() return False, str(e) else: return True
class StarTestCase(TestCase): def setUp(self): self.schedule = CronSchedule('* * * * *') def testNextMinute(self): """ Next runtime is next minute """ for i in range(0,59): self.assertEqual(self.schedule.getNextEntry(datetime(2008, 01, 01, 00, i, 00, 00)), datetime(2008,01,01,00,i+1,00,00)) def testNextHour(self): """ Next runtime is next hour """ for i in range(0,23): self.assertEqual(self.schedule.getNextEntry(datetime(2008, 01, 01, i, 59, 00, 00)), datetime(2008,01,01,i+1,00,00,00)) def testNextDay(self): """ Next runtime is next day """ for i in range(1,31): self.assertEqual(self.schedule.getNextEntry(datetime(2008, 01, i, 23, 59, 00, 00)), datetime(2008,01,i+1,00,00,00,00)) def testNextMonth(self): """ Next runtime is next month """ self.assertEqual(self.schedule.getNextEntry(datetime(2008, 01, 31, 23, 59, 00, 00)), datetime(2008,02,01,00,00,00,00)) def testNextYear(self): """ Next runtime is next year """ self.assertEqual(self.schedule.getNextEntry(datetime(2008, 12, 31, 23, 59, 00, 00)), datetime(2009,01,01,00,00,00,00))
class StarTestCase(TestCase): def setUp(self): self.schedule = CronSchedule('* * * * *') def testNextMinute(self): """ Next runtime is next minute """ for i in range(0, 59): self.assertEqual( self.schedule.getNextEntry( datetime(2008, 01, 01, 00, i, 00, 00)), datetime(2008, 01, 01, 00, i + 1, 00, 00)) def testNextHour(self): """ Next runtime is next hour """ for i in range(0, 23): self.assertEqual( self.schedule.getNextEntry( datetime(2008, 01, 01, i, 59, 00, 00)), datetime(2008, 01, 01, i + 1, 00, 00, 00)) def testNextDay(self): """ Next runtime is next day """ for i in range(1, 31): self.assertEqual( self.schedule.getNextEntry( datetime(2008, 01, i, 23, 59, 00, 00)), datetime(2008, 01, i + 1, 00, 00, 00, 00)) def testNextMonth(self): """ Next runtime is next month """ self.assertEqual( self.schedule.getNextEntry(datetime(2008, 01, 31, 23, 59, 00, 00)), datetime(2008, 02, 01, 00, 00, 00, 00)) def testNextYear(self): """ Next runtime is next year """ self.assertEqual( self.schedule.getNextEntry(datetime(2008, 12, 31, 23, 59, 00, 00)), datetime(2009, 01, 01, 00, 00, 00, 00))
def setUp(self): self.schedule = CronSchedule('*/15 * * * 1,3,5')
def test_getFirstDayWithSundayDOW(self): """ Test next day with Sunday DOW entry """ schedule = CronSchedule('* * * * 0,3,5') self.assertEqual(schedule.getNextEntry(datetime(2008,8,31,23,59,00,00)), datetime(2008,9,3,00,00,00,00))
def setUp(self): self.schedule = CronSchedule('*/15 * */5 * *')
def test_equality(self): """ CronSchedule equality testing """ self.assertEqual(self.schedule, CronSchedule('* * * * *')) self.assertEqual(self.schedule == 'blah', False)
def setUp(self): self.schedule = CronSchedule.from_cron_line('15-20 3-6 5-10 5-8 2-3')
def cron_edit(request): '''修改计划任务 参数: tid 计划任务的ID name 名称 rule 计划任务规则 repo_id 代码库id version 代码库的版本 pre_build 预构建命令 info 说明 run_cmd 运行命令 worker_id worker的id group_id 分组的id ''' post_info = request.POST tid = post_info.get("tid").strip() name = post_info.get("name", "").strip() rule = post_info.get("rule", "").strip() repo_id = post_info.get("repo_id", "") version = post_info.get("version", "").strip() pre_build = post_info.get("pre_build", "") info = post_info.get("info", "") run_cmd = post_info.get("run_cmd", "").strip() worker_id = post_info.get("worker_id") group_id = post_info.get("group_id") tid = int(tid) cron = CronTask.objects.get(tid=tid) if not name: raise FieldError("name", "名称不能为空") if not rule: raise FieldError("rule", "时间规则不能为空") try: CronSchedule(rule) except: raise FieldError("rule", "时间规则不合法!") if not repo_id: raise FieldError("repo_id", "请选择一个代码库") repo_id = int(repo_id) Repo.objects.get(id=int(repo_id)) if not version: raise FieldError("version", "版本不能为空!") if not run_cmd: raise FieldError("run_cmd", "执行命令不能为空!") if not worker_id: raise FieldError("work_id", "请选择一个worker节点") work_id = int(worker_id) _worker = Worker.objects.get(id=int(work_id)) if work_id != cron.worker_id: raise FieldError("work_id", "不允许修改所在的worker节点") if not group_id: raise FieldError("groupid", "请选择一个分组") if not _worker.is_alive(): raise FieldError("worker_id", "worker节点已经下线!") group_id = int(group_id) Group.objects.get(id=group_id) if repo_id != cron.repo_id or version != cron.version or pre_build != cron.pre_build: cron.name = name cron.rule = rule cron.repo_id = repo_id cron.uptime = int(time.time()) cron.version = version cron.pre_build = pre_build cron.info = info cron.run_cmd = run_cmd cron.group_id = group_id cron.save() cron.pure_init() return True else: cron.name = name cron.rule = rule cron.repo_id = repo_id cron.uptime = int(time.time()) cron.version = version cron.pre_build = pre_build cron.info = info cron.run_cmd = run_cmd cron.group_id = group_id cron.enable() return True
def setUp(self): self.schedule = CronSchedule.from_cron_line('* * * * *')
def setUp(self): self.schedule = CronSchedule.from_cron_line('*/15 * * * 1,3,5')
def setUp(self): self.schedule = CronSchedule('15-20 3-6 5-10 5-8 2-3')
class RangeTestCase(TestCase): def setUp(self): self.schedule = CronSchedule('15-20 3-6 5-10 5-8 2-3') def testNextMinute(self): """ Test range get next minute """ # Test minutes that should end up in the current hour at 15 minutes for i in range(0, 14): self.assertEqual( self.schedule.getNextEntry( datetime(2008, 05, 05, 03, i, 00, 00)), datetime(2008, 05, 05, 03, 15, 00, 00)) # Test minutes that should end up in the current hour at i+1 minutes for i in range(14, 20): self.assertEqual( self.schedule.getNextEntry( datetime(2008, 05, 05, 03, i, 00, 00)), datetime(2008, 05, 05, 03, i + 1, 00, 00)) # Test minutes that should end up at hour+1 and 15 minutes for i in range(20, 60): self.assertEqual( self.schedule.getNextEntry( datetime(2008, 05, 05, 03, i, 00, 00)), datetime(2008, 05, 05, 04, 15, 00, 00)) def testNextHour(self): """ Test range get next hour """ for i in range(0, 3): self.assertEqual( self.schedule.getNextEntry( datetime(2008, 05, 05, i, 59, 00, 00)), datetime(2008, 05, 05, 3, 15, 00, 00)) for i in range(3, 6): self.assertEqual( self.schedule.getNextEntry( datetime(2008, 05, 05, i, 59, 00, 00)), datetime(2008, 05, 05, i + 1, 15, 00, 00)) for i in range(6, 24): self.assertEqual( self.schedule.getNextEntry( datetime(2008, 05, 05, i, 59, 00, 00)), datetime(2008, 05, 06, 03, 15, 00, 00)) def testNextDay(self): """ Test range get next day """ for i in range(1, 5): self.assertEqual( self.schedule.getNextEntry( datetime(2008, 05, i, 23, 59, 00, 00)), datetime(2008, 05, 5, 3, 15, 00, 00)) for i in range(5, 10): self.assertEqual( self.schedule.getNextEntry( datetime(2008, 05, i, 23, 59, 00, 00)), datetime(2008, 05, i + 1, 3, 15, 00, 00)) for i in range(10, 13): self.assertEqual( self.schedule.getNextEntry( datetime(2008, 05, i, 23, 59, 00, 00)), datetime(2008, 05, 13, 3, 15, 00, 00)) self.assertEqual( self.schedule.getNextEntry(datetime(2008, 05, 13, 23, 59, 00, 00)), datetime(2008, 05, 14, 3, 15, 00, 00)) for i in range(14, 20): self.assertEqual( self.schedule.getNextEntry( datetime(2008, 05, i, 23, 59, 00, 00)), datetime(2008, 05, 20, 3, 15, 00, 00)) self.assertEqual( self.schedule.getNextEntry(datetime(2008, 05, 20, 23, 59, 00, 00)), datetime(2008, 05, 21, 3, 15, 00, 00)) def testNextMonth(self): """ Test range get next month """ self.assertEqual( self.schedule.getNextEntry(datetime(2008, 01, 01, 00, 00, 00, 00)), datetime(2008, 05, 05, 03, 15, 00, 00)) self.assertEqual( self.schedule.getNextEntry(datetime(2008, 05, 31, 23, 59, 00, 00)), datetime(2008, 06, 03, 03, 15, 00, 00)) self.assertEqual( self.schedule.getNextEntry(datetime(2008, 07, 30, 23, 59, 00, 00)), datetime(2008, 8, 05, 03, 15, 00, 00)) self.assertEqual( self.schedule.getNextEntry(datetime(2008, 8, 31, 23, 59, 00, 00)), datetime(2009, 05, 05, 03, 15, 00, 00))
class RangeTestCase(TestCase): def setUp(self): self.schedule = CronSchedule('15-20 3-6 5-10 5-8 2-3') def testNextMinute(self): """ Test range get next minute """ # Test minutes that should end up in the current hour at 15 minutes for i in range(0,14): self.assertEqual(self.schedule.getNextEntry(datetime(2008, 05, 05, 03, i, 00, 00)), datetime(2008,05,05,03,15,00,00)) # Test minutes that should end up in the current hour at i+1 minutes for i in range(14,20): self.assertEqual(self.schedule.getNextEntry(datetime(2008, 05, 05, 03, i, 00, 00)), datetime(2008,05,05,03,i+1,00,00)) # Test minutes that should end up at hour+1 and 15 minutes for i in range(20,60): self.assertEqual(self.schedule.getNextEntry(datetime(2008, 05, 05, 03, i, 00, 00)), datetime(2008,05,05,04,15,00,00)) def testNextHour(self): """ Test range get next hour """ for i in range(0,3): self.assertEqual(self.schedule.getNextEntry(datetime(2008, 05, 05, i, 59, 00, 00)), datetime(2008,05,05,3,15,00,00)) for i in range(3,6): self.assertEqual(self.schedule.getNextEntry(datetime(2008, 05, 05, i, 59, 00, 00)), datetime(2008,05,05,i+1,15,00,00)) for i in range(6,24): self.assertEqual(self.schedule.getNextEntry(datetime(2008, 05, 05, i, 59, 00, 00)), datetime(2008,05,06,03,15,00,00)) def testNextDay(self): """ Test range get next day """ for i in range(1,5): self.assertEqual(self.schedule.getNextEntry(datetime(2008, 05, i, 23, 59, 00, 00)), datetime(2008,05,5,3,15,00,00)) for i in range(5,10): self.assertEqual(self.schedule.getNextEntry(datetime(2008, 05, i, 23, 59, 00, 00)), datetime(2008,05,i+1,3,15,00,00)) for i in range(10,13): self.assertEqual(self.schedule.getNextEntry(datetime(2008, 05, i, 23, 59, 00, 00)), datetime(2008,05,13,3,15,00,00)) self.assertEqual(self.schedule.getNextEntry(datetime(2008, 05, 13, 23, 59, 00, 00)), datetime(2008,05,14,3,15,00,00)) for i in range(14,20): self.assertEqual(self.schedule.getNextEntry(datetime(2008, 05, i, 23, 59, 00, 00)), datetime(2008,05,20,3,15,00,00)) self.assertEqual(self.schedule.getNextEntry(datetime(2008, 05, 20, 23, 59, 00, 00)), datetime(2008,05,21,3,15,00,00)) def testNextMonth(self): """ Test range get next month """ self.assertEqual(self.schedule.getNextEntry(datetime(2008, 01, 01, 00, 00, 00, 00)), datetime(2008,05,05,03,15,00,00)) self.assertEqual(self.schedule.getNextEntry(datetime(2008, 05, 31, 23, 59, 00, 00)), datetime(2008,06,03,03,15,00,00)) self.assertEqual(self.schedule.getNextEntry(datetime(2008, 07, 30, 23, 59, 00, 00)), datetime(2008,8,05,03,15,00,00)) self.assertEqual(self.schedule.getNextEntry(datetime(2008, 8, 31, 23, 59, 00, 00)), datetime(2009,05,05,03,15,00,00))
def setUp(self): self.schedule = CronSchedule('* * * * *')
def cron_add(request): ''' 创建计划任务 参数: name 名称 rule 计划任务规则 repo_id 代码库id version 代码库的版本 pre_build 预构建命令 info 说明 run_cmd 运行命令 worker_id worker的id group_id 分组的id ''' post_info = request.POST name = post_info.get("name", "") rule = post_info.get("rule", "").strip() repo_id = post_info.get("repo_id", "") version = post_info.get("version", "").strip() pre_build = post_info.get("pre_build", "") info = post_info.get("info", "") run_cmd = post_info.get("run_cmd", "").strip() worker_id = post_info.get("worker_id") group_id = post_info.get("group_id") if not name: raise FieldError("name", "名称不能为空") if not rule: raise FieldError("rule", "时间规则不能为空") try: CronSchedule(rule) except: raise FieldError("rule", "时间规则不合法!") if not repo_id: raise FieldError("repo_id", "请选择一个代码库") repo_id = int(repo_id) Repo.objects.get(id=int(repo_id)) if not version: raise FieldError("version", "版本不能为空!") if not run_cmd: raise FieldError("run_cmd", "执行命令不能为空!") if not worker_id: raise FieldError("worker_id", "请选择一个worker节点") work_id = int(worker_id) _worker = Worker.objects.get(id=int(work_id)) if not group_id: raise FieldError("groupid", "请选择一个分组") if not _worker.is_alive(): raise FieldError("worker_id", "worker节点已经下线!") group_id = int(group_id) Group.objects.get(id=group_id) _ = CronTask(name=name, rule=rule, repo_id=repo_id, version=version, pre_build=pre_build, info=info, owner=request.user.username, run_cmd=run_cmd, worker_id=work_id, group_id=group_id) _.save() _.pure_init() return True
def test_equality(self): """ CronSchedule equality testing """ self.assertEqual(self.schedule,CronSchedule.from_cron_line('* * * * *')) self.assertEqual(self.schedule == 'blah',False)