Skip to content

Commit

Permalink
fix: code app api corrections (#557)
Browse files Browse the repository at this point in the history
  • Loading branch information
artsiomkorzun authored Oct 31, 2024
1 parent d05140d commit 03c49ba
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ public static class Function {
private Map<String, String> env;

public enum Status {
CREATED, STARTING, STOPPING, STARTED, STOPPED, FAILED;
DEPLOYING, UNDEPLOYING, DEPLOYED, UNDEPLOYED, FAILED;

public boolean isPending() {
return switch (this) {
case CREATED, STARTED, FAILED, STOPPED -> false;
case STARTING, STOPPING -> true;
case DEPLOYED, FAILED, UNDEPLOYED -> false;
case DEPLOYING, UNDEPLOYING -> true;
};
}

public boolean isActive() {
return switch (this) {
case CREATED, FAILED, STOPPED -> false;
case STARTING, STARTED, STOPPING -> true;
case FAILED, UNDEPLOYED -> false;
case DEPLOYING, DEPLOYED, UNDEPLOYING -> true;
};
}
}
Expand All @@ -58,7 +58,7 @@ public boolean isActive() {
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public static class Mapping {
private String completion;
private String chatCompletion;
private String rate;
private String tokenize;
private String truncatePrompt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,29 +85,29 @@ public Future<?> getApplications() {
return Future.succeededFuture();
}

public Future<?> startApplication() {
public Future<?> deployApplication() {
context.getRequest()
.body()
.compose(body -> {
String url = ProxyUtil.convertToObject(body, ResourceLink.class).url();
ResourceDescriptor resource = decodeUrl(url);
checkAccess(resource);
return vertx.executeBlocking(() -> applicationService.startApplication(context, resource), false);
return vertx.executeBlocking(() -> applicationService.deployApplication(context, resource), false);
})
.onSuccess(application -> context.respond(HttpStatus.OK, application))
.onFailure(this::respondError);

return Future.succeededFuture();
}

public Future<?> stopApplication() {
public Future<?> undeployApplication() {
context.getRequest()
.body()
.compose(body -> {
String url = ProxyUtil.convertToObject(body, ResourceLink.class).url();
ResourceDescriptor resource = decodeUrl(url);
checkAccess(resource);
return vertx.executeBlocking(() -> applicationService.stopApplication(resource), false);
return vertx.executeBlocking(() -> applicationService.undeployApplication(resource), false);
})
.onSuccess(application -> context.respond(HttpStatus.OK, application))
.onFailure(this::respondError);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ControllerSelector {

private static final Pattern PATTERN_APPLICATION = Pattern.compile("^/+openai/applications/(?<id>.+?)$");
private static final Pattern PATTERN_APPLICATIONS = Pattern.compile("^/+openai/applications$");
private static final Pattern APPLICATIONS = Pattern.compile("^/v1/ops/application/(start|stop|logs)$");
private static final Pattern APPLICATIONS = Pattern.compile("^/v1/ops/application/(deploy|undeploy|logs)$");

private static final Pattern PATTERN_BUCKET = Pattern.compile("^/v1/bucket$");

Expand Down Expand Up @@ -334,8 +334,8 @@ private static Controller selectPost(Proxy proxy, ProxyContext context, String p
ApplicationController controller = new ApplicationController(context);

return switch (operation) {
case "start" -> controller::startApplication;
case "stop" -> controller::stopApplication;
case "deploy" -> controller::deployApplication;
case "undeploy" -> controller::undeployApplication;
case "logs" -> controller::getApplicationLogs;
default -> null;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public Pair<ResourceItemMetadata, Application> putApplication(ResourceDescriptor

function.setId(UrlUtil.encodePathSegment(idGenerator.get()));
function.setAuthorBucket(resource.getBucketName());
function.setStatus(Application.Function.Status.CREATED);
function.setStatus(Application.Function.Status.UNDEPLOYED);
function.setTargetFolder(encodeTargetFolder(resource, function.getId()));
} else {
if (isPublicOrReview(resource) && !function.getSourceFolder().equals(existing.getFunction().getSourceFolder())) {
Expand Down Expand Up @@ -291,7 +291,7 @@ public void copyApplication(ResourceDescriptor source, ResourceDescriptor destin
if (function != null) {
if (existing == null || existing.getFunction() == null) {
function.setId(UrlUtil.encodePathSegment(idGenerator.get()));
function.setStatus(Application.Function.Status.CREATED);
function.setStatus(Application.Function.Status.UNDEPLOYED);
function.setTargetFolder(encodeTargetFolder(destination, function.getId()));

if (isPublicOrReview) {
Expand Down Expand Up @@ -327,7 +327,7 @@ public void copyApplication(ResourceDescriptor source, ResourceDescriptor destin
}
}

public Application startApplication(ProxyContext context, ResourceDescriptor resource) {
public Application deployApplication(ProxyContext context, ResourceDescriptor resource) {
verifyApplication(resource);
controller.verifyActive();

Expand All @@ -346,7 +346,7 @@ public Application startApplication(ProxyContext context, ResourceDescriptor res
throw new HttpException(HttpStatus.CONFLICT, "Application must be stopped: " + resource.getUrl());
}

application.getFunction().setStatus(Application.Function.Status.STARTING);
application.getFunction().setStatus(Application.Function.Status.DEPLOYING);
application.getFunction().setError(null);

result.setValue(application);
Expand All @@ -361,7 +361,7 @@ public Application startApplication(ProxyContext context, ResourceDescriptor res
return result.getValue();
}

public Application stopApplication(ResourceDescriptor resource) {
public Application undeployApplication(ResourceDescriptor resource) {
verifyApplication(resource);
controller.verifyActive();

Expand All @@ -376,7 +376,7 @@ public Application stopApplication(ResourceDescriptor resource) {
throw new HttpException(HttpStatus.CONFLICT, "Application does not have function: " + resource.getUrl());
}

if (application.getFunction().getStatus() != Application.Function.Status.STARTED) {
if (application.getFunction().getStatus() != Application.Function.Status.DEPLOYED) {
throw new HttpException(HttpStatus.CONFLICT, "Application is not started: " + resource.getUrl());
}

Expand All @@ -385,7 +385,7 @@ public Application stopApplication(ResourceDescriptor resource) {
application.getFeatures().setTokenizeEndpoint(null);
application.getFeatures().setTruncatePromptEndpoint(null);
application.getFeatures().setConfigurationEndpoint(null);
application.getFunction().setStatus(Application.Function.Status.STOPPING);
application.getFunction().setStatus(Application.Function.Status.UNDEPLOYING);

result.setValue(application);
pendingApplications.add(System.currentTimeMillis() + checkDelay, resource.getUrl());
Expand All @@ -403,7 +403,7 @@ public Application.Logs getApplicationLogs(ResourceDescriptor resource) {

Application application = getApplication(resource).getValue();

if (application.getFunction() == null || application.getFunction().getStatus() != Application.Function.Status.STARTED) {
if (application.getFunction() == null || application.getFunction().getStatus() != Application.Function.Status.DEPLOYED) {
throw new HttpException(HttpStatus.CONFLICT, "Application is not started: " + resource.getUrl());
}

Expand Down Expand Up @@ -451,7 +451,7 @@ private void prepareApplication(ResourceDescriptor resource, Application applica
throw new IllegalArgumentException("Application function mapping must be provided");
}

verifyMapping(function.getMapping().getCompletion(), true, "Application completion mapping is missing/invalid");
verifyMapping(function.getMapping().getChatCompletion(), true, "Application chat_completion mapping is missing/invalid");
verifyMapping(function.getMapping().getRate(), false, "Application rate mapping is invalid");
verifyMapping(function.getMapping().getTokenize(), false, "Application tokenize mapping is invalid");
verifyMapping(function.getMapping().getTruncatePrompt(), false, "Application truncate_prompt mapping is invalid");
Expand Down Expand Up @@ -513,7 +513,7 @@ private Void launchApplication(ProxyContext context, ResourceDescriptor resource
throw new IllegalStateException("Application has no function");
}

if (function.getStatus() != Application.Function.Status.STARTING) {
if (function.getStatus() != Application.Function.Status.DEPLOYING) {
throw new IllegalStateException("Application is not starting");
}

Expand All @@ -532,9 +532,9 @@ private Void launchApplication(ProxyContext context, ResourceDescriptor resource
throw new IllegalStateException("Application function has been updated");
}

function.setStatus(Application.Function.Status.STARTED);
function.setStatus(Application.Function.Status.DEPLOYED);
existing.setFunction(function);
existing.setEndpoint(buildMapping(endpoint, function.getMapping().getCompletion()));
existing.setEndpoint(buildMapping(endpoint, function.getMapping().getChatCompletion()));
existing.getFeatures().setRateEndpoint(buildMapping(endpoint, function.getMapping().getRate()));
existing.getFeatures().setTokenizeEndpoint(buildMapping(endpoint, function.getMapping().getTokenize()));
existing.getFeatures().setTruncatePromptEndpoint(buildMapping(endpoint, function.getMapping().getTruncatePrompt()));
Expand Down Expand Up @@ -583,8 +583,8 @@ private Void terminateApplication(ResourceDescriptor resource, String error) {
throw new IllegalStateException("Application function has been updated");
}

Application.Function.Status status = (function.getStatus() == Application.Function.Status.STOPPING)
? Application.Function.Status.STOPPED
Application.Function.Status status = (function.getStatus() == Application.Function.Status.UNDEPLOYING)
? Application.Function.Status.UNDEPLOYED
: Application.Function.Status.FAILED;

function.setStatus(status);
Expand Down
Loading

0 comments on commit 03c49ba

Please sign in to comment.