-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: optimize theme and plugin config retrieval and saving
- Loading branch information
Showing
9 changed files
with
177 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
application/src/main/java/run/halo/app/core/extension/service/SettingConfigService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package run.halo.app.core.extension.service; | ||
|
||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import reactor.core.publisher.Mono; | ||
import run.halo.app.core.extension.Setting; | ||
import run.halo.app.extension.ConfigMap; | ||
|
||
/** | ||
* {@link Setting} related {@link ConfigMap} service. | ||
* | ||
* @author guqing | ||
* @since 2.20.0 | ||
*/ | ||
public interface SettingConfigService { | ||
|
||
Mono<Void> upsertConfig(String configMapName, ObjectNode configJsonData); | ||
|
||
Mono<ObjectNode> fetchConfig(String configMapName); | ||
} |
57 changes: 57 additions & 0 deletions
57
...tion/src/main/java/run/halo/app/core/extension/service/impl/SettingConfigServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package run.halo.app.core.extension.service.impl; | ||
|
||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import java.time.Duration; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.dao.OptimisticLockingFailureException; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.Assert; | ||
import reactor.core.publisher.Mono; | ||
import reactor.util.retry.Retry; | ||
import run.halo.app.core.extension.Setting; | ||
import run.halo.app.core.extension.service.SettingConfigService; | ||
import run.halo.app.core.extension.theme.SettingUtils; | ||
import run.halo.app.extension.ConfigMap; | ||
import run.halo.app.extension.Metadata; | ||
import run.halo.app.extension.ReactiveExtensionClient; | ||
|
||
/** | ||
* {@link Setting} related {@link ConfigMap} service implementation. | ||
* | ||
* @author guqing | ||
* @since 2.20.0 | ||
*/ | ||
@Component | ||
@RequiredArgsConstructor | ||
public class SettingConfigServiceImpl implements SettingConfigService { | ||
private final ReactiveExtensionClient client; | ||
|
||
@Override | ||
public Mono<Void> upsertConfig(String configMapName, ObjectNode configJsonData) { | ||
Assert.notNull(configMapName, "Config map name must not be null"); | ||
Assert.notNull(configJsonData, "Config json data must not be null"); | ||
var data = SettingUtils.settingConfigJsonToMap(configJsonData); | ||
return Mono.defer(() -> client.fetch(ConfigMap.class, configMapName) | ||
.flatMap(persisted -> { | ||
persisted.setData(data); | ||
return client.update(persisted); | ||
})) | ||
.retryWhen(Retry.backoff(5, Duration.ofMillis(300)) | ||
.filter(OptimisticLockingFailureException.class::isInstance) | ||
) | ||
.switchIfEmpty(Mono.defer(() -> { | ||
var configMap = new ConfigMap(); | ||
configMap.setMetadata(new Metadata()); | ||
configMap.getMetadata().setName(configMapName); | ||
configMap.setData(data); | ||
return client.create(configMap); | ||
})) | ||
.then(); | ||
} | ||
|
||
@Override | ||
public Mono<ObjectNode> fetchConfig(String configMapName) { | ||
return client.fetch(ConfigMap.class, configMapName) | ||
.map(SettingUtils::settingConfigToJson); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.