def _run_task_inner_function(task: Task, job: Job) -> Tuple[Job, bool]: task_succeeded = False try: job.variables = task.inner_function(**job.variables) task_succeeded = True except Exception as e: logger.debug(f"Failed job: {job}. Error: {e}.") task.exception_handler(e, job) finally: return job, task_succeeded
def _add_decorators_to_task(self, task: Task, before: List[TaskDecorator], after: List[TaskDecorator]) -> Task: before_decorators = self._before.copy() before_decorators.extend(before) after.extend(self._after) task.before(*before_decorators) task.after(*after) return task
def test_add_after_plus_constructor(): def constructor_decorator(x): return x + 1 def function_decorator(x): return x base_decorator = Task(task_type=str(uuid.uuid4()), task_handler=lambda x: x, exception_handler=lambda x: x, after=[constructor_decorator]) base_decorator.after(function_decorator) assert len(base_decorator._after) == 2 assert base_decorator._after == [constructor_decorator, function_decorator]
def test_remove_task_from_many(task_handler, task): task_handler.tasks.append(task) for i in range(0, randint(0, 100)): task_handler.tasks.append(Task(str(uuid4()), lambda x: x, lambda x: x)) assert task_handler.remove_task(task.type) is not None assert task not in task_handler.tasks
def create_random_task_and_activate(grpc_servicer, task_type: str = None) -> str: if task_type: mock_task_type = task_type else: mock_task_type = str(uuid4()) task = Task(task_type=mock_task_type, task_handler=lambda x: x, exception_handler=lambda x: x) job = random_job(task) grpc_servicer.active_jobs[job.key] = job return mock_task_type
def wrapper(fn: Callable[..., Dict]): nonlocal variables_to_fetch if not variables_to_fetch: variables_to_fetch = self._get_parameters_from_function(fn) task = Task(task_type=task_type, task_handler=fn, exception_handler=exception_handler, timeout=timeout, max_jobs_to_activate=max_jobs_to_activate, before=before, after=after, variables_to_fetch=variables_to_fetch) self._add_task(task) return fn
def random_job(task: Task = Task(task_type="test", task_handler=lambda x: {"x": x}, exception_handler=lambda x, y, z: x), zeebe_adapter: ZeebeAdapter = None) -> Job: return Job(_type=task.type, key=randint(0, RANDOM_RANGE), worker=str(uuid4()), retries=randint(0, 10), workflow_instance_key=randint(0, RANDOM_RANGE), bpmn_process_id=str(uuid4()), workflow_definition_version=randint(0, 100), workflow_key=randint(0, RANDOM_RANGE), element_id=str(uuid4()), element_instance_key=randint(0, RANDOM_RANGE), variables={}, custom_headers={}, deadline=randint(0, RANDOM_RANGE), zeebe_adapter=zeebe_adapter)
def wrapper(fn: Callable[..., Union[str, bool, int, List]]): nonlocal variables_to_fetch if not variables_to_fetch: variables_to_fetch = self._get_parameters_from_function(fn) dict_fn = self._single_value_function_to_dict( variable_name=variable_name, fn=fn) task = Task(task_type=task_type, task_handler=dict_fn, exception_handler=exception_handler, timeout=timeout, max_jobs_to_activate=max_jobs_to_activate, before=before, after=after, variables_to_fetch=variables_to_fetch) self._add_task(task) return fn
def task(task_type): return Task(task_type, MagicMock(wraps=lambda x: dict(x=x)), MagicMock(wraps=lambda x, y, z: x))
def build_task(task_function: Function, task_config: TaskConfig) -> Task: task_config.job_parameter_name = get_job_parameter_name(task_function) return Task(task_function, build_job_handler(task_function, task_config), task_config)
def _add_task(self, task: Task) -> None: self._is_task_duplicate(task.type) task.handler = self._create_task_handler(task) self.tasks.append(task)
def test_add_before(): base_decorator = Task(task_type=str(uuid.uuid4()), task_handler=lambda x: x, exception_handler=lambda x: x) base_decorator.before(lambda x: x) assert len(base_decorator._before) == 1
def mock_job_handler(task: Task): task.job_handler = AsyncMock()
def _create_task(self, task_type: str, task_handler: Callable, exception_handler: ExceptionHandler, timeout: int = 10000, max_jobs_to_activate: int = 32, before: List[TaskDecorator] = None, after: List[TaskDecorator] = None, variables_to_fetch: List[str] = None) -> Task: task = Task(task_type=task_type, task_handler=task_handler, exception_handler=exception_handler, timeout=timeout, max_jobs_to_activate=max_jobs_to_activate, variables_to_fetch=variables_to_fetch) return self._add_decorators_to_task(task, before or [], after or [])