TaskResource { @Get("/:name") public TaskResponse getTask(String id) { TaskManager.MonitorableFutureTask task = taskManager.getTask(id); TaskResponse response = task == null ? null:new TaskResponse(task); return notFoundIfNull(response); } @Inject  TaskResource(final TaskFactory taskFactory, final TaskManager taskManager, final PropertiesProvider propertiesProvider, final PipelineRegistry pipelineRegistry); @Get("/all") List<TaskResponse> tasks(Context context); @Get("/:name") TaskResponse getTask(String id); @Post("/batchUpdate/index") TaskResponse indexQueue(final OptionsWrapper optionsWrapper, Context context); @Post("/batchUpdate/index/file") List<TaskResponse> indexDefault(final OptionsWrapper optionsWrapper, Context context); @Post("/batchUpdate/index/:filePath:") List<TaskResponse> indexFile(final String filePath, final OptionsWrapper optionsWrapper, Context context); @Post("/batchUpdate/scan/:filePath:") TaskResponse scanFile(final String filePath, final OptionsWrapper optionsWrapper, Context context); @Post("/clean") List<TaskResponse> cleanDoneTasks(); @Post("/batchSearch") TaskResponse runBatchSearches(Context context); @Put("/stop/:taskId:") boolean stopTask(final String taskId); @net.codestory.http.annotations.Options("/stop/:taskName:") Payload stopTaskPreflight(final String taskName); @Put("/stopAll") Map<String, Boolean> stopAllTasks(final Context context); @net.codestory.http.annotations.Options("/stopAll") Payload stopAllTasksPreflight(); @Post("/findNames/:pipeline") List<TaskResponse> extractNlp(final String pipelineName, final OptionsWrapper optionsWrapper, Context context);  }
@Test public void test_stop_task() { TaskManager.MonitorableFutureTask<String> dummyTask = taskManager.startTask(() -> { Thread.sleep(10000); return "ok"; }); put("/api/task/stop/" + dummyTask).should().respond(200).contain("true"); assertThat(taskManager.getTask(dummyTask.toString()).isCancelled()).isTrue(); get("/api/task/all").should().respond(200).contain("\"state\":\"CANCELLED\""); }
@Test public void test_stop_all() { TaskManager.MonitorableFutureTask<String> t1 = taskManager.startTask(() -> { Thread.sleep(10000); return "ok"; }); TaskManager.MonitorableFutureTask<String> t2 = taskManager.startTask(() -> { Thread.sleep(10000); return "ok"; }); put("/api/task/stopAll").should().respond(200). contain(t1.toString() + "\":true"). contain(t2.toString() + "\":true"); assertThat(taskManager.getTask(t1.toString()).isCancelled()).isTrue(); assertThat(taskManager.getTask(t2.toString()).isCancelled()).isTrue(); }