3535import com .netflix .conductor .model .TaskModel ;
3636import com .netflix .conductor .model .TaskModel .Status ;
3737import com .netflix .conductor .model .WorkflowModel ;
38+ import com .netflix .conductor .service .ExecutionLockService ;
3839
3940import static com .netflix .conductor .core .config .SchedulerConfiguration .SWEEPER_EXECUTOR_NAME ;
4041import static com .netflix .conductor .core .utils .Utils .DECIDER_QUEUE ;
@@ -49,6 +50,7 @@ public class WorkflowSweeper {
4950 private final WorkflowRepairService workflowRepairService ;
5051 private final QueueDAO queueDAO ;
5152 private final ExecutionDAOFacade executionDAOFacade ;
53+ private final ExecutionLockService executionLockService ;
5254
5355 private static final String CLASS_NAME = WorkflowSweeper .class .getSimpleName ();
5456
@@ -57,12 +59,14 @@ public WorkflowSweeper(
5759 Optional <WorkflowRepairService > workflowRepairService ,
5860 ConductorProperties properties ,
5961 QueueDAO queueDAO ,
60- ExecutionDAOFacade executionDAOFacade ) {
62+ ExecutionDAOFacade executionDAOFacade ,
63+ ExecutionLockService executionLockService ) {
6164 this .properties = properties ;
6265 this .queueDAO = queueDAO ;
6366 this .workflowExecutor = workflowExecutor ;
6467 this .executionDAOFacade = executionDAOFacade ;
6568 this .workflowRepairService = workflowRepairService .orElse (null );
69+ this .executionLockService = executionLockService ;
6670 LOGGER .info ("WorkflowSweeper initialized." );
6771 }
6872
@@ -73,25 +77,26 @@ public CompletableFuture<Void> sweepAsync(String workflowId) {
7377 }
7478
7579 public void sweep (String workflowId ) {
80+ WorkflowContext workflowContext = new WorkflowContext (properties .getAppId ());
81+ WorkflowContext .set (workflowContext );
7682 WorkflowModel workflow = null ;
7783 try {
78- WorkflowContext workflowContext = new WorkflowContext (properties .getAppId ());
79- WorkflowContext .set (workflowContext );
80- LOGGER .debug ("Running sweeper for workflow {}" , workflowId );
81-
84+ if (!executionLockService .acquireLock (workflowId )) {
85+ return ;
86+ }
8287 workflow = executionDAOFacade .getWorkflowModel (workflowId , true );
83-
88+ LOGGER . debug ( "Running sweeper for workflow {}" , workflowId );
8489 if (workflowRepairService != null ) {
8590 // Verify and repair tasks in the workflow.
8691 workflowRepairService .verifyAndRepairWorkflowTasks (workflow );
8792 }
88-
89- workflow = workflowExecutor .decideWithLock (workflow );
93+ long decideStartTime = System .currentTimeMillis ();
94+ workflow = workflowExecutor .decide (workflow .getWorkflowId ());
95+ Monitors .recordWorkflowDecisionTime (System .currentTimeMillis () - decideStartTime );
9096 if (workflow != null && workflow .getStatus ().isTerminal ()) {
9197 queueDAO .remove (DECIDER_QUEUE , workflowId );
9298 return ;
9399 }
94-
95100 } catch (NotFoundException nfe ) {
96101 queueDAO .remove (DECIDER_QUEUE , workflowId );
97102 LOGGER .info (
@@ -100,6 +105,8 @@ public void sweep(String workflowId) {
100105 } catch (Exception e ) {
101106 Monitors .error (CLASS_NAME , "sweep" );
102107 LOGGER .error ("Error running sweep for " + workflowId , e );
108+ } finally {
109+ executionLockService .releaseLock (workflowId );
103110 }
104111 long workflowOffsetTimeout =
105112 workflowOffsetWithJitter (properties .getWorkflowOffsetTimeout ().getSeconds ());
0 commit comments