Skip to content

Commit

Permalink
feat: funman taskrunner api plumbing (#3411)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwdchang authored Apr 24, 2024
1 parent f7cef87 commit 818aa5f
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 1 deletion.
16 changes: 16 additions & 0 deletions containers/scripts/docker-compose-taskrunner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,19 @@ services:
condition: service_healthy
extra_hosts:
- "host.docker.internal:host-gateway"

funman-taskrunner:
container_name: funman-taskrunner
image: ghcr.io/darpa-askem/funman-taskrunner:latest
networks:
- terarium
environment:
TERARIUM_MQ-ADDRESSES: "amqp://rabbitmq:5672"
TERARIUM_MQ-PASSWORD: "terarium123"
TERARIUM_MQ-USERNAME: "terarium"
TERARIUM_TASKRUNNER_REQUEST-TYPE: "funman"
depends_on:
rabbitmq:
condition: service_healthy
extra_hosts:
- "host.docker.internal:host-gateway"
Original file line number Diff line number Diff line change
@@ -1,25 +1,96 @@
package software.uncharted.terarium.hmiserver.controller.funman;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import feign.FeignException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.annotation.PostConstruct;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.server.ResponseStatusException;
import software.uncharted.terarium.hmiserver.models.funman.FunmanPostQueriesRequest;
import software.uncharted.terarium.hmiserver.models.task.TaskRequest;
import software.uncharted.terarium.hmiserver.models.task.TaskRequest.TaskType;
import software.uncharted.terarium.hmiserver.models.task.TaskResponse;
import software.uncharted.terarium.hmiserver.proxies.funman.FunmanProxy;
import software.uncharted.terarium.hmiserver.security.Roles;
import software.uncharted.terarium.hmiserver.service.CurrentUserService;
import software.uncharted.terarium.hmiserver.service.tasks.TaskService;
import software.uncharted.terarium.hmiserver.service.tasks.TaskService.TaskMode;
import software.uncharted.terarium.hmiserver.service.tasks.ValidateModelConfigHandler;

@RestController
@RequestMapping("/funman/queries")
@RequiredArgsConstructor
@Slf4j
public class FunmanController {

private final ObjectMapper objectMapper;
private final TaskService taskService;
private final CurrentUserService currentUserService;

private final ValidateModelConfigHandler validateModelConfigHandler;

@PostConstruct
void init() {
taskService.addResponseHandler(validateModelConfigHandler);
}

@PostMapping("/test")
@Secured(Roles.USER)
@Operation(summary = "Dispatch a model configuration validation task")
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "Dispatched successfully",
content =
@Content(
mediaType = "application/json",
schema =
@io.swagger.v3.oas.annotations.media.Schema(
implementation = TaskResponse.class))),
@ApiResponse(responseCode = "400", description = "Invalid input or bad request", content = @Content),
@ApiResponse(
responseCode = "500",
description = "There was an issue dispatching the request",
content = @Content)
})
public ResponseEntity<TaskResponse> createValidationRequest(@RequestBody final JsonNode input) {

try {
final TaskRequest taskRequest = new TaskRequest();
taskRequest.setType(TaskType.FUNMAN);
taskRequest.setScript(ValidateModelConfigHandler.NAME);
taskRequest.setUserId(currentUserService.get().getId());
taskRequest.setInput(objectMapper.writeValueAsBytes(input));

// TODO:
// - create Simulation for tracking
// - mark Simulation as pending
final UUID uuid = UUID.randomUUID();
final ValidateModelConfigHandler.Properties props = new ValidateModelConfigHandler.Properties();
props.setSimulationId(uuid);
taskRequest.setAdditionalProperties(props);

return ResponseEntity.ok().body(taskService.runTask(TaskMode.ASYNC, taskRequest));
} catch (final ResponseStatusException e) {
throw e;
} catch (final Exception e) {
final String error = "Unable to dispatch task request";
throw new ResponseStatusException(org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR, error);
}
}

// The methods below are depreacated
private final FunmanProxy funmanProxy;

@GetMapping("/{queryId}/halt")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public static enum TaskType {
@JsonAlias("gollm")
GOLLM("gollm"),
@JsonAlias("mira")
MIRA("mira");
MIRA("mira"),
@JsonAlias("funman")
FUNMAN("funman");

private final String value;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package software.uncharted.terarium.hmiserver.service.tasks;

import java.util.UUID;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import software.uncharted.terarium.hmiserver.models.task.TaskResponse;

@Component
@RequiredArgsConstructor
@Slf4j
public class ValidateModelConfigHandler extends TaskResponseHandler {
public static final String NAME = "funman_task:validate_modelconfig";

@Override
public String getName() {
return NAME;
}

@Data
public static class Properties {
UUID simulationId;
}

@Override
public TaskResponse onSuccess(final TaskResponse resp) {
try {
final Properties props = resp.getAdditionalProperties(Properties.class);
final UUID simulationId = props.getSimulationId();

// TODO:
// - Retrive final result json
// - Upload final result into S3
// - Mark simulation as completed, update result file
System.out.println("");
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " + simulationId.toString());
System.out.println("");
} catch (final Exception e) {
throw new RuntimeException(e);
}
return resp;
}
}

0 comments on commit 818aa5f

Please sign in to comment.