Skip to content

Commit

Permalink
feat: deleteAll stops and cleans done tasks #1535
Browse files Browse the repository at this point in the history
  • Loading branch information
mvanzalu committed Jan 15, 2025
1 parent b3ad8a3 commit abbf08d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@

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;
import java.util.concurrent.Executors;
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;

Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit abbf08d

Please sign in to comment.