From fb47d47d8b45712e957b4dcc8c2cb5172fa29fed Mon Sep 17 00:00:00 2001 From: mvanzalu Date: Fri, 17 Jan 2025 18:29:49 +0000 Subject: [PATCH] feat: include TaskManager in TaskResource #1638 --- .../icij/datashare/web/StatusResource.java | 14 ++++++---- .../datashare/web/StatusResourceTest.java | 26 +++++++++++++++---- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/datashare-app/src/main/java/org/icij/datashare/web/StatusResource.java b/datashare-app/src/main/java/org/icij/datashare/web/StatusResource.java index a03d07a94..ee1d4d59e 100644 --- a/datashare-app/src/main/java/org/icij/datashare/web/StatusResource.java +++ b/datashare-app/src/main/java/org/icij/datashare/web/StatusResource.java @@ -14,6 +14,7 @@ import net.codestory.http.payload.Payload; import org.icij.datashare.PropertiesProvider; import org.icij.datashare.Repository; +import org.icij.datashare.asynctasks.TaskManager; import org.icij.datashare.openmetrics.StatusMapper; import org.icij.datashare.extract.DocumentCollectionFactory; import org.icij.datashare.text.indexing.Indexer; @@ -26,16 +27,17 @@ @Singleton @Prefix("/api") public class StatusResource { - Logger logger = LoggerFactory.getLogger(getClass()); private final PropertiesProvider propertiesProvider; private final Repository repository; private final Indexer indexer; + private final TaskManager taskManager; @Inject - public StatusResource(PropertiesProvider propertiesProvider, Repository repository, Indexer indexer) { + public StatusResource(PropertiesProvider propertiesProvider, Repository repository, Indexer indexer, TaskManager taskManager) { this.propertiesProvider = propertiesProvider; this.repository = repository; this.indexer = indexer; + this.taskManager = taskManager; } @Operation(description = "Retrieve the status of databus connection, database connection and index.", @@ -45,7 +47,7 @@ public StatusResource(PropertiesProvider propertiesProvider, Repository reposito @ApiResponse(responseCode = "503", description = "service unavailable when other services are down", useReturnTypeSchema = true) @Get("/status") public Payload getStatus(Context context) { - Status status = new Status(repository.getHealth(), indexer.getHealth()); + Status status = new Status(repository.getHealth(), indexer.getHealth(), taskManager.getHealth()); if ("openmetrics".equals(context.request().query().get("format"))) { return new Payload("text/plain;version=0.0.4", new StatusMapper("datashare", status, propertiesProvider.get("platform").orElse(null)).toString()); @@ -57,17 +59,19 @@ public Payload getStatus(Context context) { public static class Status { public final boolean database; public final boolean index; + public final boolean taskManager; - Status(boolean database, boolean index) { + Status(boolean database, boolean index, boolean taskManager) { this.database = database; this.index = index; + this.taskManager = taskManager; } @JsonIgnore int getHttpStatus() { if (!index) { return HttpStatus.GATEWAY_TIMEOUT; - } else if (!database) { + } else if (!database || !taskManager) { return HttpStatus.SERVICE_UNAVAILABLE; } return HttpStatus.OK; diff --git a/datashare-app/src/test/java/org/icij/datashare/web/StatusResourceTest.java b/datashare-app/src/test/java/org/icij/datashare/web/StatusResourceTest.java index 96fc0ef91..f49e0fd37 100644 --- a/datashare-app/src/test/java/org/icij/datashare/web/StatusResourceTest.java +++ b/datashare-app/src/test/java/org/icij/datashare/web/StatusResourceTest.java @@ -2,6 +2,7 @@ import org.icij.datashare.PropertiesProvider; import org.icij.datashare.Repository; +import org.icij.datashare.asynctasks.TaskManager; import org.icij.datashare.test.DatashareTimeRule; import org.icij.datashare.text.indexing.Indexer; import org.icij.datashare.web.testhelpers.AbstractProdWebServerTest; @@ -21,21 +22,23 @@ public class StatusResourceTest extends AbstractProdWebServerTest { @Rule public DatashareTimeRule time = new DatashareTimeRule("2020-06-30T15:31:00Z"); @Mock Repository repository; @Mock Indexer indexer; + @Mock TaskManager taskManager; @Before public void setUp() { initMocks(this); - configure(routes -> routes.add(new StatusResource(new PropertiesProvider(),repository,indexer))); + configure(routes -> routes.add(new StatusResource(new PropertiesProvider(),repository,indexer,taskManager))); } @Test public void test_get_status_ok() { when(repository.getHealth()).thenReturn(true); when(indexer.getHealth()).thenReturn(true); + when(taskManager.getHealth()).thenReturn(true); get("/api/status").should().respond(200). contain("\"database\":true"). contain("\"index\":true"). - contain("\"database\":true"); + contain("\"taskManager\":true"); } @Test @@ -48,13 +51,24 @@ public void test_get_index_status() { public void test_get_database_status_when_down() { when(repository.getHealth()).thenReturn(false); when(indexer.getHealth()).thenReturn(true); + when(taskManager.getHealth()).thenReturn(true); get("/api/status").should().respond(503).contain("\"database\":false").haveType("application/json"); } + @Test + public void test_get_taskmanager_status_when_down() { + when(repository.getHealth()).thenReturn(true); + when(indexer.getHealth()).thenReturn(true); + when(taskManager.getHealth()).thenReturn(false); + get("/api/status").should().respond(503).contain("\"taskManager\":false").haveType("application/json"); + } + + @Test public void test_get_index_status_when_down() { when(indexer.getHealth()).thenReturn(false); when(repository.getHealth()).thenReturn(true); + when(taskManager.getHealth()).thenReturn(true); get("/api/status").should().respond(504).contain("\"index\":false").haveType("application/json"); } @@ -70,18 +84,20 @@ public void test_get_status_with_open_metrics_format() { "# HELP datashare The datashare resources status\n" + "# TYPE datashare gauge\n" + "datashare{status=\"KO\",resource=\"database\"} 0 1593531060000\n" + - "datashare{status=\"KO\",resource=\"index\"} 0 1593531060000"); + "datashare{status=\"KO\",resource=\"index\"} 0 1593531060000\n" + + "datashare{status=\"KO\",resource=\"taskManager\"} 0 1593531060000\n"); } @Test public void test_get_status_with_open_metrics_format_with_platform_name() { configure(routes -> routes.add(new StatusResource(new PropertiesProvider(new HashMap<>() {{ put("platform", "platform"); - }}),repository,indexer))); + }}),repository,indexer,taskManager))); get("/api/status?format=openmetrics").should().respond(200).haveType("text/plain;version=0.0.4").contain("" + "# HELP datashare The datashare resources status\n" + "# TYPE datashare gauge\n" + "datashare{environment=\"platform\",status=\"KO\",resource=\"database\"} 0 1593531060000\n" + - "datashare{environment=\"platform\",status=\"KO\",resource=\"index\"} 0 1593531060000"); + "datashare{environment=\"platform\",status=\"KO\",resource=\"index\"} 0 1593531060000\n" + + "datashare{environment=\"platform\",status=\"KO\",resource=\"taskManager\"} 0 1593531060000\n"); } }