Skip to content

Commit

Permalink
refactor: provide API for change plugin motion status synchronously (#…
Browse files Browse the repository at this point in the history
…4745)

#### What type of PR is this?
/kind improvement
/area core
/area console

#### What this PR does / why we need it:
提供允许同步更改插件运行状态的 API

#### Which issue(s) this PR fixes:
Fixes #4744

#### Does this PR introduce a user-facing change?
```release-note
提供允许同步更改插件运行状态的 API
```
  • Loading branch information
guqing authored Oct 23, 2023
1 parent 21e975d commit 772c7fc
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
import java.util.function.Predicate;
import java.util.function.Supplier;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.pf4j.PluginState;
import org.reactivestreams.Publisher;
import org.springdoc.webflux.core.fn.SpringdocRouteBuilder;
import org.springframework.beans.factory.DisposableBean;
Expand Down Expand Up @@ -201,6 +203,27 @@ public RouterFunction<ServerResponse> endpoint() {
.response(responseBuilder()
.implementation(Plugin.class))
)
.PUT("plugins/{name}/plugin-state", this::changePluginRunningState,
builder -> builder.operationId("ChangePluginRunningState")
.description("Change the running state 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(RunningStateRequest.class))
)
)
.response(responseBuilder()
.implementation(Plugin.class))
)
.GET("plugins", this::list, builder -> {
builder.operationId("ListPlugins")
.tag(tag)
Expand Down Expand Up @@ -255,6 +278,56 @@ public RouterFunction<ServerResponse> endpoint() {
.build();
}

Mono<ServerResponse> changePluginRunningState(ServerRequest request) {
final var name = request.pathVariable("name");
return request.bodyToMono(RunningStateRequest.class)
.flatMap(runningState -> {
final var enable = runningState.isEnable();
return client.get(Plugin.class, name)
.flatMap(plugin -> {
plugin.getSpec().setEnabled(enable);
return client.update(plugin);
})
.flatMap(plugin -> {
if (runningState.isAsync()) {
return Mono.just(plugin);
}
return waitForPluginToMeetExpectedState(name, p -> {
// when enabled = true,excepted phase = started || failed
// when enabled = false,excepted phase = !started
var phase = p.statusNonNull().getPhase();
if (enable) {
return PluginState.STARTED.equals(phase)
|| PluginState.FAILED.equals(phase);
}
return !PluginState.STARTED.equals(phase);
});
});
})
.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(10, Duration.ofMillis(100))
.filter(IllegalStateException.class::isInstance)
);
}

@Data
@Schema(name = "PluginRunningStateRequest")
static class RunningStateRequest {
private boolean enable;
private boolean async;
}

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/plugin-state" ]
verbs: [ "*" ]
- apiGroups: [ "api.console.halo.run" ]
resources: [ "plugin-presets" ]
Expand Down

0 comments on commit 772c7fc

Please sign in to comment.