Skip to content

Commit

Permalink
feat: add basic healthcheck endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Dec 11, 2024
1 parent ab8c1ee commit 7c33bbd
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions java/app/src/main/java/org/vss/KVStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface KVStore {
DeleteObjectResponse delete(String userToken, DeleteObjectRequest request);

ListKeyVersionsResponse listKeyVersions(String userToken, ListKeyVersionsRequest request);

void checkHealth();
}
45 changes: 45 additions & 0 deletions java/app/src/main/java/org/vss/api/HealthCheckApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.vss.api;

import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;
import org.vss.GetObjectRequest;
import org.vss.GetObjectResponse;
import org.vss.KVStore;
import org.vss.auth.AuthResponse;
import org.vss.auth.Authorizer;

@Path(VssApiEndpoint.HEALTHCHECK)
@Slf4j
public class HealthCheckApi {
KVStore kvStore;

@Inject
public HealthCheckApi(KVStore kvstore) {
this.kvStore = kvstore;
}

@GET
public Response execute(@Context HttpHeaders headers) {
try {
log.info("Healthcheck requested");
kvStore.checkHealth();
log.info("Healthcheck passed");

return Response
.status(Response.Status.OK)
.build();
} catch (Exception e) {
log.error("Exception in HealthCheckApi: ", e);
return Response.status(500)
.entity("an unexpected error occurred: " + e.getMessage())
.build();
}
}
}
1 change: 1 addition & 0 deletions java/app/src/main/java/org/vss/api/VssApiEndpoint.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.vss.api;

public class VssApiEndpoint {
public static final String HEALTHCHECK = "/health";
public static final String GET_OBJECT = "/getObject";
public static final String PUT_OBJECTS = "/putObjects";
public static final String DELETE_OBJECT = "/deleteObject";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ public PostgresBackendImpl(DSLContext context) {
this.context = context;
}

@Override
public void checkHealth() {
OffsetDateTime yesterday = OffsetDateTime.now().minusDays(1);

VssDbRecord vssDbRecord = context.selectFrom(VSS_DB)
.where(VSS_DB.LAST_UPDATED_AT.gt(yesterday))
.limit(1)
.fetchOne();

if (vssDbRecord == null) {
throw new IllegalStateException("No recent records found");
}
}

@Override
public GetObjectResponse get(String userToken, GetObjectRequest request) {

Expand Down

0 comments on commit 7c33bbd

Please sign in to comment.