def action_change_process_state(self): resp = dict() thread_handler = self.mbean.thread_handlers[self.process_name] document = thread_handler.args[1] # of type SchedulerConfigurationEntry state = self.request.args.get('state') if state is None: # request was performed with undefined "state", what means that checkbox was unselected # thus - turning off the process thread_handler.cancel() document.set_process_state(SchedulerConfigurationEntry.STATE_OFF) message = 'Stopped RepeatTimer for %s' % document.get_process_name() elif not thread_handler.is_alive(): document.set_process_state(SchedulerConfigurationEntry.STATE_ON) thread_handler = RepeatTimer(thread_handler.interval_current, thread_handler.callable, thread_handler.args, thread_handler.kwargs) thread_handler.start() self.mbean.thread_handlers[self.process_name] = thread_handler message = 'Started RepeatTimer for %s, triggering every %d seconds' \ % (document.get_process_name(), document.get_interval()) else: message = 'RepeatTimer for %s is already active. Ignoring request.' % document.get_process_name() scheduler_configuration_helper.update(self.logger, document) self.logger.info(message) resp['status'] = message return resp
def __init__(self, logger): self.logger = logger self.trackers = dict() self.interval = settings['perf_ticker_interval'] self.mark_24_hours = time.time() self.mark_footprint = time.time() self.footprint = FootprintCalculator() self.timer = RepeatTimer(self.interval, self._run_tick_thread, daemonic=True)
class TickerThread(object): SECONDS_IN_24_HOURS = 86400 TICKS_BETWEEN_FOOTPRINTS = 10 def __init__(self, logger): self.logger = logger self.trackers = dict() self.interval = settings['perf_ticker_interval'] self.mark_24_hours = time.time() self.mark_footprint = time.time() self.footprint = FootprintCalculator() self.timer = RepeatTimer(self.interval, self._run_tick_thread, daemonic=True) def add_tracker(self, tracker): self.trackers[tracker.name] = tracker def get_tracker(self, name): return self.trackers[name] def start(self): self.timer.start() def cancel(self): self.timer.cancel() def is_alive(self): return self.timer.is_alive() def _print_footprint(self): if time.time( ) - self.mark_footprint > self.TICKS_BETWEEN_FOOTPRINTS * self.interval: self.logger.info(self.footprint.get_snapshot()) self.mark_footprint = time.time() def _run_tick_thread(self): self._print_footprint() current_time = time.time() do_24h_reset = current_time - self.mark_24_hours > self.SECONDS_IN_24_HOURS if do_24h_reset: self.mark_24_hours = current_time tracker_outputs = [] for tracker_name, tracker in self.trackers.items(): tracker_outputs.append(tracker.to_string(self.interval)) tracker.reset_tick() if do_24h_reset: tracker.reset_24h() self.logger.info('\n'.join(tracker_outputs))
def start(self, *_): """ reading box configurations and starting timers to start/monitor/kill processes """ try: box_config = self.bc_dao.get_one(self.box_id) process_list = box_config.process_list for process in process_list: params = [process] handler = RepeatTimer(TRIGGER_INTERVAL, self.manage_process, args=params) self.thread_handlers[process] = handler handler.start() self.logger.info('Started Supervisor for %s, triggering every %d seconds' % (process, TRIGGER_INTERVAL)) except LookupError as e: self.logger.error('Supervisor failed to start because of: %r' % e)
class WorkerPerformanceTicker(object): SECONDS_IN_24_HOURS = 86400 TICKS_BETWEEN_FOOTPRINTS = 10 def __init__(self, logger): self.logger = logger self.posts_per_24_hours = 0 self.posts_per_tick = 0 self.interval = settings['perf_ticker_interval'] self.mark_24_hours = time.time() self.mark_footprint = time.time() self.footprint = FootprintCalculator() def start(self): self.timer = RepeatTimer(self.interval, self._run_tick_thread) self.timer.start() def cancel(self): self.timer.cancel() def _print_footprint(self): if time.time() - self.mark_footprint > self.TICKS_BETWEEN_FOOTPRINTS * self.interval: self.logger.info(self.footprint.get_snapshot()) self.mark_footprint = time.time() def _run_tick_thread(self): self._print_footprint() self.logger.info('Processed: %d in last %d seconds; %d in last 24 hours;' \ % (self.posts_per_tick, self.interval, self.posts_per_24_hours)) self.posts_per_tick = 0 if time.time() - self.mark_24_hours > self.SECONDS_IN_24_HOURS: self.mark_24_hours = time.time() self.posts_per_24_hours = 0 def increment(self): self.posts_per_tick += 1 self.posts_per_24_hours += 1
def start(self): """ reading scheduler configurations and starting timers to trigger events """ collection = CollectionContext.get_collection(self.logger, COLLECTION_SCHEDULER_CONFIGURATION) cursor = collection.find({}) if cursor.count() == 0: raise LookupError('MongoDB has no scheduler configuration entries') for entry in cursor: document = SchedulerConfigurationEntry(entry) interval = document.get_interval() is_active = document.get_process_state() == SchedulerConfigurationEntry.STATE_ON type = ProcessContext.get_type(document.get_process_name()) parameters = [document.get_process_name(), document] if type == TYPE_ALERT: function = self.fire_alert elif type == TYPE_HORIZONTAL_AGGREGATOR: function = self.fire_worker elif type == TYPE_VERTICAL_AGGREGATOR: function = self.fire_worker elif type == TYPE_GARBAGE_COLLECTOR: function = self.fire_garbage_collector else: self.logger.error('Can not start scheduler for %s since it has no processing function' % type) continue handler = RepeatTimer(interval, function, args=parameters) self.thread_handlers[document.get_process_name()] = handler if is_active: handler.start() self.logger.info('Started scheduler for %s:%s, triggering every %d seconds'\ % (type, document.get_process_name(), interval)) else: self.logger.info('Handler for %s:%s registered in Scheduler. Idle until activated.'\ % (type, document.get_process_name())) # as Scheduler is now initialized and running - we can safely start its MX self.start_mx()
def start(self): self.timer = RepeatTimer(self.interval, self._run_tick_thread) self.timer.start()