Skip to content

Commit

Permalink
feat: include TaskManager in TaskResource #1638
Browse files Browse the repository at this point in the history
  • Loading branch information
mvanzalu committed Jan 17, 2025
1 parent ff584e8 commit fb47d47
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.",
Expand All @@ -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());
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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");
}

Expand All @@ -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");
}
}

0 comments on commit fb47d47

Please sign in to comment.