diff --git a/src/integrationTest/java/uk/gov/hmcts/darts/task/service/AdminGetAutomatedTaskServiceTest.java b/src/integrationTest/java/uk/gov/hmcts/darts/task/service/AdminGetAutomatedTaskServiceTest.java index faaf3c8264..5b4ef2337a 100644 --- a/src/integrationTest/java/uk/gov/hmcts/darts/task/service/AdminGetAutomatedTaskServiceTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/darts/task/service/AdminGetAutomatedTaskServiceTest.java @@ -38,7 +38,7 @@ void findsAutomatedTasksById() { void findsAllAutomatedTasks() { var persistedTasks = dartsDatabase.getAllAutomatedTasks(); - var automatedTasks = adminAutomatedTaskService.getAllAutomatedTasks(); + var automatedTasks = adminAutomatedTaskService.getAllAutomatedTasksSummaries(); assertThat(automatedTasks).extracting("id").isEqualTo(taskIdsOf(persistedTasks)); assertThat(automatedTasks).extracting("name").isEqualTo(taskNamesOf(persistedTasks)); diff --git a/src/main/java/uk/gov/hmcts/darts/task/controller/AutomatedTasksController.java b/src/main/java/uk/gov/hmcts/darts/task/controller/AutomatedTasksController.java index 6dc8f5d106..411314dcf9 100644 --- a/src/main/java/uk/gov/hmcts/darts/task/controller/AutomatedTasksController.java +++ b/src/main/java/uk/gov/hmcts/darts/task/controller/AutomatedTasksController.java @@ -30,7 +30,7 @@ public class AutomatedTasksController implements TasksApi { @Authorisation(contextId = ANY_ENTITY_ID, globalAccessSecurityRoles = {SUPER_ADMIN}) @Override public ResponseEntity> getAutomatedTasks() { - return new ResponseEntity<>(adminAutomatedTaskService.getAllAutomatedTasks(), HttpStatus.OK); + return new ResponseEntity<>(adminAutomatedTaskService.getAllAutomatedTasksSummaries(), HttpStatus.OK); } @SecurityRequirement(name = SECURITY_SCHEMES_BEARER_AUTH) diff --git a/src/main/java/uk/gov/hmcts/darts/task/runner/AutomatedTask.java b/src/main/java/uk/gov/hmcts/darts/task/runner/AutomatedTask.java index 6ce531521b..315edd990e 100644 --- a/src/main/java/uk/gov/hmcts/darts/task/runner/AutomatedTask.java +++ b/src/main/java/uk/gov/hmcts/darts/task/runner/AutomatedTask.java @@ -6,6 +6,12 @@ public interface AutomatedTask extends Runnable { String getTaskName(); + void run(boolean isManualRun); + + default void run() { + run(false); + } + AutomatedTaskStatus getAutomatedTaskStatus(); String getLastCronExpression(); diff --git a/src/main/java/uk/gov/hmcts/darts/task/runner/impl/AbstractLockableAutomatedTask.java b/src/main/java/uk/gov/hmcts/darts/task/runner/impl/AbstractLockableAutomatedTask.java index 6075e04591..eda366b377 100644 --- a/src/main/java/uk/gov/hmcts/darts/task/runner/impl/AbstractLockableAutomatedTask.java +++ b/src/main/java/uk/gov/hmcts/darts/task/runner/impl/AbstractLockableAutomatedTask.java @@ -53,12 +53,6 @@ public abstract class AbstractLockableAutomatedTask implements AutomatedTask, Au private ThreadLocal executionId; - private boolean isManualTask = false; - - public void setManualTask() { - this.isManualTask = true; - } - protected AbstractLockableAutomatedTask(AutomatedTaskRepository automatedTaskRepository, AutomatedTaskConfigurationProperties automatedTaskConfigurationProperties, LogApi logApi, LockService lockService) { @@ -69,17 +63,15 @@ protected AbstractLockableAutomatedTask(AutomatedTaskRepository automatedTaskRep } private void setupUserAuthentication() { - Jwt jwt = Jwt.withTokenValue("automated-task") .header("alg", "RS256") .claim("emails", List.of(automatedTaskConfigurationProperties.getSystemUserEmail())) .build(); SecurityContextHolder.getContext().setAuthentication(new JwtAuthenticationToken(jwt)); - } @Override - public void run() { + public void run(boolean isManualRun) { executionId = ThreadLocal.withInitial(UUID::randomUUID); preRunTask(); try { @@ -89,8 +81,11 @@ public void run() { AutomatedTaskEntity automatedTask = automatedTaskEntity.get(); String dbCronExpression = automatedTask.getCronExpression(); // Check the cron expression hasn't been changed in the database by another instance, if so skip this run - if (isManualTask || getLastCronExpression().equals(dbCronExpression)) { - if (TRUE.equals(automatedTask.getTaskEnabled())) { + if (isManualRun || getLastCronExpression().equals(dbCronExpression)) { + if (isManualRun || TRUE.equals(automatedTask.getTaskEnabled())) { + if (!TRUE.equals(automatedTask.getTaskEnabled())) { + log.info("Task: {} is inactive but has been run manually", getTaskName()); + } logApi.taskStarted(executionId.get(), this.getTaskName()); lockService.getLockingTaskExecutor().executeWithLock(new LockedTask(), getLockConfiguration()); } else { diff --git a/src/main/java/uk/gov/hmcts/darts/task/service/AdminAutomatedTaskService.java b/src/main/java/uk/gov/hmcts/darts/task/service/AdminAutomatedTaskService.java index bb7ba900c7..e28a9e4e5d 100644 --- a/src/main/java/uk/gov/hmcts/darts/task/service/AdminAutomatedTaskService.java +++ b/src/main/java/uk/gov/hmcts/darts/task/service/AdminAutomatedTaskService.java @@ -8,7 +8,7 @@ public interface AdminAutomatedTaskService { - List getAllAutomatedTasks(); + List getAllAutomatedTasksSummaries(); DetailedAutomatedTask getAutomatedTaskById(Integer taskId); diff --git a/src/main/java/uk/gov/hmcts/darts/task/service/impl/AdminAutomatedTasksServiceImpl.java b/src/main/java/uk/gov/hmcts/darts/task/service/impl/AdminAutomatedTasksServiceImpl.java index 55bd0e88fe..6a9b984ba0 100644 --- a/src/main/java/uk/gov/hmcts/darts/task/service/impl/AdminAutomatedTasksServiceImpl.java +++ b/src/main/java/uk/gov/hmcts/darts/task/service/impl/AdminAutomatedTasksServiceImpl.java @@ -12,6 +12,7 @@ import uk.gov.hmcts.darts.common.repository.AutomatedTaskRepository; import uk.gov.hmcts.darts.task.api.AutomatedTaskName; import uk.gov.hmcts.darts.task.exception.AutomatedTaskApiError; +import uk.gov.hmcts.darts.task.runner.AutomatedTask; import uk.gov.hmcts.darts.task.service.AdminAutomatedTaskService; import uk.gov.hmcts.darts.task.service.LockService; import uk.gov.hmcts.darts.tasks.model.AutomatedTaskPatch; @@ -34,22 +35,29 @@ public class AdminAutomatedTasksServiceImpl implements AdminAutomatedTaskService private final AutomatedTaskRepository automatedTaskRepository; private final AutomatedTasksMapper mapper; - private final ManualTaskService manualTaskService; private final AutomatedTaskRunner automatedTaskRunner; private final AuditApi auditApi; private final LockService lockService; - private final ConfigurableBeanFactory configurableBeanFactory; + private final List automatedTasks; @Override - public List getAllAutomatedTasks() { - var automatedTask = automatedTaskRepository.findAll() + public List getAllAutomatedTasksSummaries() { + return mapper.mapEntitiesToModel(getAllAutomatedTasksEntities()); + } + + List getAllAutomatedTasksEntities() { + return automatedTaskRepository.findAll() .stream() .filter(this::shouldIncludeAutomatedTask) .toList(); - return mapper.mapEntitiesToModel(automatedTask); } + List getAllAutomatedTasks() { + return automatedTasks; + } + + @Override public DetailedAutomatedTask getAutomatedTaskById(Integer taskId) { return mapper.mapEntityToDetailedAutomatedTask(getAutomatedTaskEntityById(taskId)); @@ -65,7 +73,8 @@ public void runAutomatedTask(Integer taskId) { throw new DartsApiException(AUTOMATED_TASK_ALREADY_RUNNING); } - var automatedTask = manualTaskService.getAutomatedTasks().stream() + var automatedTask = getAllAutomatedTasks() + .stream() .filter(task -> task.getTaskName().equals(automatedTaskEntity.getTaskName())) .findFirst(); @@ -74,7 +83,7 @@ public void runAutomatedTask(Integer taskId) { throw new DartsApiException(AutomatedTaskApiError.AUTOMATED_TASK_NOT_CONFIGURED_CORRECTLY); } - automatedTaskRunner.run(automatedTask.get()); + automatedTaskRunner.run(automatedTask.get(), true); auditApi.record(RUN_JOB_MANUALLY, automatedTaskEntity.getTaskName()); } diff --git a/src/main/java/uk/gov/hmcts/darts/task/service/impl/AutomatedTaskRunner.java b/src/main/java/uk/gov/hmcts/darts/task/service/impl/AutomatedTaskRunner.java index acd80619e1..9a277e6a91 100644 --- a/src/main/java/uk/gov/hmcts/darts/task/service/impl/AutomatedTaskRunner.java +++ b/src/main/java/uk/gov/hmcts/darts/task/service/impl/AutomatedTaskRunner.java @@ -3,15 +3,14 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; -import uk.gov.hmcts.darts.task.runner.impl.AbstractLockableAutomatedTask; +import uk.gov.hmcts.darts.task.runner.AutomatedTask; @Component @Slf4j public class AutomatedTaskRunner { - @Async - public void run(AbstractLockableAutomatedTask task) { - log.info("Attempting manual run of {}", task.getTaskName()); - task.run(); + public void run(AutomatedTask task, boolean isManualRun) { + log.info("Attempting {} run of {}", isManualRun ? " manual " : " automated", task.getTaskName()); + task.run(isManualRun); } } diff --git a/src/main/java/uk/gov/hmcts/darts/task/service/impl/ManualTaskService.java b/src/main/java/uk/gov/hmcts/darts/task/service/impl/ManualTaskService.java deleted file mode 100644 index bc0cf7fbbd..0000000000 --- a/src/main/java/uk/gov/hmcts/darts/task/service/impl/ManualTaskService.java +++ /dev/null @@ -1,32 +0,0 @@ -package uk.gov.hmcts.darts.task.service.impl; - -import jakarta.annotation.PostConstruct; -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import uk.gov.hmcts.darts.task.runner.AutoloadingManualTask; -import uk.gov.hmcts.darts.task.runner.impl.AbstractLockableAutomatedTask; - -import java.util.ArrayList; -import java.util.List; - -@Service -@Slf4j -@RequiredArgsConstructor(onConstructor = @__(@Autowired)) -public class ManualTaskService { - private final List autoloadingManualTasks; - - @Getter - private final List automatedTasks = new ArrayList<>(); - - @PostConstruct - public void configureAndLoadAutomatedTasks() { - autoloadingManualTasks.forEach(autoloadingManualTask -> { - AbstractLockableAutomatedTask task = autoloadingManualTask.getAbstractLockableAutomatedTask(); - task.setManualTask(); - automatedTasks.add(task); - }); - } -} \ No newline at end of file diff --git a/src/test/java/uk/gov/hmcts/darts/task/service/impl/AdminAutomatedTasksServiceImplTest.java b/src/test/java/uk/gov/hmcts/darts/task/service/impl/AdminAutomatedTasksServiceImplTest.java index 160496f9ee..3f49535ee8 100644 --- a/src/test/java/uk/gov/hmcts/darts/task/service/impl/AdminAutomatedTasksServiceImplTest.java +++ b/src/test/java/uk/gov/hmcts/darts/task/service/impl/AdminAutomatedTasksServiceImplTest.java @@ -13,6 +13,7 @@ import uk.gov.hmcts.darts.common.exception.DartsApiException; import uk.gov.hmcts.darts.common.repository.AutomatedTaskRepository; import uk.gov.hmcts.darts.task.exception.AutomatedTaskApiError; +import uk.gov.hmcts.darts.task.runner.AutomatedTask; import uk.gov.hmcts.darts.task.runner.impl.AbstractLockableAutomatedTask; import uk.gov.hmcts.darts.task.service.LockService; import uk.gov.hmcts.darts.tasks.model.AutomatedTaskPatch; @@ -27,8 +28,10 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; @@ -43,8 +46,6 @@ class AdminAutomatedTasksServiceImplTest { @Mock private AutomatedTasksMapper mapper; @Mock - private ManualTaskService manualTaskService; - @Mock private AutomatedTaskRunner automatedTaskRunner; @Mock private LockService lockService; @@ -62,17 +63,19 @@ class AdminAutomatedTasksServiceImplTest { @Test void invokesTaskWhenTaskIsNotLocked() { + adminAutomatedTaskService = spy(adminAutomatedTaskService); var automatedTask = anAutomatedTaskEntityWithName("some-task-name", null); when(someAutomatedTask.getTaskName()).thenReturn("some-task-name"); when(lockService.isLocked(automatedTask)).thenReturn(false); when(automatedTaskRepository.findById(1)).thenReturn(Optional.of(automatedTask)); - when(manualTaskService.getAutomatedTasks()).thenReturn(List.of(someAutomatedTask)); + doReturn(List.of((AutomatedTask) someAutomatedTask)).when(adminAutomatedTaskService).getAllAutomatedTasks(); adminAutomatedTaskService.runAutomatedTask(1); - verify(automatedTaskRunner, times(1)).run(someAutomatedTask); - verify(auditApi, times(1)).record(AuditActivity.RUN_JOB_MANUALLY,"some-task-name"); + verify(automatedTaskRunner, times(1)).run(someAutomatedTask, true); + verify(auditApi, times(1)).record(AuditActivity.RUN_JOB_MANUALLY, "some-task-name"); + verify(adminAutomatedTaskService, times(1)).getAllAutomatedTasks(); } @Test @@ -95,7 +98,7 @@ void updateAutomatedTask() { assertFalse(automatedTaskEntity.getTaskEnabled()); assertEquals(100, automatedTaskEntity.getBatchSize()); assertEquals(expectedReturnTask, task); - verify(auditApi).record(AuditActivity.ENABLE_DISABLE_JOB,"some-task-name disabled"); + verify(auditApi).record(AuditActivity.ENABLE_DISABLE_JOB, "some-task-name disabled"); verifyNoMoreInteractions(auditApi); } @@ -116,7 +119,7 @@ void updateAutomatedTaskDoesNotUpdateFieldsWithNullValueInPatchRequest() { @Test - void positiveGetAllAutomatedTasks() { + void positiveGetAllAutomatedTasksSummaries() { AutomatedTaskEntity automatedTaskEntity1 = createAutomatedTaskEntity("task1", true); AutomatedTaskEntity automatedTaskEntity2 = @@ -126,7 +129,7 @@ void positiveGetAllAutomatedTasks() { when(automatedTaskRepository.findAll()).thenReturn(List.of(automatedTaskEntity1, automatedTaskEntity2, automatedTaskEntity3)); - adminAutomatedTaskService.getAllAutomatedTasks(); + adminAutomatedTaskService.getAllAutomatedTasksSummaries(); verify(automatedTaskRepository, times(1)) .findAll(); @@ -136,7 +139,7 @@ void positiveGetAllAutomatedTasks() { @Test - void positiveGetAllAutomatedTasksDisabledExcludeFlag() { + void positiveGetAllAutomatedTasksSummariesDisabledExcludeFlag() { AutomatedTaskEntity automatedTaskEntity1 = createAutomatedTaskEntity("task1", true); AutomatedTaskEntity automatedTaskEntity2 = @@ -146,7 +149,7 @@ void positiveGetAllAutomatedTasksDisabledExcludeFlag() { when(automatedTaskRepository.findAll()).thenReturn(List.of(automatedTaskEntity1, automatedTaskEntity2, automatedTaskEntity3)); - adminAutomatedTaskService.getAllAutomatedTasks(); + adminAutomatedTaskService.getAllAutomatedTasksSummaries(); verify(automatedTaskRepository, times(1)) .findAll(); diff --git a/src/test/java/uk/gov/hmcts/darts/task/service/impl/AutomatedTaskServiceImplTest.java b/src/test/java/uk/gov/hmcts/darts/task/service/impl/AutomatedTaskServiceImplTest.java index 36585376b6..3a45ef9510 100644 --- a/src/test/java/uk/gov/hmcts/darts/task/service/impl/AutomatedTaskServiceImplTest.java +++ b/src/test/java/uk/gov/hmcts/darts/task/service/impl/AutomatedTaskServiceImplTest.java @@ -513,7 +513,7 @@ public void setLastCronExpression(String cronExpression) { } @Override - public void run() { + public void run(boolean isManualRun) { log.debug("Running test automated task"); }