Skip to content

Commit

Permalink
fix: handle plugin entry file loading when cache temp directory is cl…
Browse files Browse the repository at this point in the history
…eared (halo-dev#6238)

#### What type of PR is this?
/kind bug
/area plugin
/milestone 2.17.x

#### What this PR does / why we need it:
修复当插件入口文件的缓存目录被系统清理后会导致一直无法加载的问题

原问题复现步骤:
1. 登录后刷新页面,此时缓存目录被创建
2. 删除缓存目录后就会提示文件不存在然后导致插件入口文件一致无法加载直到重启 Halo

#### Which issue(s) this PR fixes:
Fixes halo-dev#6226

#### Does this PR introduce a user-facing change?
```release-note
修复当插件入口文件的缓存目录被系统清理后会导致一直无法加载的问题
```
  • Loading branch information
guqing authored Jul 1, 2024
1 parent cc3564b commit b9c500d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -549,15 +549,22 @@ Mono<Resource> computeIfAbsent(String version, Publisher<DataBuffer> content) {
// double check of the resource
.filter(res -> isResourceMatch(res, newFilename))
.switchIfEmpty(Mono.using(
() -> tempDir.resolve(newFilename),
() -> {
if (!Files.exists(tempDir)) {
Files.createDirectories(tempDir);
}
return tempDir.resolve(newFilename);
},
path -> DataBufferUtils.write(content, path,
CREATE, TRUNCATE_EXISTING)
.then(Mono.<Resource>fromSupplier(
() -> new FileSystemResource(path)
)),
path -> {
// clean up old resource
cleanUp(this.resource);
if (shouldCleanUp(path)) {
// clean up old resource
cleanUp(this.resource);
}
})
.subscribeOn(scheduler)
.doOnNext(newResource -> this.resource = newResource)
Expand All @@ -579,6 +586,18 @@ Mono<Resource> computeIfAbsent(String version, Publisher<DataBuffer> content) {
});
}

private boolean shouldCleanUp(Path newPath) {
if (this.resource == null || !this.resource.exists()) {
return false;
}
try {
var oldPath = this.resource.getFile().toPath();
return !oldPath.equals(newPath);
} catch (IOException e) {
return false;
}
}

private static void cleanUp(Resource resource) {
if (resource instanceof WritableResource wr
&& wr.isWritable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.pf4j.PluginDescriptor;
import org.pf4j.PluginWrapper;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.util.FileSystemUtils;
import org.springframework.web.server.ServerWebInputException;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
Expand Down Expand Up @@ -391,6 +392,24 @@ void shouldComputeBundleFileIfAbsent() {
}
})
.verifyComplete();

try {
FileSystemUtils.deleteRecursively(tempDir);
} catch (IOException e) {
throw new RuntimeException(e);
}
cache.computeIfAbsent("fake-version", fakeContent)
.as(StepVerifier::create)
.assertNext(resource -> {
try {
assertThat(Files.exists(tempDir)).isTrue();
assertEquals(tempDir.resolve("different-version.js"),
resource.getFile().toPath());
} catch (IOException e) {
throw new RuntimeException(e);
}
})
.verifyComplete();
}

@Test
Expand Down

0 comments on commit b9c500d

Please sign in to comment.