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 by system
  • Loading branch information
guqing committed Jul 1, 2024
1 parent 3875251 commit efaee42
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 efaee42

Please sign in to comment.