use of org.motechproject.event.MotechEvent in project motech by motech.
the class TaskTriggerHandlerTest method shouldNotPassFiltersCriteriaAndNotExecuteSecondAction.
@Test
public void shouldNotPassFiltersCriteriaAndNotExecuteSecondAction() throws Exception {
setTriggerEvent();
setActionEvent();
addActionFilterNotPassingCriteria();
setSecondAction();
when(taskService.findActiveTasksForTriggerSubject(TRIGGER_SUBJECT)).thenReturn(tasks);
when(taskService.getActionEventFor(any(TaskActionInformation.class))).thenReturn(actionEvent);
ArgumentCaptor<MotechEvent> captor = ArgumentCaptor.forClass(MotechEvent.class);
handler.handle(createEvent());
verify(taskService).findActiveTasksForTriggerSubject(TRIGGER_SUBJECT);
verify(taskService).getActionEventFor(task.getActions().get(0));
verify(taskService, never()).getActionEventFor(task.getActions().get(1));
verify(eventRelay, times(1)).sendEventMessage(captor.capture());
verify(taskActivityService, never()).addTaskFiltered(TASK_ACTIVITY_ID);
List<MotechEvent> events = captor.getAllValues();
assertEquals(asList(ACTION_SUBJECT), extract(events, on(MotechEvent.class).getSubject()));
MotechEvent motechEventAction1 = events.get(0);
assertEquals(ACTION_SUBJECT, motechEventAction1.getSubject());
assertNotNull(motechEventAction1.getParameters());
assertEquals(2, motechEventAction1.getParameters().size());
assertEquals(task.getActions().get(0).getValues().get("phone"), motechEventAction1.getParameters().get("phone").toString());
assertEquals("Hello 123456789, You have an appointment on 2012-11-20", motechEventAction1.getParameters().get("message"));
}
use of org.motechproject.event.MotechEvent in project motech by motech.
the class TaskContextTest method testGetNullTriggerKey.
@Test
public void testGetNullTriggerKey() throws Exception {
Map<String, Object> parameters = new HashMap<>();
MotechEvent event = mock(MotechEvent.class);
KeyInformation key = parse(String.format("%s.%s", TRIGGER_PREFIX, EVENT_KEY));
Map<String, String> child = new HashMap<>();
child.put("key", null);
parameters.put("event", child);
Task task = new TaskBuilder().addAction(new TaskActionInformation()).build();
assertEquals(null, new TaskContext(task, parameters, null, activityService).getTriggerValue(key.getKey()));
// should not throw any exceptions
assertNull(new TaskContext(task, parameters, null, activityService).getTriggerValue(key.getKey()));
}
use of org.motechproject.event.MotechEvent in project motech by motech.
the class TasksPostExecutionHandler method publishTaskDisabledMessage.
private void publishTaskDisabledMessage(String taskName) {
Map<String, Object> params = new HashMap<>();
params.put("message", "Task disabled automatically: " + taskName);
params.put("level", "CRITICAL");
params.put("moduleName", settings.getBundleSymbolicName());
eventRelay.sendEventMessage(new MotechEvent("org.motechproject.message", params));
}
use of org.motechproject.event.MotechEvent in project motech by motech.
the class TasksPostExecutionHandler method handleSuccess.
private void handleSuccess(Map<String, Object> params, Task task) {
LOGGER.debug("All actions from task: {} with ID: {} were successfully executed", task.getName(), task.getId());
task.resetFailuresInRow();
taskService.save(task);
eventRelay.sendEventMessage(new MotechEvent(createHandlerSuccessSubject(task.getName()), params));
}
use of org.motechproject.event.MotechEvent in project motech by motech.
the class TasksPostExecutionHandler method handleError.
/**
* Handles task action failure. It sets the specified task activity as failed and raises the failures in a row count of
* a task. If the failure threshold is reached, it disables the task and publishes an event. It passes the
* info about failed execution to {@link TaskRetryHandler}.
*
* @param params trigger event parameters that invoked the task
* @param task the task that has failed
* @param e the exception that caused the failure
* @param activityId the id of an activity
*/
public void handleError(Map<String, Object> params, Map<String, Object> metadata, Task task, TaskHandlerException e, Long activityId) {
LOGGER.warn("Omitted task: {} with ID: {} because: {}", task.getName(), task.getId(), e);
activityService.addFailedExecution(activityId, e);
task.incrementFailuresInRow();
LOGGER.info("The number of failures for task: {} is: {}", task.getName(), task.getFailuresInRow());
int failureNumber = task.getFailuresInRow();
int possibleErrorsNumber = getPossibleErrorsNumber();
if (possibleErrorsNumber != 0 && failureNumber >= possibleErrorsNumber) {
task.setEnabled(false);
activityService.addTaskDisabledWarning(task);
publishTaskDisabledMessage(task.getName());
}
taskService.save(task);
Map<String, Object> errorParam = new HashMap<>();
errorParam.put(TASK_FAIL_MESSAGE, e.getMessage());
errorParam.put(TASK_FAIL_STACK_TRACE, ExceptionUtils.getStackTrace(e));
errorParam.put(TASK_FAIL_FAILURE_DATE, DateTime.now());
errorParam.put(TASK_FAIL_FAILURE_NUMBER, failureNumber);
errorParam.put(TASK_FAIL_TRIGGER_DISABLED, task.isEnabled());
errorParam.put(TASK_FAIL_TASK_ID, task.getId());
errorParam.put(TASK_FAIL_TASK_NAME, task.getName());
Map<String, Object> errorEventParam = new HashMap<>();
errorEventParam.putAll(params);
errorEventParam.put(HANDLER_ERROR_PARAM, errorParam);
eventRelay.sendEventMessage(new MotechEvent(createHandlerFailureSubject(task.getName(), e.getFailureCause()), errorEventParam));
int numberOfRetries = getRetriesFromSettings(task, params);
if (shouldScheduleRetry(params, numberOfRetries)) {
retryHandler.handleTaskRetries(task, params);
}
}
Aggregations