diff --git a/core/runner.py b/core/runner.py index 648e673..e3adf20 100644 --- a/core/runner.py +++ b/core/runner.py @@ -12,7 +12,7 @@ import threading from multiprocessing import Pipe from random import randrange -from typing import Any, Union, Dict, List +from typing import Any, Union, Dict, List, Type from meow_base.core.base_conductor import BaseConductor from meow_base.core.base_handler import BaseHandler @@ -364,6 +364,58 @@ class MeowRunner: print_debug(self._print_target, self.debug_level, "Job conductor thread stopped", DEBUG_INFO) + def get_monitor_by_name(self, queried_name:str)->BaseMonitor: + """Gets a runner monitor with a name matching the queried name. Note + in the case of multiple monitors having the same name, only the first + match is returned.""" + return self._get_entity_by_name(queried_name, self.monitors) + + def get_monitor_by_type(self, queried_type:Type)->BaseMonitor: + """Gets a runner monitor with a type matching the queried type. Note + in the case of multiple monitors having the same name, only the first + match is returned.""" + return self._get_entity_by_type(queried_type, self.monitors) + + def get_handler_by_name(self, queried_name:str)->BaseHandler: + """Gets a runner handler with a name matching the queried name. Note + in the case of multiple handlers having the same name, only the first + match is returned.""" + return self._get_entity_by_name(queried_name, self.handlers) + + def get_handler_by_type(self, queried_type:Type)->BaseHandler: + """Gets a runner handler with a type matching the queried type. Note + in the case of multiple handlers having the same name, only the first + match is returned.""" + return self._get_entity_by_type(queried_type, self.handlers) + + def get_conductor_by_name(self, queried_name:str)->BaseConductor: + """Gets a runner conductor with a name matching the queried name. Note + in the case of multiple conductors having the same name, only the first + match is returned.""" + return self._get_entity_by_name(queried_name, self.conductors) + + def get_conductor_by_type(self, queried_type:Type)->BaseConductor: + """Gets a runner conductor with a type matching the queried type. Note + in the case of multiple conductors having the same name, only the first + match is returned.""" + return self._get_entity_by_type(queried_type, self.conductors) + + def _get_entity_by_name(self, queried_name:str, entities + )->Union[BaseMonitor,BaseHandler,BaseConductor]: + """Base function inherited by more specific name query functions.""" + for entity in entities: + if entity.name == queried_name: + return entity + return None + + def _get_entity_by_type(self, queried_type:Type, entities + )->Union[BaseMonitor,BaseHandler,BaseConductor]: + """Base function inherited by more specific type query functions.""" + for entity in entities: + if isinstance(entity, queried_type): + return entity + return None + def _is_valid_monitors(self, monitors:Union[BaseMonitor,List[BaseMonitor]])->None: """Validation check for 'monitors' variable from main constructor.""" diff --git a/tests/test_runner.py b/tests/test_runner.py index 67de0f4..c3342f6 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -1536,13 +1536,84 @@ class MeowTests(unittest.TestCase): self.assertEqual(results, good+big+small) + def testMonitorIdentification(self)->None: + monitor_one = WatchdogMonitor(TEST_MONITOR_BASE, {}, {}, name="m1") + monitor_two = WatchdogMonitor(TEST_MONITOR_BASE, {}, {}, name="m2") + monitors = [ monitor_one, monitor_two ] + + handler_one = PapermillHandler(name="h1") + handler_two = PapermillHandler(name="h2") + handlers = [ handler_one, handler_two ] + + conductor_one = LocalPythonConductor(name="c1") + conductor_two = LocalPythonConductor(name="c2") + conductors = [ conductor_one, conductor_two ] + + runner = MeowRunner(monitors, handlers, conductors) + + m1 = runner.get_monitor_by_name("m1") + self.assertEqual(monitor_one, m1) + m2 = runner.get_monitor_by_name("m2") + self.assertEqual(monitor_two, m2) + m3 = runner.get_monitor_by_name("m3") + self.assertIsNone(m3) + + mt = runner.get_monitor_by_type(WatchdogMonitor) + self.assertIn(mt, monitors) + + def testHandlerIdentification(self)->None: + monitor_one = WatchdogMonitor(TEST_MONITOR_BASE, {}, {}, name="m1") + monitor_two = WatchdogMonitor(TEST_MONITOR_BASE, {}, {}, name="m2") + monitors = [ monitor_one, monitor_two ] + + handler_one = PapermillHandler(name="h1") + handler_two = PapermillHandler(name="h2") + handlers = [ handler_one, handler_two ] + + conductor_one = LocalPythonConductor(name="c1") + conductor_two = LocalPythonConductor(name="c2") + conductors = [ conductor_one, conductor_two ] + + runner = MeowRunner(monitors, handlers, conductors) + + h1 = runner.get_handler_by_name("h1") + self.assertEqual(handler_one, h1) + h2 = runner.get_handler_by_name("h2") + self.assertEqual(handler_two, h2) + h3 = runner.get_handler_by_name("h3") + self.assertIsNone(h3) + + mt = runner.get_handler_by_type(PapermillHandler) + self.assertIn(mt, handlers) + mn = runner.get_handler_by_type(PythonHandler) + self.assertIsNone(mn) + + def testConductorIdentification(self)->None: + monitor_one = WatchdogMonitor(TEST_MONITOR_BASE, {}, {}, name="m1") + monitor_two = WatchdogMonitor(TEST_MONITOR_BASE, {}, {}, name="m2") + monitors = [ monitor_one, monitor_two ] + + handler_one = PapermillHandler(name="h1") + handler_two = PapermillHandler(name="h2") + handlers = [ handler_one, handler_two ] + + conductor_one = LocalPythonConductor(name="c1") + conductor_two = LocalPythonConductor(name="c2") + conductors = [ conductor_one, conductor_two ] + + runner = MeowRunner(monitors, handlers, conductors) + + c1 = runner.get_conductor_by_name("c1") + self.assertEqual(conductor_one, c1) + c2 = runner.get_conductor_by_name("c2") + self.assertEqual(conductor_two, c2) + c3 = runner.get_conductor_by_name("c3") + self.assertIsNone(c3) + + ct = runner.get_conductor_by_type(LocalPythonConductor) + self.assertIn(ct, conductors) + # TODO test getting job cannot handle # TODO test getting event cannot handle - # TODO test with several matched monitors - # TODO test with several mismatched monitors - # TODO test with several matched handlers - # TODO test with several mismatched handlers - # TODO test with several matched conductors - # TODO test with several mismatched conductors # TODO tests runner job queue dir # TODO tests runner job output dir