def test_dynamic_handler_registration(self): builtin_count = len(TaskHandler.list_all()) self.assertGreaterEqual( builtin_count, 1, 'There should be at least single built in tasks defined.' ) register_1() register_2() self.assertEqual( len(TaskHandler.list_all()), builtin_count + 2, 'There should be more than two new tasks defined.' )
def run(self): """The "main function" of the agent, looping the claim & execute tasks flow.""" with Executor(self.max_tasks) as executor: while self._run: wait_time = self.default_wait min_wait_time = 1 # request for tasks try: task_response = self.afm.request_tasks( agent_id=self.uuid, agent_name=self.name, agent_time=timeutil.format(timeutil.now()), agent_capabilities=TaskHandler.list_all(), max_tasks=executor.available_executors(), ) if "tasks" in task_response: for task_data in task_response["tasks"]: executor.submit_task( task_data, lambda *args, **kwargs: self.afm.post_result(*args, **kwargs) ) if "return_time" in task_response: return_time = timeutil.parse(task_response["return_time"]) wait_time = max(min_wait_time, (return_time - timeutil.now()).total_seconds()) except TemporaryError as e: logging.getLogger("Agent").error("An error occurred while claiming tasks: %s", e) time.sleep(wait_time)