Search in sources :

Example 1 with WorkflowContext

use of com.netflix.conductor.core.WorkflowContext in project conductor by Netflix.

the class WorkflowSweeper method sweep.

public void sweep(List<String> workflowIds, WorkflowExecutor workflowExecutor, WorkflowRepairService workflowRepairService) throws Exception {
    List<Future<?>> futures = new LinkedList<>();
    for (String workflowId : workflowIds) {
        Future<?> future = executorService.submit(() -> {
            try {
                WorkflowContext workflowContext = new WorkflowContext(config.getAppId());
                WorkflowContext.set(workflowContext);
                if (logger.isDebugEnabled()) {
                    logger.debug("Running sweeper for workflow {}", workflowId);
                }
                if (config.isWorkflowRepairServiceEnabled()) {
                    // Verify and repair tasks in the workflow.
                    workflowRepairService.verifyAndRepairWorkflowTasks(workflowId);
                }
                boolean done = workflowExecutor.decide(workflowId);
                if (!done) {
                    queueDAO.setUnackTimeout(WorkflowExecutor.DECIDER_QUEUE, workflowId, config.getSweepFrequency() * 1000);
                } else {
                    queueDAO.remove(WorkflowExecutor.DECIDER_QUEUE, workflowId);
                }
            } catch (ApplicationException e) {
                if (e.getCode().equals(Code.NOT_FOUND)) {
                    logger.error("Workflow NOT found for id: " + workflowId, e);
                    queueDAO.remove(WorkflowExecutor.DECIDER_QUEUE, workflowId);
                }
            } catch (Exception e) {
                queueDAO.setUnackTimeout(WorkflowExecutor.DECIDER_QUEUE, workflowId, config.getSweepFrequency() * 1000);
                Monitors.error(className, "sweep");
                logger.error("Error running sweep for " + workflowId, e);
            }
        });
        futures.add(future);
    }
    for (Future<?> future : futures) {
        future.get();
    }
}
Also used : WorkflowContext(com.netflix.conductor.core.WorkflowContext) Future(java.util.concurrent.Future) LinkedList(java.util.LinkedList)

Example 2 with WorkflowContext

use of com.netflix.conductor.core.WorkflowContext in project conductor by Netflix.

the class AbstractWorkflowServiceTest method init.

@Before
public void init() {
    System.setProperty("EC2_REGION", "us-east-1");
    System.setProperty("EC2_AVAILABILITY_ZONE", "us-east-1c");
    if (registered) {
        return;
    }
    WorkflowContext.set(new WorkflowContext("junit_app"));
    for (int i = 0; i < 21; i++) {
        String name = "junit_task_" + i;
        if (notFoundSafeGetTaskDef(name) != null) {
            continue;
        }
        TaskDef task = new TaskDef();
        task.setName(name);
        task.setTimeoutSeconds(120);
        task.setRetryCount(RETRY_COUNT);
        metadataService.registerTaskDef(Collections.singletonList(task));
    }
    for (int i = 0; i < 5; i++) {
        String name = "junit_task_0_RT_" + i;
        if (notFoundSafeGetTaskDef(name) != null) {
            continue;
        }
        TaskDef task = new TaskDef();
        task.setName(name);
        task.setTimeoutSeconds(120);
        task.setRetryCount(0);
        metadataService.registerTaskDef(Collections.singletonList(task));
    }
    TaskDef task = new TaskDef();
    task.setName("short_time_out");
    task.setTimeoutSeconds(5);
    task.setRetryCount(RETRY_COUNT);
    metadataService.registerTaskDef(Collections.singletonList(task));
    WorkflowDef def = new WorkflowDef();
    def.setName(LINEAR_WORKFLOW_T1_T2);
    def.setDescription(def.getName());
    def.setVersion(1);
    def.setInputParameters(Arrays.asList("param1", "param2"));
    Map<String, Object> outputParameters = new HashMap<>();
    outputParameters.put("o1", "${workflow.input.param1}");
    outputParameters.put("o2", "${t2.output.uuid}");
    outputParameters.put("o3", "${t1.output.op}");
    def.setOutputParameters(outputParameters);
    def.setFailureWorkflow("$workflow.input.failureWfName");
    def.setSchemaVersion(2);
    LinkedList<WorkflowTask> wftasks = new LinkedList<>();
    WorkflowTask wft1 = new WorkflowTask();
    wft1.setName("junit_task_1");
    Map<String, Object> ip1 = new HashMap<>();
    ip1.put("p1", "${workflow.input.param1}");
    ip1.put("p2", "${workflow.input.param2}");
    ip1.put("someNullKey", null);
    wft1.setInputParameters(ip1);
    wft1.setTaskReferenceName("t1");
    WorkflowTask wft2 = new WorkflowTask();
    wft2.setName("junit_task_2");
    Map<String, Object> ip2 = new HashMap<>();
    ip2.put("tp1", "${workflow.input.param1}");
    ip2.put("tp2", "${t1.output.op}");
    wft2.setInputParameters(ip2);
    wft2.setTaskReferenceName("t2");
    wftasks.add(wft1);
    wftasks.add(wft2);
    def.setTasks(wftasks);
    WorkflowTask wft3 = new WorkflowTask();
    wft3.setName("junit_task_3");
    Map<String, Object> ip3 = new HashMap<>();
    ip3.put("tp1", "${workflow.input.param1}");
    ip3.put("tp2", "${t1.output.op}");
    wft3.setInputParameters(ip3);
    wft3.setTaskReferenceName("t3");
    WorkflowDef def2 = new WorkflowDef();
    def2.setName(TEST_WORKFLOW);
    def2.setDescription(def2.getName());
    def2.setVersion(1);
    def2.setInputParameters(Arrays.asList("param1", "param2"));
    LinkedList<WorkflowTask> wftasks2 = new LinkedList<>();
    wftasks2.add(wft1);
    wftasks2.add(wft2);
    wftasks2.add(wft3);
    def2.setSchemaVersion(2);
    def2.setTasks(wftasks2);
    try {
        WorkflowDef[] wdsf = new WorkflowDef[] { def, def2 };
        for (WorkflowDef wd : wdsf) {
            metadataService.updateWorkflowDef(wd);
        }
        createForkJoinWorkflow();
        def.setName(LONG_RUNNING);
        metadataService.updateWorkflowDef(def);
    } catch (Exception e) {
    }
    taskDefs = metadataService.getTaskDefs();
    registered = true;
}
Also used : WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) HashMap(java.util.HashMap) WorkflowContext(com.netflix.conductor.core.WorkflowContext) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) LinkedList(java.util.LinkedList) ExpectedException(org.junit.rules.ExpectedException) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) TaskDef(com.netflix.conductor.common.metadata.tasks.TaskDef) Before(org.junit.Before)

Aggregations

WorkflowContext (com.netflix.conductor.core.WorkflowContext)2 LinkedList (java.util.LinkedList)2 TaskDef (com.netflix.conductor.common.metadata.tasks.TaskDef)1 WorkflowDef (com.netflix.conductor.common.metadata.workflow.WorkflowDef)1 WorkflowTask (com.netflix.conductor.common.metadata.workflow.WorkflowTask)1 ApplicationException (com.netflix.conductor.core.execution.ApplicationException)1 HashMap (java.util.HashMap)1 Future (java.util.concurrent.Future)1 Before (org.junit.Before)1 ExpectedException (org.junit.rules.ExpectedException)1