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)
def __call__(self, *args, **kwargs): if self.called: raise FatalError('Exit agent') self.called = True return { 'return_time': timeutil.format(timeutil.now()), 'tasks': [{ 'task_type': 'test', 'task_version': 1, 'task_id': uuid.uuid4(), 'task_data': {} }] }