From abbf08d809f7cee550403f62c51675ee2b32582d Mon Sep 17 00:00:00 2001 From: mvanzalu Date: Tue, 14 Jan 2025 13:05:20 +0000 Subject: [PATCH] feat: deleteAll stops and cleans done tasks #1535 --- .../icij/datashare/web/ProjectResource.java | 4 +++ .../asynctasks/TaskManagersIntTest.java | 36 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/datashare-app/src/main/java/org/icij/datashare/web/ProjectResource.java b/datashare-app/src/main/java/org/icij/datashare/web/ProjectResource.java index 4e070d93e..c7b1d5dbb 100644 --- a/datashare-app/src/main/java/org/icij/datashare/web/ProjectResource.java +++ b/datashare-app/src/main/java/org/icij/datashare/web/ProjectResource.java @@ -49,6 +49,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; + import static java.util.concurrent.TimeUnit.MILLISECONDS; + import static java.util.concurrent.TimeUnit.SECONDS; import static net.codestory.http.errors.NotFoundException.notFoundIfNull; import static net.codestory.http.payload.Payload.ok; import static org.apache.tika.utils.StringUtils.isEmpty; @@ -221,6 +223,8 @@ public Payload deleteProjects(Context context) { } }); try { + logger.info("Stopping tasks : {}", taskManager.stopAllTasks(user)); + taskManager.waitTasksToBeDone(TaskManager.POLLING_INTERVAL*2, MILLISECONDS); logger.info("Deleted tasks : {}", !taskManager.clearDoneTasks().isEmpty()); } catch (IOException e) { throw new RuntimeException(e); diff --git a/datashare-tasks/src/test/java/org/icij/datashare/asynctasks/TaskManagersIntTest.java b/datashare-tasks/src/test/java/org/icij/datashare/asynctasks/TaskManagersIntTest.java index 4bfa7034d..cdf06a378 100644 --- a/datashare-tasks/src/test/java/org/icij/datashare/asynctasks/TaskManagersIntTest.java +++ b/datashare-tasks/src/test/java/org/icij/datashare/asynctasks/TaskManagersIntTest.java @@ -24,6 +24,7 @@ import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; @@ -31,6 +32,7 @@ import java.util.concurrent.TimeUnit; import static java.util.Arrays.asList; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.SECONDS; import static org.fest.assertions.Assertions.assertThat; @@ -141,6 +143,40 @@ public void test_stop_all_tasks() throws Exception { assertThat(taskManager.getTask(tv2Id).getState()).isEqualTo(Task.State.CANCELLED); } + @Test(timeout = 10000) + public void test_stop_all_wait_clear_done_tasks() throws Exception { + String tv1Id = taskManager.startTask(TestFactory.SleepForever.class, User.local(), new HashMap<>()); + String tv2Id = taskManager.startTask(TestFactory.SleepForever.class, User.local(), new HashMap<>()); + + taskInspector.awaitStatus(tv1Id, Task.State.RUNNING, 1, SECONDS); + taskManager.stopAllTasks(User.local()); + taskInspector.awaitStatus(tv1Id, Task.State.CANCELLED, 1, SECONDS); + + assertThat(taskManager.getTask(tv1Id).getState()).isEqualTo(Task.State.CANCELLED); + assertThat(taskManager.getTask(tv2Id).getState()).isEqualTo(Task.State.CANCELLED); + + taskManager.clearDoneTasks(); + + assertThat(taskManager.getTasks()).isEmpty(); + } + + @Test() + public void test_stop_all_wait_clear_done_tasks_not_cancellable_task() throws Exception { + String tv1Id = taskManager.startTask(TestFactory.Sleep.class, User.local(), Map.of("duration", 3000)); + String tv2Id = taskManager.startTask(TestFactory.SleepForever.class, User.local(), new HashMap<>()); + + taskInspector.awaitStatus(tv1Id, Task.State.RUNNING, 1, SECONDS); + taskManager.stopAllTasks(User.local()); + taskInspector.awaitStatus(tv1Id, Task.State.DONE, 1, SECONDS); + + assertThat(taskManager.getTask(tv1Id).getState()).isEqualTo(Task.State.RUNNING); + assertThat(taskManager.getTask(tv2Id).getState()).isEqualTo(Task.State.CREATED); + + taskManager.clearDoneTasks(); + + assertThat(taskManager.getTasks()).isNotEmpty(); + } + @Test(timeout = 10000) public void test_await_tasks_termination() throws Exception { String tv1Id = taskManager.startTask(TestFactory.Sleep.class, User.local(), Map.of("duration", 100));