Search in sources :

Example 61 with MotechEvent

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"));
}
Also used : TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) MotechEvent(org.motechproject.event.MotechEvent) Test(org.junit.Test)

Example 62 with MotechEvent

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()));
}
Also used : TaskBuilder(org.motechproject.tasks.domain.mds.task.builder.TaskBuilder) Task(org.motechproject.tasks.domain.mds.task.Task) HashMap(java.util.HashMap) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) MotechEvent(org.motechproject.event.MotechEvent) KeyInformation(org.motechproject.tasks.domain.KeyInformation) Test(org.junit.Test)

Example 63 with MotechEvent

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));
}
Also used : HashMap(java.util.HashMap) MotechEvent(org.motechproject.event.MotechEvent)

Example 64 with MotechEvent

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));
}
Also used : MotechEvent(org.motechproject.event.MotechEvent)

Example 65 with MotechEvent

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);
    }
}
Also used : HashMap(java.util.HashMap) MotechEvent(org.motechproject.event.MotechEvent)

Aggregations

MotechEvent (org.motechproject.event.MotechEvent)138 Test (org.junit.Test)87 HashMap (java.util.HashMap)79 CronSchedulableJob (org.motechproject.scheduler.contract.CronSchedulableJob)28 DateTime (org.joda.time.DateTime)25 DateUtil.newDateTime (org.motechproject.commons.date.util.DateUtil.newDateTime)20 ArrayList (java.util.ArrayList)14 RepeatingSchedulableJob (org.motechproject.scheduler.contract.RepeatingSchedulableJob)13 RunOnceSchedulableJob (org.motechproject.scheduler.contract.RunOnceSchedulableJob)11 JobBasicInfo (org.motechproject.scheduler.contract.JobBasicInfo)10 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)8 Matchers.anyString (org.mockito.Matchers.anyString)7 EventListener (org.motechproject.event.listener.EventListener)7 CronJobId (org.motechproject.scheduler.contract.CronJobId)7 Task (org.motechproject.tasks.domain.mds.task.Task)7 JobId (org.motechproject.scheduler.contract.JobId)6 RepeatingJobId (org.motechproject.scheduler.contract.RepeatingJobId)6 Time (org.motechproject.commons.date.model.Time)5 DayOfWeekSchedulableJob (org.motechproject.scheduler.contract.DayOfWeekSchedulableJob)5 RepeatingPeriodSchedulableJob (org.motechproject.scheduler.contract.RepeatingPeriodSchedulableJob)5