public List<Workflow> getWorkflowInstances(String workflowName, String correlationId, boolean includeClosed, boolean includeTasks) { List<Workflow> workflows = executionDAOFacade.getWorkflowsByCorrelationId(correlationId, includeTasks); List<Workflow> result = new LinkedList<>(); for (Workflow wf : workflows) { if (wf.getWorkflowName().equals(workflowName) && (includeClosed || wf.getStatus().equals(Workflow.WorkflowStatus.RUNNING))) { result.add(wf); } } return result; }
@Override public void cancel(Workflow workflow, Task task, WorkflowExecutor provider) { String workflowId = (String) task.getOutputData().get(SUB_WORKFLOW_ID); if(workflowId == null) { workflowId = (String) task.getInputData().get(SUB_WORKFLOW_ID); //Backward compatibility } if(StringUtils.isEmpty(workflowId)) { return; } Workflow subWorkflow = provider.getWorkflow(workflowId, false); subWorkflow.setStatus(WorkflowStatus.TERMINATED); provider.terminateWorkflow(subWorkflow, "Parent workflow has been terminated with status " + workflow.getStatus(), null); }
/** * @param workflowId * @throws IllegalStateException */ public void resumeWorkflow(String workflowId) { Workflow workflow = executionDAOFacade.getWorkflowById(workflowId, false); if (!workflow.getStatus().equals(WorkflowStatus.PAUSED)) { throw new IllegalStateException("The workflow " + workflowId + " is not PAUSED so cannot resume. " + "Current status is " + workflow.getStatus().name()); } workflow.setStatus(WorkflowStatus.RUNNING); executionDAOFacade.updateWorkflow(workflow); decide(workflowId); }
/** * @throws ApplicationException */ public void pauseWorkflow(String workflowId) { WorkflowStatus status = WorkflowStatus.PAUSED; Workflow workflow = executionDAOFacade.getWorkflowById(workflowId, false); if (workflow.getStatus().isTerminal()) { throw new ApplicationException(CONFLICT, "Workflow id " + workflowId + " has ended, status cannot be updated."); } if (workflow.getStatus().equals(status)) { return; //Already paused! } workflow.setStatus(status); executionDAOFacade.updateWorkflow(workflow); }
private void terminate(final Workflow workflow, TerminateWorkflowException tw) { if (!workflow.getStatus().isTerminal()) { workflow.setStatus(tw.workflowStatus); } String failureWorkflow = workflow.getWorkflowDefinition().getFailureWorkflow(); if (failureWorkflow != null) { if (failureWorkflow.startsWith("$")) { String[] paramPathComponents = failureWorkflow.split("\\."); String name = paramPathComponents[2]; // name of the input parameter failureWorkflow = (String) workflow.getInput().get(name); } } if (tw.task != null) { executionDAOFacade.updateTask(tw.task); } terminateWorkflow(workflow, tw.getMessage(), failureWorkflow); }
this.updateTime = sdf.format(new Date(workflow.getUpdateTime())); this.status = workflow.getStatus(); if(workflow.getInput() != null){ this.input = workflow.getInput().toString();
@Override public String updateWorkflow(Workflow workflow) { try { workflow.setUpdateTime(System.currentTimeMillis()); if (workflow.getStatus().isTerminal()) { workflow.setEndTime(System.currentTimeMillis()); } List<Task> tasks = workflow.getTasks(); workflow.setTasks(new LinkedList<>()); String payload = toJson(workflow); recordCassandraDaoRequests("createWorkflow", "n/a", workflow.getWorkflowName()); recordCassandraDaoPayloadSize("createWorkflow", payload.length(), "n/a", workflow.getWorkflowName()); session.execute(updateWorkflowStatement.bind(payload, UUID.fromString(workflow.getWorkflowId()))); workflow.setTasks(tasks); return workflow.getWorkflowId(); } catch (Exception e) { Monitors.error(CLASS_NAME, "updateWorkflow"); String errorMsg = String.format("Failed to update workflow: %s", workflow.getWorkflowId()); LOGGER.error(errorMsg, e); throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, errorMsg); } }
@Override public boolean execute(Workflow workflow, Task task, WorkflowExecutor provider) { String workflowId = (String) task.getOutputData().get(SUB_WORKFLOW_ID); if (workflowId == null) { workflowId = (String) task.getInputData().get(SUB_WORKFLOW_ID); //Backward compatibility } if(StringUtils.isEmpty(workflowId)) { return false; } Workflow subWorkflow = provider.getWorkflow(workflowId, false); WorkflowStatus subWorkflowStatus = subWorkflow.getStatus(); if(!subWorkflowStatus.isTerminal()){ return false; } task.getOutputData().putAll(subWorkflow.getOutput()); if (subWorkflowStatus.isSuccessful()) { task.setStatus(Status.COMPLETED); } else { task.setReasonForIncompletion(subWorkflow.getReasonForIncompletion()); task.setStatus(Status.FAILED); } return true; }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Workflow workflow = (Workflow) o; return getEndTime() == workflow.getEndTime() && getWorkflowVersion() == workflow.getWorkflowVersion() && getSchemaVersion() == workflow.getSchemaVersion() && getStatus() == workflow.getStatus() && Objects.equals(getWorkflowId(), workflow.getWorkflowId()) && Objects.equals(getParentWorkflowId(), workflow.getParentWorkflowId()) && Objects.equals(getParentWorkflowTaskId(), workflow.getParentWorkflowTaskId()) && Objects.equals(getTasks(), workflow.getTasks()) && Objects.equals(getInput(), workflow.getInput()) && Objects.equals(getOutput(), workflow.getOutput()) && Objects.equals(getWorkflowName(), workflow.getWorkflowName()) && Objects.equals(getCorrelationId(), workflow.getCorrelationId()) && Objects.equals(getReRunFromWorkflowId(), workflow.getReRunFromWorkflowId()) && Objects.equals(getReasonForIncompletion(), workflow.getReasonForIncompletion()) && Objects.equals(getEvent(), workflow.getEvent()) && Objects.equals(getTaskToDomain(), workflow.getTaskToDomain()) && Objects.equals(getFailedReferenceTaskNames(), workflow.getFailedReferenceTaskNames()) && Objects.equals(getExternalInputPayloadStoragePath(), workflow.getExternalInputPayloadStoragePath()) && Objects.equals(getExternalOutputPayloadStoragePath(), workflow.getExternalOutputPayloadStoragePath()) && Objects.equals(getWorkflowDefinition(), workflow.getWorkflowDefinition()); }
private void printTaskStatuses(Workflow wf, String message) { if (printWFTaskDetails) { System.out.println(message + " >>> Workflow status " + wf.getStatus().name()); wf.getTasks().forEach(t -> { System.out.println("Task " + String.format("%-15s", t.getTaskType()) + "\t" + String.format("%-15s", t.getReferenceTaskName()) + "\t" + String.format("%-15s", t.getWorkflowTask().getType()) + "\t" + t.getSeq() + "\t" + t.getStatus() + "\t" + t.getTaskId()); }); System.out.println(); } } }
/** * @param workflowId the id of the workflow for which callbacks are to be reset * @throws ApplicationException if the workflow is in terminal state */ public void resetCallbacksForInProgressTasks(String workflowId) { Workflow workflow = executionDAOFacade.getWorkflowById(workflowId, true); if (workflow.getStatus().isTerminal()) { throw new ApplicationException(CONFLICT, "Workflow is in terminal state. Status =" + workflow.getStatus()); } // Get tasks that are in progress and have callbackAfterSeconds > 0 // and set the callbackAfterSeconds to 0; for (Task task : workflow.getTasks()) { if (task.getStatus().equals(IN_PROGRESS) && task.getCallbackAfterSeconds() > 0) { if (queueDAO.setOffsetTime(QueueUtils.getQueueName(task), task.getTaskId(), 0)) { task.setCallbackAfterSeconds(0); executionDAOFacade.updateTask(task); } } } }
private String insertOrUpdateWorkflow(Workflow workflow, boolean update) { Preconditions.checkNotNull(workflow, "workflow object cannot be null"); boolean terminal = workflow.getStatus().isTerminal(); if (terminal) { workflow.setEndTime(System.currentTimeMillis()); } List<Task> tasks = workflow.getTasks(); workflow.setTasks(Lists.newLinkedList()); withTransaction(tx -> { if (!update) { addWorkflow(tx, workflow); addWorkflowDefToWorkflowMapping(tx, workflow); } else { updateWorkflow(tx, workflow); } if (terminal) { removePendingWorkflow(tx, workflow.getWorkflowName(), workflow.getWorkflowId()); } else { addPendingWorkflow(tx, workflow.getWorkflowName(), workflow.getWorkflowId()); } }); workflow.setTasks(tasks); return workflow.getWorkflowId(); }
Preconditions.checkNotNull(workflow, "workflow object cannot be null"); if (workflow.getStatus().isTerminal()) { workflow.setEndTime(System.currentTimeMillis()); if (workflow.getStatus().isTerminal()) { dynoClient.srem(nsKey(PENDING_WORKFLOWS, workflow.getWorkflowName()), workflow.getWorkflowId()); } else {
@Test public void testWorkflowWithNoTasks() { WorkflowDef empty = new WorkflowDef(); empty.setName("empty_workflow"); empty.setSchemaVersion(2); metadataService.registerWorkflowDef(empty); String id = startOrLoadWorkflowExecution(empty.getName(), 1, "testWorkflowWithNoTasks", new HashMap<>(), null, null); assertNotNull(id); Workflow workflow = workflowExecutionService.getExecutionStatus(id, true); assertNotNull(workflow); assertEquals(WorkflowStatus.COMPLETED, workflow.getStatus()); assertEquals(0, workflow.getTasks().size()); }
@Test public void testFailures() { metadataService.getWorkflowDef(FORK_JOIN_WF, 1); String taskName = "junit_task_1"; TaskDef taskDef = notFoundSafeGetTaskDef(taskName); taskDef.setRetryCount(0); metadataService.updateTaskDef(taskDef); WorkflowDef found = metadataService.getWorkflowDef(LINEAR_WORKFLOW_T1_T2, 1); assertNotNull(found.getFailureWorkflow()); assertFalse(StringUtils.isBlank(found.getFailureWorkflow())); String correlationId = "unit_test_1" + UUID.randomUUID().toString(); Map<String, Object> input = new HashMap<String, Object>(); String inputParam1 = "p1 value"; input.put("param1", inputParam1); input.put("param2", "p2 value"); input.put("failureWfName", "FanInOutTest"); String wfid = startOrLoadWorkflowExecution(LINEAR_WORKFLOW_T1_T2, 1, correlationId, input, null, null); assertNotNull(wfid); Task task = getTask("junit_task_1"); assertNotNull(task); task.setStatus(FAILED); workflowExecutionService.updateTask(task); // If we get the full workflow here then, last task should be completed and the next task should be scheduled Workflow es = workflowExecutionService.getExecutionStatus(wfid, true); assertNotNull(es); assertEquals(WorkflowStatus.FAILED, es.getStatus()); taskDef.setRetryCount(RETRY_COUNT); metadataService.updateTaskDef(taskDef); }
@Override public int hashCode() { return Objects.hash( getStatus(), getEndTime(), getWorkflowId(), getParentWorkflowId(), getParentWorkflowTaskId(), getTasks(), getInput(), getOutput(), getWorkflowName(), getWorkflowVersion(), getCorrelationId(), getReRunFromWorkflowId(), getReasonForIncompletion(), getSchemaVersion(), getEvent(), getTaskToDomain(), getFailedReferenceTaskNames(), getWorkflowDefinition(), getExternalInputPayloadStoragePath(), getExternalOutputPayloadStoragePath() ); } }
private void verify(String inputParam1, String wfid, String task1Op, boolean fail) { Task task = workflowExecutionService.poll("junit_task_2", "task2.junit.worker"); assertNotNull(task); assertTrue(workflowExecutionService.ackTaskReceived(task.getTaskId())); String task2Input = (String) task.getInputData().get("tp2"); assertNotNull(task2Input); assertEquals(task1Op, task2Input); task2Input = (String) task.getInputData().get("tp1"); assertNotNull(task2Input); assertEquals(inputParam1, task2Input); if (fail) { task.setStatus(FAILED); task.setReasonForIncompletion("failure...0"); } else { task.setStatus(COMPLETED); } workflowExecutionService.updateTask(task); Workflow es = workflowExecutionService.getExecutionStatus(wfid, false); assertNotNull(es); if (fail) { assertEquals(RUNNING, es.getStatus()); } else { assertEquals(WorkflowStatus.COMPLETED, es.getStatus()); } }