33import functools
44import inspect
55import logging
6- from collections .abc import Sequence
76from typing import Any , TypeVar
87
98from typing_extensions import ParamSpec
1413from pyzeebe .function_tools .dict_tools import convert_to_dict_function
1514from pyzeebe .function_tools .parameter_tools import get_job_parameter_name
1615from pyzeebe .job .job import JobController
17- from pyzeebe .task .exception_handler import default_exception_handler
1816from pyzeebe .task .task import Task
1917from pyzeebe .task .task_config import TaskConfig
20- from pyzeebe .task .types import AsyncTaskDecorator , DecoratorRunner , JobHandler
18+ from pyzeebe .task .types import JobHandler
2119from pyzeebe .types import Variables
2220
2321P = ParamSpec ("P" )
@@ -34,20 +32,10 @@ def build_task(task_function: Function[..., Any], task_config: TaskConfig) -> Ta
3432def build_job_handler (task_function : Function [..., Any ], task_config : TaskConfig ) -> JobHandler :
3533 prepared_task_function = prepare_task_function (task_function , task_config )
3634
37- before_decorator_runner = create_decorator_runner (task_config .before )
38- after_decorator_runner = create_decorator_runner (task_config .after )
39-
4035 @functools .wraps (task_function )
4136 async def job_handler (job : Job , job_controller : JobController ) -> Job :
42- job = await before_decorator_runner (job )
43- return_variables , succeeded = await run_original_task_function (
44- prepared_task_function , task_config , job , job_controller
45- )
37+ return_variables = await run_original_task_function (prepared_task_function , task_config , job )
4638 job .set_task_result (return_variables )
47- await job_controller .set_running_after_decorators_status ()
48- job = await after_decorator_runner (job )
49- if succeeded :
50- await job_controller .set_success_status (variables = return_variables )
5139 return job
5240
5341 return job_handler
@@ -63,38 +51,30 @@ def prepare_task_function(task_function: Function[P, R], task_config: TaskConfig
6351 return task_function # type: ignore[return-value]
6452
6553
66- async def run_original_task_function (
67- task_function : DictFunction [...], task_config : TaskConfig , job : Job , job_controller : JobController
68- ) -> tuple [Variables , bool ]:
69- try :
70- if task_config .variables_to_fetch is None :
71- variables : dict [str , Any ] = {}
72- elif task_wants_all_variables (task_config ):
73- if only_job_is_required_in_task_function (task_function ):
74- variables = {}
75- else :
76- variables = {** job .variables }
54+ async def run_original_task_function (task_function : DictFunction [...], task_config : TaskConfig , job : Job ) -> Variables :
55+ if task_config .variables_to_fetch is None :
56+ variables : dict [str , Any ] = {}
57+ elif task_wants_all_variables (task_config ):
58+ if only_job_is_required_in_task_function (task_function ):
59+ variables = {}
7760 else :
78- variables = {
79- k : v
80- for k , v in job .variables .items ()
81- if k in task_config .variables_to_fetch or k == task_config .job_parameter_name
82- }
61+ variables = {** job .variables }
62+ else :
63+ variables = {
64+ k : v
65+ for k , v in job .variables .items ()
66+ if k in task_config .variables_to_fetch or k == task_config .job_parameter_name
67+ }
8368
84- if task_config .job_parameter_name :
85- variables [task_config .job_parameter_name ] = job
69+ if task_config .job_parameter_name :
70+ variables [task_config .job_parameter_name ] = job
8671
87- returned_value = await task_function (** variables )
72+ returned_value = await task_function (** variables )
8873
89- if returned_value is None :
90- returned_value = {}
74+ if returned_value is None :
75+ returned_value = {}
9176
92- return returned_value , True
93- except Exception as e :
94- logger .debug ("Failed job: %s. Error: %s." , job , e )
95- exception_handler = task_config .exception_handler or default_exception_handler
96- await exception_handler (e , job , job_controller )
97- return job .variables , False
77+ return returned_value
9878
9979
10080def only_job_is_required_in_task_function (task_function : DictFunction [...]) -> bool :
@@ -104,20 +84,3 @@ def only_job_is_required_in_task_function(task_function: DictFunction[...]) -> b
10484
10585def task_wants_all_variables (task_config : TaskConfig ) -> bool :
10686 return task_config .variables_to_fetch == []
107-
108-
109- def create_decorator_runner (decorators : Sequence [AsyncTaskDecorator ]) -> DecoratorRunner :
110- async def decorator_runner (job : Job ) -> Job :
111- for decorator in decorators :
112- job = await run_decorator (decorator , job )
113- return job
114-
115- return decorator_runner
116-
117-
118- async def run_decorator (decorator : AsyncTaskDecorator , job : Job ) -> Job :
119- try :
120- return await decorator (job )
121- except Exception as e :
122- logger .warning ("Failed to run decorator %s. Exception: %s" , decorator , e , exc_info = True )
123- return job
0 commit comments