TaskRetryHandler { public void handleTaskRetries(Task task, Map<String, Object> parameters) { if (task.isRetryTaskOnFailure()) { LOGGER.info("Scheduling task retries, since the execution of a task failed."); scheduleTaskRetry(task, parameters); } }  void handleTaskRetries(Task task, Map<String, Object> parameters);  }
@Test public void shouldScheduleTaskRetriesOnFailure() throws Exception { setTriggerEvent(); setActionEvent(); task.setRetryTaskOnFailure(true); task.setRetryIntervalInMilliseconds(5000); actionEvent.setServiceInterface("TestService"); actionEvent.setServiceMethod("abc"); when(taskService.findActiveTasksForTriggerSubject(triggerEvent.getSubject())).thenReturn(tasks); when(taskService.getActionEventFor(task.getActions().get(0))).thenThrow(new RuntimeException()); taskRetryHandler.handleTaskRetries(task, createEventParameters()); ArgumentCaptor<MotechEvent> captorEvent = ArgumentCaptor.forClass(MotechEvent.class); verify(eventRelay).sendEventMessage(captorEvent.capture()); MotechEvent scheduleJobEvent = captorEvent.getValue(); assertEquals(SCHEDULE_REPEATING_JOB, scheduleJobEvent.getSubject()); Map<String, Object> metadata = scheduleJobEvent.getMetadata(); assertEquals(5, metadata.get(EventDataKeys.JOB_START)); assertEquals(task.getId(), metadata.get(EventDataKeys.TASK_ID)); assertEquals(task.getTrigger().getEffectiveListenerRetrySubject(), metadata.get(EventDataKeys.JOB_SUBJECT)); }
@Test public void shouldNotScheduleTaskRetryOnFailureWhenRetryTaskOnFailureIsFalse() throws Exception { setTriggerEvent(); setActionEvent(); task.setRetryTaskOnFailure(false); actionEvent.setServiceInterface("TestService"); actionEvent.setServiceMethod("abc"); when(taskService.findActiveTasksForTriggerSubject(triggerEvent.getSubject())).thenReturn(tasks); when(taskService.getActionEventFor(task.getActions().get(0))).thenThrow(new RuntimeException()); MotechEvent event = createEvent(); taskRetryHandler.handleTaskRetries(task, event.getParameters()); verify(eventRelay, never()).sendEventMessage(any(MotechEvent.class)); }