Search in sources :

Example 1 with TaskContext

use of org.motechproject.tasks.service.util.TaskContext in project motech by motech.

the class TaskTriggerHandler method handleTask.

private void handleTask(Task task, Map<String, Object> parameters) {
    long activityId = activityService.addTaskStarted(task, parameters);
    Map<String, Object> metadata = prepareTaskMetadata(task.getId(), activityId);
    TaskContext taskContext = new TaskContext(task, parameters, metadata, activityService);
    TaskInitializer initializer = new TaskInitializer(taskContext);
    List<FilterSet> filterSetList = new ArrayList<>(task.getTaskConfig().getFilters());
    boolean actionFilterResult = true;
    int executedAndFilteredActions = 0;
    int stepIndex = 0;
    int actualFilterIndex = initializer.getActionFilters();
    try {
        LOGGER.info("Executing all actions from task: {}", task.getName());
        if (initializer.evalConfigSteps(dataProviders)) {
            while (executedAndFilteredActions < task.getActions().size()) {
                if (isActionFilter(filterSetList, actualFilterIndex, stepIndex)) {
                    actionFilterResult = initializer.checkActionFilter(actualFilterIndex, filterSetList);
                    actualFilterIndex++;
                    stepIndex++;
                }
                if (actionFilterResult) {
                    executor.execute(task, task.getActions().get(executedAndFilteredActions), executedAndFilteredActions, taskContext, activityId);
                    executedAndFilteredActions++;
                } else {
                    activityService.addFilteredExecution(activityId);
                    executedAndFilteredActions++;
                }
                stepIndex++;
                actionFilterResult = true;
            }
        } else {
            activityService.addTaskFiltered(activityId);
            LOGGER.warn("Actions from task: {} weren't executed, because config steps didn't pass the evaluation", task.getName());
        }
    } catch (TaskHandlerException e) {
        postExecutionHandler.handleError(parameters, metadata, task, e, activityId);
    } catch (RuntimeException e) {
        postExecutionHandler.handleError(parameters, metadata, task, new TaskHandlerException(TRIGGER, "task.error.unrecognizedError", e), activityId);
    }
}
Also used : TaskContext(org.motechproject.tasks.service.util.TaskContext) FilterSet(org.motechproject.tasks.domain.mds.task.FilterSet) ArrayList(java.util.ArrayList) TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException)

Example 2 with TaskContext

use of org.motechproject.tasks.service.util.TaskContext in project motech by motech.

the class TaskActionExecutorTest method shouldRaiseEventWhenActionHasSubjectAndService_IfServiceIsNotAvailable.

@Test
public void shouldRaiseEventWhenActionHasSubjectAndService_IfServiceIsNotAvailable() throws TaskHandlerException, ActionNotFoundException {
    TaskActionInformation actionInformation = new TaskActionInformation("action", "channel", "module", "0.1", "serviceInterface", "serviceMethod");
    ActionEvent actionEvent = new ActionEventBuilder().setDisplayName("Action").setSubject("actionSubject").setDescription("").setServiceInterface("serviceInterface").setServiceMethod("serviceMethod").setActionParameters(new TreeSet<ActionParameter>()).build();
    actionEvent.setActionParameters(new TreeSet<>());
    when(taskService.getActionEventFor(actionInformation)).thenReturn(actionEvent);
    when(bundleContext.getServiceReference("serviceInterface")).thenReturn(null);
    Task task = new TaskBuilder().addAction(new TaskActionInformation("Action", "channel", "module", "0.1", "actionSubject")).build();
    taskActionExecutor.setBundleContext(bundleContext);
    taskActionExecutor.execute(task, actionInformation, 0, new TaskContext(task, new HashMap<>(), new HashMap<>(), activityService), TASK_ACTIVITY_ID);
    verify(eventRelay).sendEventMessage(any(MotechEvent.class));
}
Also used : TaskBuilder(org.motechproject.tasks.domain.mds.task.builder.TaskBuilder) Task(org.motechproject.tasks.domain.mds.task.Task) TaskContext(org.motechproject.tasks.service.util.TaskContext) HashMap(java.util.HashMap) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) TreeSet(java.util.TreeSet) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) MotechEvent(org.motechproject.event.MotechEvent) Test(org.junit.Test) ObjectTest(org.motechproject.tasks.domain.ObjectTest)

Example 3 with TaskContext

use of org.motechproject.tasks.service.util.TaskContext in project motech by motech.

the class TaskActionExecutorTest method shouldInvokeServiceIfActionHasService.

@Test
public void shouldInvokeServiceIfActionHasService() throws ActionNotFoundException, TaskHandlerException {
    TaskActionInformation actionInformation = new TaskActionInformation("action", "channel", "module", "0.1", "serviceInterface", "serviceMethod");
    ActionEvent actionEvent = new ActionEventBuilder().setDisplayName("Action").setDescription("").setServiceInterface("serviceInterface").setServiceMethod("serviceMethod").setActionParameters(new TreeSet<>()).build();
    when(taskService.getActionEventFor(actionInformation)).thenReturn(actionEvent);
    ServiceReference serviceReference = mock(ServiceReference.class);
    when(bundleContext.getServiceReference("serviceInterface")).thenReturn(serviceReference);
    TestService testService = new TestService();
    when(bundleContext.getService(serviceReference)).thenReturn(testService);
    Task task = new TaskBuilder().addAction(new TaskActionInformation("Action", "channel", "module", "0.1", "actionSubject")).build();
    taskActionExecutor.setBundleContext(bundleContext);
    taskActionExecutor.execute(task, actionInformation, 0, new TaskContext(task, new HashMap<>(), new HashMap<>(), activityService), TASK_ACTIVITY_ID);
    assertTrue(testService.serviceMethodInvoked());
}
Also used : TaskBuilder(org.motechproject.tasks.domain.mds.task.builder.TaskBuilder) Task(org.motechproject.tasks.domain.mds.task.Task) TaskContext(org.motechproject.tasks.service.util.TaskContext) HashMap(java.util.HashMap) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) TreeSet(java.util.TreeSet) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test) ObjectTest(org.motechproject.tasks.domain.ObjectTest)

Example 4 with TaskContext

use of org.motechproject.tasks.service.util.TaskContext in project motech by motech.

the class TaskActionExecutorTest method shouldAddActivityNotificationIfServiceIsNotAvailable.

@Test
public void shouldAddActivityNotificationIfServiceIsNotAvailable() throws TaskHandlerException, ActionNotFoundException {
    TaskActionInformation actionInformation = new TaskActionInformation("action", "channel", "module", "0.1", "serviceInterface", "serviceMethod");
    ActionEvent actionEvent = new ActionEventBuilder().setDisplayName("Action").setSubject("actionSubject").setDescription("").setServiceInterface("serviceInterface").setServiceMethod("serviceMethod").setActionParameters(new TreeSet<>()).build();
    actionEvent.setActionParameters(new TreeSet<>());
    when(taskService.getActionEventFor(actionInformation)).thenReturn(actionEvent);
    when(bundleContext.getServiceReference("serviceInterface")).thenReturn(null);
    Task task = new TaskBuilder().addAction(new TaskActionInformation("Action", "channel", "module", "0.1", "actionSubject")).build();
    taskActionExecutor.setBundleContext(bundleContext);
    taskActionExecutor.execute(task, actionInformation, 0, new TaskContext(task, new HashMap<>(), new HashMap<>(), activityService), TASK_ACTIVITY_ID);
    verify(activityService).addWarning(task, "task.warning.serviceUnavailable", "serviceInterface");
}
Also used : TaskBuilder(org.motechproject.tasks.domain.mds.task.builder.TaskBuilder) Task(org.motechproject.tasks.domain.mds.task.Task) TaskContext(org.motechproject.tasks.service.util.TaskContext) HashMap(java.util.HashMap) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) TreeSet(java.util.TreeSet) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) Test(org.junit.Test) ObjectTest(org.motechproject.tasks.domain.ObjectTest)

Example 5 with TaskContext

use of org.motechproject.tasks.service.util.TaskContext in project motech by motech.

the class TaskActionExecutorTest method shouldRaiseEventIfActionHasSubject.

@Test
public void shouldRaiseEventIfActionHasSubject() throws ActionNotFoundException, TaskHandlerException {
    TaskActionInformation actionInformation = new TaskActionInformation("action", "channel", "module", "0.1", "actionSubject");
    ActionEvent actionEvent = new ActionEventBuilder().setDisplayName("Action").setSubject("actionSubject").setDescription("").setActionParameters(new TreeSet<>()).build();
    when(taskService.getActionEventFor(actionInformation)).thenReturn(actionEvent);
    Task task = new TaskBuilder().addAction(new TaskActionInformation("Action", "channel", "module", "0.1", "actionSubject")).build();
    task.setId(11L);
    Map<String, Object> metadata = new HashMap<>();
    metadata.put(EventDataKeys.TASK_ID, 11L);
    metadata.put(EventDataKeys.TASK_RETRY, null);
    metadata.put(EventDataKeys.TASK_ACTIVITY_ID, TASK_ACTIVITY_ID);
    taskActionExecutor.setBundleContext(bundleContext);
    taskActionExecutor.execute(task, actionInformation, 0, new TaskContext(task, new HashMap<>(), metadata, activityService), TASK_ACTIVITY_ID);
    MotechEvent raisedEvent = new MotechEvent("actionSubject", new HashMap<>(), TasksEventCallbackService.TASKS_EVENT_CALLBACK_NAME, metadata);
    verify(eventRelay).sendEventMessage(raisedEvent);
}
Also used : TaskBuilder(org.motechproject.tasks.domain.mds.task.builder.TaskBuilder) Task(org.motechproject.tasks.domain.mds.task.Task) TaskContext(org.motechproject.tasks.service.util.TaskContext) HashMap(java.util.HashMap) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) TreeSet(java.util.TreeSet) MotechEvent(org.motechproject.event.MotechEvent) Test(org.junit.Test) ObjectTest(org.motechproject.tasks.domain.ObjectTest)

Aggregations

TaskContext (org.motechproject.tasks.service.util.TaskContext)9 Test (org.junit.Test)8 ObjectTest (org.motechproject.tasks.domain.ObjectTest)8 ActionEvent (org.motechproject.tasks.domain.mds.channel.ActionEvent)8 Task (org.motechproject.tasks.domain.mds.task.Task)8 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)8 HashMap (java.util.HashMap)7 TreeSet (java.util.TreeSet)7 ActionEventBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)7 TaskBuilder (org.motechproject.tasks.domain.mds.task.builder.TaskBuilder)7 MotechEvent (org.motechproject.event.MotechEvent)3 ServiceReference (org.osgi.framework.ServiceReference)3 ArrayList (java.util.ArrayList)1 FilterSet (org.motechproject.tasks.domain.mds.task.FilterSet)1 TaskHandlerException (org.motechproject.tasks.exception.TaskHandlerException)1