TasksPostExecutionHandler { 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); } }  void handleActionExecuted(Map<String, Object> params, Map<String, Object> metadata, Long activityId); void handleError(Map<String, Object> params, Map<String, Object> metadata, Task task, TaskHandlerException e, Long activityId);  }
@Test public void shouldDisableTaskWhenNumberOfPossibleErrorsIsExceeded() throws Exception { setTriggerEvent(); setActionEvent(); setTaskActivities(); task.setFailuresInRow(taskActivities.size()); when(settingsFacade.getRawConfig(TASK_PROPERTIES_FILE_NAME)).thenReturn(TASK_RETRIES); assertTrue(task.isEnabled()); postExecutionHandler.handleError(createEventParameters(), new HashMap<>(), task, taskHandlerException, TASK_ACTIVITY_ID); assertFalse(task.isEnabled()); assertEquals(5, task.getFailuresInRow()); verify(taskService).save(task); verify(taskActivityService).addTaskDisabledWarning(task); ArgumentCaptor<MotechEvent> captorEvent = ArgumentCaptor.forClass(MotechEvent.class); verify(eventRelay, times(2)).sendEventMessage(captorEvent.capture()); List<MotechEvent> capturedEvents = captorEvent.getAllValues(); assertEquals(asList("org.motechproject.message", createHandlerFailureSubject(task.getName(), TRIGGER)), extract(capturedEvents, on(MotechEvent.class).getSubject())); }
@Test public void shouldNotScheduleTaskRetryOnFailureWhenNumberOfRetriesIsZero() throws Exception { setTriggerEvent(); setActionEvent(); Map<String, Object> params = setEventParameters(); task.setRetryTaskOnFailure(true); actionEvent.setServiceInterface("TestService"); actionEvent.setServiceMethod("abc"); when(taskService.findActiveTasksForTriggerSubject(triggerEvent.getSubject())).thenReturn(tasks); when(taskService.getActionEventFor(task.getActions().get(0))).thenThrow(new RuntimeException()); when(settingsFacade.getRawConfig(TASK_PROPERTIES_FILE_NAME)).thenReturn(null); postExecutionHandler.handleError(params, new HashMap<>(), task, taskHandlerException, TASK_ACTIVITY_ID); params.put(TASK_RETRY_NUMBER, 2); verify(retryHandler, never()).handleTaskRetries(task, params); }
@Test public void shouldNotScheduleTaskRetryOnFailureWhenAllRetriesWereExecuted() throws Exception { setTriggerEvent(); setActionEvent(); Map<String, Object> params = setEventParameters(); task.setRetryTaskOnFailure(true); actionEvent.setServiceInterface("TestService"); actionEvent.setServiceMethod("abc"); when(taskService.findActiveTasksForTriggerSubject(triggerEvent.getSubject())).thenReturn(tasks); when(taskService.getActionEventFor(task.getActions().get(0))).thenThrow(new RuntimeException()); when(settingsFacade.getRawConfig(TASK_PROPERTIES_FILE_NAME)).thenReturn(TASK_RETRIES); postExecutionHandler.handleError(params, new HashMap<>(), task, taskHandlerException, TASK_ACTIVITY_ID); params.put(TASK_RETRY_NUMBER, 2); verify(retryHandler, never()).handleTaskRetries(task, params); }