WorkflowExecutor { public void terminateWorkflow(String workflowId, String reason) { Workflow workflow = executionDAOFacade.getWorkflowById(workflowId, true); if (WorkflowStatus.COMPLETED.equals(workflow.getStatus())) { throw new ApplicationException(CONFLICT, "Cannot terminate a COMPLETED workflow."); } workflow.setStatus(WorkflowStatus.TERMINATED); terminateWorkflow(workflow, reason, null); } @Inject  WorkflowExecutor(
        DeciderService deciderService,
        MetadataDAO metadataDAO,
        QueueDAO queueDAO,
        MetadataMapperService metadataMapperService,
        WorkflowStatusListener workflowStatusListener,
        ExecutionDAOFacade executionDAOFacade,
        Configuration config,
        ExecutionLockService executionLockService,
        ParametersUtils parametersUtils
    ); String startWorkflow(String name, Integer version, String correlationId, Map<String, Object> input, String externalInputPayloadStoragePath); String startWorkflow(String name, Integer version, String correlationId, Integer priority, Map<String, Object> input, String externalInputPayloadStoragePath); String startWorkflow(String name, Integer version, String correlationId, Map<String, Object> input, String externalInputPayloadStoragePath, String event); String startWorkflow(String name, Integer version, String correlationId, Integer priority, Map<String, Object> input, String externalInputPayloadStoragePath, String event); String startWorkflow(
            String name,
            Integer version,
            String correlationId,
            Map<String, Object> input,
            String externalInputPayloadStoragePath,
            String event,
            Map<String, String> taskToDomain
    ); String startWorkflow(
            String name,
            Integer version,
            String correlationId,
            Integer priority,
            Map<String, Object> input,
            String externalInputPayloadStoragePath,
            String event,
            Map<String, String> taskToDomain
    ); String startWorkflow(
            String name,
            Integer version,
            Map<String, Object> input,
            String externalInputPayloadStoragePath,
            String correlationId,
            String parentWorkflowId,
            String parentWorkflowTaskId,
            String event
    ); String startWorkflow(
            WorkflowDef workflowDefinition,
            Map<String, Object> workflowInput,
            String externalInputPayloadStoragePath,
            String correlationId,
            String event,
            Map<String, String> taskToDomain
    ); String startWorkflow(
            WorkflowDef workflowDefinition,
            Map<String, Object> workflowInput,
            String externalInputPayloadStoragePath,
            String correlationId,
            Integer priority,
            String event,
            Map<String, String> taskToDomain
    ); String startWorkflow(
            String name,
            Integer version,
            Map<String, Object> workflowInput,
            String externalInputPayloadStoragePath,
            String correlationId,
            String parentWorkflowId,
            String parentWorkflowTaskId,
            String event,
            Map<String, String> taskToDomain
    ); String startWorkflow(
            String name,
            Integer version,
            Map<String, Object> workflowInput,
            String externalInputPayloadStoragePath,
            String correlationId,
            Integer priority,
            String parentWorkflowId,
            String parentWorkflowTaskId,
            String event,
            Map<String, String> taskToDomain
    ); String startWorkflow(
            WorkflowDef workflowDefinition,
            Map<String, Object> workflowInput,
            String externalInputPayloadStoragePath,
            String correlationId,
            Integer priority,
            String parentWorkflowId,
            String parentWorkflowTaskId,
            String event,
            Map<String, String> taskToDomain
    ); void resetCallbacksForWorkflow(String workflowId); String rerun(RerunWorkflowRequest request); void rewind(String workflowId, boolean useLatestDefinitions); void retry(String workflowId); Task getPendingTaskByWorkflow(String taskReferenceName, String workflowId); void terminateWorkflow(String workflowId, String reason); void terminateWorkflow(Workflow workflow, String reason, String failureWorkflow); void updateTask(TaskResult taskResult); Task getTask(String taskId); List<Task> getTasks(String taskType, String startKey, int count); List<Workflow> getRunningWorkflows(String workflowName, int version); List<String> getWorkflows(String name, Integer version, Long startTime, Long endTime); List<String> getRunningWorkflowIds(String workflowName, int version); boolean decide(String workflowId); void pauseWorkflow(String workflowId); void resumeWorkflow(String workflowId); void skipTaskFromWorkflow(String workflowId, String taskReferenceName, SkipTaskRequest skipTaskRequest); Workflow getWorkflow(String workflowId, boolean includeTasks); void addTaskToQueue(Task task); void executeSystemTask(WorkflowSystemTask systemTask, String taskId, int callbackTime); void scheduleNextIteration(Task loopTask, Workflow workflow); TaskDef getTaskDefinition(Task task); static final String DECIDER_QUEUE; }
@Test @SuppressWarnings("unchecked") public void testQueueExceptionsIgnoredDuringTerminateWorkflow() { WorkflowDef def = new WorkflowDef(); def.setName("test"); def.setWorkflowStatusListenerEnabled(true); Workflow workflow = new Workflow(); workflow.setWorkflowDefinition(def); workflow.setWorkflowId("1"); workflow.setStatus(Workflow.WorkflowStatus.RUNNING); workflow.setOwnerApp("junit_test"); workflow.setStartTime(10L); workflow.setEndTime(100L); workflow.setOutput(Collections.EMPTY_MAP); when(executionDAOFacade.getWorkflowById(anyString(), anyBoolean())).thenReturn(workflow); AtomicInteger updateWorkflowCalledCounter = new AtomicInteger(0); doAnswer(invocation -> { updateWorkflowCalledCounter.incrementAndGet(); return null; }).when(executionDAOFacade).updateWorkflow(any()); AtomicInteger updateTasksCalledCounter = new AtomicInteger(0); doAnswer(invocation -> { updateTasksCalledCounter.incrementAndGet(); return null; }).when(executionDAOFacade).updateTasks(any()); doThrow(new RuntimeException()).when(queueDAO).remove(anyString(), anyString()); workflowExecutor.terminateWorkflow("workflowId", "reason"); assertEquals(Workflow.WorkflowStatus.TERMINATED, workflow.getStatus()); assertEquals(1, updateWorkflowCalledCounter.get()); verify(workflowStatusListener, times(1)).onWorkflowTerminated(any(Workflow.class)); }
@Test(expected = ApplicationException.class) public void testTerminateWorkflow() { Workflow workflow = new Workflow(); workflow.setWorkflowId("testTerminateTerminalWorkflow"); workflow.setStatus(Workflow.WorkflowStatus.COMPLETED); when(executionDAOFacade.getWorkflowById(anyString(), anyBoolean())).thenReturn(workflow); workflowExecutor.terminateWorkflow(workflow.getWorkflowId(), "test terminating terminal workflow"); }