Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Prevent queue backups by blocking for job completion #50

Merged
merged 2 commits into from May 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,5 +1,7 @@
package net.pl3x.map.plugin.task.render;

import java.util.ArrayList;
import net.pl3x.map.plugin.Logger;
import net.pl3x.map.plugin.data.ChunkCoordinate;
import net.pl3x.map.plugin.data.Image;
import net.pl3x.map.plugin.data.MapWorld;
Expand Down Expand Up @@ -32,21 +34,27 @@ public int totalRegions() {

@Override
protected void render() {

long time = System.currentTimeMillis();
final Set<ChunkCoordinate> chunks = new HashSet<>();
while (mapWorld.hasModifiedChunks() && chunks.size() < mapWorld.config().BACKGROUND_RENDER_MAX_CHUNKS_PER_INTERVAL) {
chunks.add(mapWorld.nextModifiedChunk());
}
final Map<Region, List<ChunkCoordinate>> coordMap = chunks.stream().collect(Collectors.groupingBy(ChunkCoordinate::regionCoordinate));

List<CompletableFuture<Void>> futures = new ArrayList<>();
coordMap.forEach((region, chunkCoords) -> {
final Image img = new Image(region, worldTilesDir, mapWorld.config().ZOOM_MAX);

final CompletableFuture<Void> future = CompletableFuture.allOf(chunkCoords.stream().map(coord ->
mapSingleChunk(img, coord.getX(), coord.getZ())).toArray(CompletableFuture[]::new));

future.whenComplete((result, throwable) -> mapWorld.saveImage(img));
futures.add(future);
});

if (!futures.isEmpty()) {
CompletableFuture.allOf(futures.toArray(CompletableFuture[]::new)).join();
Logger.debug(String.format("Finished background render cycle in %.2f seconds",
(double) (System.currentTimeMillis() - time) / 1000.0D));
}
}
}