Skip to content

Commit

Permalink
refactor: provide APIs for change plugin motion status synchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
guqing committed Oct 18, 2023
1 parent 493c599 commit a64a0b9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.function.Predicate;
import java.util.function.Supplier;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.reactivestreams.Publisher;
import org.springdoc.webflux.core.fn.SpringdocRouteBuilder;
Expand Down Expand Up @@ -201,6 +202,27 @@ public RouterFunction<ServerResponse> endpoint() {
.response(responseBuilder()
.implementation(Plugin.class))
)
.PUT("plugins/{name}/motion-status", this::changeMotionStatus,
builder -> builder.operationId("ChangePluginMotionStatus")
.description("Change the motion status of a plugin by name.")
.tag(tag)
.parameter(parameterBuilder()
.name("name")
.in(ParameterIn.PATH)
.required(true)
.implementation(String.class)
)
.requestBody(requestBodyBuilder()
.required(true)
.content(contentBuilder()
.mediaType(MediaType.APPLICATION_JSON_VALUE)
.schema(schemaBuilder()
.implementation(MotionStatusRequest.class))
)
)
.response(responseBuilder()
.implementation(Plugin.class))
)
.GET("plugins", this::list, builder -> {
builder.operationId("ListPlugins")
.tag(tag)
Expand Down Expand Up @@ -255,6 +277,56 @@ public RouterFunction<ServerResponse> endpoint() {
.build();
}

Mono<ServerResponse> changeMotionStatus(ServerRequest request) {
final var name = request.pathVariable("name");
return request.bodyToMono(MotionStatusRequest.class)
.flatMap(motionStatus -> {
var enabled = switch (motionStatus.getAction()) {
case START -> true;
case STOP -> false;
};
return client.get(Plugin.class, name)
.flatMap(plugin -> {
plugin.getSpec().setEnabled(enabled);
return client.update(plugin);
})
.flatMap(plugin -> {
if (motionStatus.isAsync()) {
return Mono.just(plugin);
}
return waitForPluginToMeetExpectedState(name, p ->
enabled == p.getSpec().getEnabled());
});
})
.flatMap(plugin -> ServerResponse.ok().bodyValue(plugin));
}

Mono<Plugin> waitForPluginToMeetExpectedState(String name, Predicate<Plugin> predicate) {
return Mono.defer(() -> client.get(Plugin.class, name)
.map(plugin -> {
if (predicate.test(plugin)) {
return plugin;
}
throw new IllegalStateException("Plugin " + name + " is not in expected state");
})
)
.retryWhen(Retry.backoff(20, Duration.ofMillis(100))
.filter(IllegalStateException.class::isInstance)
);
}

@Data
@Schema(name = "PluginMotionStatusRequest")
static class MotionStatusRequest {
private ExpectedAction action;
private boolean async;

enum ExpectedAction {
START,
STOP
}
}

private Mono<ServerResponse> fetchJsBundle(ServerRequest request) {
Optional<String> versionOption = request.queryParam("v");
return versionOption.map(s ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rules:
verbs: [ "create", "patch", "update", "delete", "deletecollection" ]
- apiGroups: [ "api.console.halo.run" ]
resources: [ "plugins/upgrade", "plugins/resetconfig", "plugins/config", "plugins/reload",
"plugins/install-from-uri", "plugins/upgrade-from-uri" ]
"plugins/install-from-uri", "plugins/upgrade-from-uri", "plugins/motion-status" ]
verbs: [ "*" ]
- apiGroups: [ "api.console.halo.run" ]
resources: [ "plugin-presets" ]
Expand Down

0 comments on commit a64a0b9

Please sign in to comment.