def __init__(self): ''' Load initial Astral configuration from Astral.conf ''' config_file = config_to_location('Astral.conf') self.config = ConfigParser.RawConfigParser() self.config.read(os.path.join(config_file)) # Get broker information (ZeroMQ) self.coordinator_host = self.config.get("coordinator", "host") self.coordinator_port = self.config.getint("coordinator", "port") self.loglevel = self.config.get('general', 'loglevel') self.log = Logging("Astral", console=True) self.log.set_level(self.loglevel) self.id = self.config.get('general', 'id') callbacks = {} self.pluginapi = pluginapi.PluginAPI(self.id, 'Astral', broker_host=self.coordinator_host, broker_port=self.coordinator_port, **callbacks) self.pluginapi.ready() c = CronSchedule("* * * * *") s = ScheduledCall(f=self.fire_minute) s.start(c) reactor.run() return True
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)))
def schedule_standup(client, channel, cron_string=None): chan = client.config["channels"].get(channel) if not chan: log.msg("Channel %s not found in config, skipping standup check" % (channel)) return False if not cron_string: cron_string = chan["time"] try: cs = CronSchedule(cron_string) except Exception as e: log.msg("Exception parsing cron string '%s': %s" % (cron_string, e)) sc = SCHEDULES.get(channel) if not sc: log.msg("Creating new scheduledcall for channel %s @ %s" % (channel, cron_string)) sc = ScheduledCall(IRC.run, client, channel) if sc.running: sc.stop() SCHEDULES[channel] = sc try: sc.start(cs) client.config["channels"][channel]["time"] = cron_string client.config.flush() log.msg("schedule started for channel %s @ %s" % (channel, cron_string)) return True except Exception as e: log.msg("Standup could not be scheduled for time %s in channel %s: %s" % (cron_string, channel, e)) return False
def _register_crontab_timer(self, timer_class): """ Registers a CrontabTimer sub-class. These fire on pre-set schedules. """ schedule = CronSchedule(timer_class.schedule) # noinspection PyProtectedMember call = ScheduledCall(timer_class.fire, self.protocol) call.start(schedule) self._timers.append(call)
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 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
def initialize_plugin(self, plugin): if not hasattr(plugin, 'is_initialized'): setattr(plugin, 'filesystem', self.filesystem) setattr(plugin, 'is_initialized', True) setattr(plugin, 'paths', self.paths) setattr(plugin, 'configfile', self.configfile) setattr(plugin, 'config_section', 'plugin-%s' % plugin.name) if hasattr(plugin, 'initialize'): plugin.initialize() if ICrontab.providedBy(plugin): result = plugin.schedule() if result: crontab_syntax, f = result crontab = ScheduledCall(f) crontab.start(CronSchedule(crontab_syntax))
class SimpleTests(TestCase): """ Tests to verify basic behavior """ def setUp(self): super(SimpleTests, self).setUp() self.sc = ScheduledCall(f=lambda: None) def test_clock(self): """ Test that the clock used is the reactor """ self.assertEqual(self.sc.clock, reactor) def test_stoppingStopped(self): """ Test an invalid stop call to a stopped ScheduledCall """ self.assertRaises(AssertionError, self.sc.stop) def test_startingStarted(self): """ Test an invalid start call to a running ScheduledCall """ self.sc.start(SimpleSchedule()) self.assertRaises(AssertionError, self.sc.start, SimpleSchedule()) self.sc.stop() def test_startingInvalidSchedule(self): """ Test calling start with an invalid schedule """ self.assertRaises(TypeError, self.sc.start, 'this is garbage') def test_testStopCallback(self): """ Test start deferred callback on stop """ def cb(result): self.assertIdentical(result, self.sc) d = self.sc.start(SimpleSchedule()) d.addCallback(cb) self.sc.stop()
class SimpleTests(TestCase): """ Tests to verify basic behavior """ def setUp(self): super(SimpleTests, self).setUp() self.sc = ScheduledCall(f=lambda: None) def test_clock(self): """ Test that the clock used is the reactor """ self.assertEqual(self.sc.clock, reactor) def test_stoppingStopped(self): """ Test an invalid stop call to a stopped ScheduledCall """ self.assertRaises(AssertionError, self.sc.stop) def test_startingStarted(self): """ Test an invalid start call to a running ScheduledCall """ self.sc.start(SimpleSchedule()) self.assertRaises(AssertionError,self.sc.start,SimpleSchedule()) self.sc.stop() def test_startingInvalidSchedule(self): """ Test calling start with an invalid schedule """ self.assertRaises(TypeError, self.sc.start,'this is garbage') def test_testStopCallback(self): """ Test start deferred callback on stop """ def cb(result): self.assertIdentical(result, self.sc) d = self.sc.start(SimpleSchedule()) d.addCallback(cb) self.sc.stop()
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 setUp(self): super(SimpleTests, self).setUp() self.sc = ScheduledCall(f=lambda: None)
def __init__(self, clock, *a, **kw): ScheduledCall.__init__(self, *a, **kw) self.clock = clock
def __init__(self, clock, *a, **kw): ScheduledCall.__init__(self,*a, **kw) self.clock = clock