2828import com .bytechef .atlas .configuration .domain .Task ;
2929import com .bytechef .atlas .configuration .domain .WorkflowTask ;
3030import com .bytechef .atlas .coordinator .event .TaskExecutionCompleteEvent ;
31- import com .bytechef .atlas .coordinator .event . TaskExecutionErrorEvent ;
31+ import com .bytechef .atlas .coordinator .task . dispatcher . ErrorHandlingTaskDispatcher ;
3232import com .bytechef .atlas .coordinator .task .dispatcher .TaskDispatcher ;
3333import com .bytechef .atlas .coordinator .task .dispatcher .TaskDispatcherResolver ;
3434import com .bytechef .atlas .execution .domain .Context .Classname ;
3737import com .bytechef .atlas .execution .service .TaskExecutionService ;
3838import com .bytechef .atlas .file .storage .TaskFileStorage ;
3939import com .bytechef .commons .util .MapUtils ;
40- import com .bytechef .error .ExecutionError ;
4140import com .bytechef .evaluator .Evaluator ;
4241import com .fasterxml .jackson .core .type .TypeReference ;
4342import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
4443import java .time .Instant ;
45- import java .util .Arrays ;
4644import java .util .Collections ;
4745import java .util .List ;
4846import java .util .Map ;
4947import java .util .Objects ;
5048import org .apache .commons .lang3 .Validate ;
51- import org .apache .commons .lang3 .exception .ExceptionUtils ;
5249import org .springframework .context .ApplicationEventPublisher ;
5350
5451/**
5754 * @since Jun 3, 2017
5855 * @see BranchTaskCompletionHandler
5956 */
60- public class BranchTaskDispatcher implements TaskDispatcher < TaskExecution >, TaskDispatcherResolver {
57+ public class BranchTaskDispatcher extends ErrorHandlingTaskDispatcher implements TaskDispatcherResolver {
6158
6259 private final ContextService contextService ;
6360 private final Evaluator evaluator ;
@@ -72,6 +69,8 @@ public BranchTaskDispatcher(
7269 TaskDispatcher <? super Task > taskDispatcher , TaskExecutionService taskExecutionService ,
7370 TaskFileStorage taskFileStorage ) {
7471
72+ super (eventPublisher );
73+
7574 this .evaluator = evaluator ;
7675 this .contextService = contextService ;
7776 this .eventPublisher = eventPublisher ;
@@ -81,69 +80,64 @@ public BranchTaskDispatcher(
8180 }
8281
8382 @ Override
84- public void dispatch (TaskExecution taskExecution ) {
83+ public void doDispatch (TaskExecution taskExecution ) {
8584 taskExecution .setStartDate (Instant .now ());
8685 taskExecution .setStatus (TaskExecution .Status .STARTED );
8786
8887 taskExecution = taskExecutionService .update (taskExecution );
8988
9089 Map <String , ?> selectedCase = resolveCase (taskExecution );
9190
92- try {
93- if (selectedCase .containsKey (TASKS )) {
94- List <WorkflowTask > subWorkflowTasks = MapUtils .getList (
95- selectedCase , TASKS , WorkflowTask .class , Collections .emptyList ());
91+ if (selectedCase .containsKey (TASKS )) {
92+ List <WorkflowTask > subWorkflowTasks = MapUtils .getList (
93+ selectedCase , TASKS , WorkflowTask .class , Collections .emptyList ());
9694
97- if (subWorkflowTasks .isEmpty ()) {
98- taskExecution .setStartDate (Instant .now ());
99- taskExecution .setEndDate (Instant .now ());
100- taskExecution .setExecutionTime (0 );
95+ if (subWorkflowTasks .isEmpty ()) {
96+ taskExecution .setStartDate (Instant .now ());
97+ taskExecution .setEndDate (Instant .now ());
98+ taskExecution .setExecutionTime (0 );
10199
102- eventPublisher .publishEvent (new TaskExecutionCompleteEvent (taskExecution ));
103- } else {
104- WorkflowTask subWorkflowTask = subWorkflowTasks .get (0 );
100+ eventPublisher .publishEvent (new TaskExecutionCompleteEvent (taskExecution ));
101+ } else {
102+ WorkflowTask subWorkflowTask = subWorkflowTasks .get (0 );
105103
106- TaskExecution subTaskExecution = TaskExecution .builder ()
107- .jobId (taskExecution .getJobId ())
108- .parentId (taskExecution .getId ())
109- .priority (taskExecution .getPriority ())
110- .taskNumber (1 )
111- .workflowTask (subWorkflowTask )
112- .build ();
104+ TaskExecution subTaskExecution = TaskExecution .builder ()
105+ .jobId (taskExecution .getJobId ())
106+ .parentId (taskExecution .getId ())
107+ .priority (taskExecution .getPriority ())
108+ .taskNumber (1 )
109+ .workflowTask (subWorkflowTask )
110+ .build ();
113111
114- Map <String , ?> context = taskFileStorage .readContextValue (
115- contextService .peek (Validate .notNull (taskExecution .getId (), "id" ), Classname .TASK_EXECUTION ));
112+ Map <String , ?> context = taskFileStorage .readContextValue (
113+ contextService .peek (Validate .notNull (taskExecution .getId (), "id" ), Classname .TASK_EXECUTION ));
116114
117- subTaskExecution .evaluate (context , evaluator );
115+ subTaskExecution .evaluate (context , evaluator );
118116
119- subTaskExecution = taskExecutionService .create (subTaskExecution );
117+ subTaskExecution = taskExecutionService .create (subTaskExecution );
120118
121- contextService .push (
122- Validate .notNull (subTaskExecution .getId (), "id" ), Classname .TASK_EXECUTION ,
123- taskFileStorage .storeContextValue (
124- Validate .notNull (subTaskExecution .getId (), "id" ), Classname .TASK_EXECUTION , context ));
119+ contextService .push (
120+ Validate .notNull (subTaskExecution .getId (), "id" ), Classname .TASK_EXECUTION ,
121+ taskFileStorage .storeContextValue (
122+ Validate .notNull (subTaskExecution .getId (), "id" ), Classname .TASK_EXECUTION , context ));
125123
126- taskDispatcher .dispatch (subTaskExecution );
127- }
128- } else {
129- taskExecution .setStartDate (Instant .now ());
130- taskExecution .setEndDate (Instant .now ());
131- taskExecution .setExecutionTime (0 );
132- // TODO check, it seems wrong
133-
134- if (selectedCase .get ("value" ) != null ) {
135- taskExecution .setOutput (
136- taskFileStorage .storeTaskExecutionOutput (
137- Validate .notNull (taskExecution .getId (), "id" ), selectedCase .get ("value" )));
138- }
139-
140- eventPublisher .publishEvent (new TaskExecutionCompleteEvent (taskExecution ));
124+ taskDispatcher .dispatch (subTaskExecution );
125+ }
126+ } else {
127+ taskExecution .setStartDate (Instant .now ());
128+ taskExecution .setEndDate (Instant .now ());
129+ taskExecution .setExecutionTime (0 );
130+ // TODO check, it seems wrong
131+
132+ if (selectedCase .get ("value" ) != null ) {
133+ taskExecution .setOutput (
134+ taskFileStorage .storeTaskExecutionOutput (
135+ Validate .notNull (taskExecution .getId (), "id" ), selectedCase .get ("value" )));
141136 }
142- } catch (Exception e ) {
143- taskExecution .setError (new ExecutionError (e .getMessage (), Arrays .asList (ExceptionUtils .getStackFrames (e ))));
144137
145- eventPublisher .publishEvent (new TaskExecutionErrorEvent (taskExecution ));
138+ eventPublisher .publishEvent (new TaskExecutionCompleteEvent (taskExecution ));
146139 }
140+
147141 }
148142
149143 @ Override
0 commit comments