def _run_worker(): LOG.info("(PID=%s) TimerEngine started.", os.getpid()) timer = None try: timer_thread = None if cfg.CONF.timer.enable or cfg.CONF.timersengine.enable: local_tz = (cfg.CONF.timer.local_timezone or cfg.CONF.timersengine.local_timezone) timer = St2Timer(local_timezone=local_tz) timer_thread = concurrency.spawn(_kickoff_timer, timer) LOG.info(TIMER_ENABLED_LOG_LINE) return timer_thread.wait() else: LOG.info(TIMER_DISABLED_LOG_LINE) except (KeyboardInterrupt, SystemExit): LOG.info("(PID=%s) TimerEngine stopped.", os.getpid()) deregister_service(TIMER_ENGINE) except: LOG.exception("(PID:%s) TimerEngine quit due to exception.", os.getpid()) return 1 finally: if timer: timer.cleanup() return 0
def test_existing_rules_are_loaded_on_start(self): # Assert that we dispatch message for every existing Trigger object St2Timer._handle_create_trigger = mock.Mock() timer = St2Timer() timer._scheduler = mock.Mock() timer._trigger_watcher.run = mock.Mock() # Verify there are no Trigger and TriggerType in the db wh:w self.assertItemsEqual(Trigger.get_all(), []) self.assertItemsEqual(TriggerType.get_all(), []) # Add a dummy timer Trigger object type_ = list(TIMER_TRIGGER_TYPES.keys())[0] parameters = {'unit': 'seconds', 'delta': 1000} trigger_db = TriggerDB(id=bson.ObjectId(), name='test_trigger_1', pack='dummy', type=type_, parameters=parameters) trigger_db = Trigger.add_or_update(trigger_db) # Verify object has been added self.assertEqual(len(Trigger.get_all()), 1) timer.start() timer._trigger_watcher._load_thread.wait() # Verify handlers are called timer._handle_create_trigger.assert_called_with(trigger_db)
def main(): timer = St2Timer(local_timezone=cfg.CONF.timer.local_timezone) try: _setup() timer_thread = eventlet.spawn(_kickoff_timer, timer) worker_thread = eventlet.spawn(_kickoff_rules_worker, worker) return (timer_thread.wait() and worker_thread.wait()) except SystemExit as exit_code: sys.exit(exit_code) except: LOG.exception('(PID:%s) RulesEngine quit due to exception.', os.getpid()) return 1 finally: timer.cleanup() _teardown()
def test_timer_trace_tag_creation(self, dispatch_mock): timer = St2Timer() timer._scheduler = mock.Mock() timer._trigger_watcher = mock.Mock() # Add a dummy timer Trigger object type_ = list(TIMER_TRIGGER_TYPES.keys())[0] parameters = {'unit': 'seconds', 'delta': 1} trigger_db = TriggerDB(name='test_trigger_1', pack='dummy', type=type_, parameters=parameters) timer.add_trigger(trigger_db) timer._emit_trigger_instance(trigger=trigger_db.to_serializable_dict()) self.assertEqual( dispatch_mock.call_args[1]['trace_context'].trace_tag, '%s-%s' % (TIMER_TRIGGER_TYPES[type_]['name'], trigger_db.name))
def test_trigger_types_are_registered_on_start(self): timer = St2Timer() timer._scheduler = Mock() # Verify there are no TriggerType in the db when we start self.assertItemsEqual(TriggerType.get_all(), []) timer.start() # Verify TriggerType objects have been created trigger_type_dbs = TriggerType.get_all() self.assertEqual(len(trigger_type_dbs), len(TIMER_TRIGGER_TYPES)) timer_trigger_type_refs = TIMER_TRIGGER_TYPES.keys() for trigger_type in trigger_type_dbs: ref = ResourceReference(pack=trigger_type.pack, name=trigger_type.name).ref self.assertTrue(ref in timer_trigger_type_refs)
def test_timer_trace_tag_creation(self, dispatch_mock): timer = St2Timer() timer._scheduler = mock.Mock() timer._trigger_watcher = mock.Mock() # Add a dummy timer Trigger object type_ = list(TIMER_TRIGGER_TYPES.keys())[0] parameters = {"unit": "seconds", "delta": 1} trigger_db = TriggerDB(name="test_trigger_1", pack="dummy", type=type_, parameters=parameters) timer.add_trigger(trigger_db) timer._emit_trigger_instance(trigger=trigger_db.to_serializable_dict()) self.assertEqual( dispatch_mock.call_args[1]["trace_context"].trace_tag, "%s-%s" % (TIMER_TRIGGER_TYPES[type_]["name"], trigger_db.name), )
def _run_worker(): LOG.info('(PID=%s) RulesEngine started.', os.getpid()) timer = St2Timer(local_timezone=cfg.CONF.timer.local_timezone) rules_engine_worker = worker.get_worker() try: timer_thread = eventlet.spawn(_kickoff_timer, timer) rules_engine_worker.start() return timer_thread.wait() and rules_engine_worker.wait() except (KeyboardInterrupt, SystemExit): LOG.info('(PID=%s) RulesEngine stopped.', os.getpid()) rules_engine_worker.shutdown() except: LOG.exception('(PID:%s) RulesEngine quit due to exception.', os.getpid()) return 1 finally: timer.cleanup() return 0