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);
}
}
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));
}
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());
}
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");
}
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);
}
Aggregations