Skip to content

Commit d1e626e

Browse files
committed
make world size calculate async
1 parent beae997 commit d1e626e

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/main/java/de/sldk/mc/metrics/WorldSize.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import de.sldk.mc.utils.PathFileSize;
44
import io.prometheus.client.Gauge;
5+
6+
import java.util.concurrent.CompletableFuture;
57
import java.util.logging.Logger;
68
import org.bukkit.World;
79
import org.bukkit.plugin.Plugin;
@@ -27,13 +29,17 @@ protected void clear() {
2729

2830
@Override
2931
public void collect(World world) {
30-
try {
31-
PathFileSize pathUtils = new PathFileSize(world.getWorldFolder().toPath());
32-
long size = pathUtils.getSize();
32+
CompletableFuture.supplyAsync(() -> {
33+
try {
34+
PathFileSize pathUtils = new PathFileSize(world.getWorldFolder().toPath());
35+
return pathUtils.getSize();
36+
} catch (Throwable t) {
37+
log.throwing(this.getClass().getSimpleName(), "collect", t);
38+
}
39+
return 0L;
40+
}).thenAccept(size -> {
3341
String worldName = world.getName();
3442
WORLD_SIZE.labels(worldName).set(size);
35-
} catch (Throwable t) {
36-
log.throwing(this.getClass().getSimpleName(), "collect", t);
37-
}
43+
});
3844
}
3945
}

src/test/java/de/sldk/mc/metrics/WorldSizeTest.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,21 @@ public void doesNotPopulateMetricIfFileDoesNotExist() {
5050
}
5151

5252
@Test
53-
public void setsMetricWithCorrectNameAndLabel() {
53+
public void setsMetricWithCorrectNameAndLabel() throws InterruptedException {
5454
String worldName = new RandomString(10).nextString();
5555
givenWorldFileExists(worldName);
5656
worldSizeMetric.collect(world);
57+
Thread.sleep(500);
5758
assertNotNull(getMetricValue(worldName));
5859
}
5960

6061
@Test
61-
public void setsCorrectWorldSizeValue() throws IOException {
62+
public void setsCorrectWorldSizeValue() throws IOException, InterruptedException {
6263
String worldName = new RandomString(10).nextString();
6364
int worldSize = new Random().ints(128, 1024).findFirst().getAsInt();
6465
givenWorldFileExists(worldName, worldSize);
6566
worldSizeMetric.collect(world);
67+
Thread.sleep(500);
6668
Double value = getMetricValue(worldName);
6769
assertEquals(worldSize, value.longValue());
6870
}

0 commit comments

Comments
 (0)