Skip to content

Commit

Permalink
move unfinished children check code to state execution
Browse files Browse the repository at this point in the history
  • Loading branch information
efonsell committed Feb 18, 2021
1 parent daf0a69 commit 8ac5678
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package io.nflow.engine.internal.workflow;

import static java.util.Collections.unmodifiableList;
import static java.util.EnumSet.complementOf;
import static org.joda.time.DateTime.now;
import static org.slf4j.LoggerFactory.getLogger;
import static org.springframework.util.Assert.notNull;

import java.util.Arrays;
import java.util.EnumSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
Expand All @@ -22,11 +24,14 @@
import io.nflow.engine.workflow.definition.WorkflowState;
import io.nflow.engine.workflow.instance.QueryWorkflowInstances;
import io.nflow.engine.workflow.instance.WorkflowInstance;
import io.nflow.engine.workflow.instance.WorkflowInstance.WorkflowInstanceStatus;
import io.nflow.engine.workflow.instance.WorkflowInstanceAction.WorkflowActionType;

public class StateExecutionImpl extends ModelObject implements StateExecution {

private static final Logger LOG = getLogger(StateExecutionImpl.class);
private static final WorkflowInstanceStatus[] UNFINISHED_STATUSES = complementOf(EnumSet.of(WorkflowInstanceStatus.finished))
.toArray(new WorkflowInstanceStatus[0]);
private final WorkflowInstance instance;
private final ObjectStringMapper objectMapper;
private final WorkflowInstanceDao workflowDao;
Expand Down Expand Up @@ -309,4 +314,10 @@ public void handleFailure(AbstractWorkflowDefinition<?> definition, String failu
}
}

@Override
public boolean hasUnfinishedChildren() {
QueryWorkflowInstances unfinishedChildren = new QueryWorkflowInstances.Builder().addStatuses(UNFINISHED_STATUSES)
.setParentActionId(instance.id).build();
return !workflowDao.queryWorkflowInstances(unfinishedChildren).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import io.nflow.engine.workflow.definition.WorkflowSettings;
import io.nflow.engine.workflow.definition.WorkflowSettings.Builder;
import io.nflow.engine.workflow.definition.WorkflowStateType;
import io.nflow.engine.workflow.instance.WorkflowInstance.WorkflowInstanceStatus;

/**
* Workflow that wakes up periodically to execute a task.
Expand Down Expand Up @@ -194,7 +193,7 @@ public NextAction waitForWorkToFinish(StateExecution execution) {
* @return Time when check should be retried. Null to go to schedule state immediately.
*/
protected DateTime waitForWorkToFinishImpl(StateExecution execution) {
if (execution.getAllChildWorkflows().stream().anyMatch(child -> child.status != WorkflowInstanceStatus.finished)) {
if (execution.hasUnfinishedChildren()) {
logger.info("Unfinished child workflow found, waiting before scheduling next work.");
return now().plusHours(1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,10 @@ public interface StateExecution {
*/
Optional<Long> getParentId();

/**
* Return true if this workflow instance has unfinished child workflow instances.
*
* @return True if unfinished child workflow instances are found, false otherwise.
*/
boolean hasUnfinishedChildren();
}

0 comments on commit 8ac5678

Please sign in to comment.