mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-09-12 06:48:50 +02:00
Added exception logging to the Processing system
This commit is contained in:
parent
2476c75b6c
commit
f0f4bf975b
@ -6,7 +6,6 @@ import com.djrapitops.plan.system.file.FileSystem;
|
|||||||
import com.djrapitops.plan.system.settings.Settings;
|
import com.djrapitops.plan.system.settings.Settings;
|
||||||
import com.djrapitops.plugin.api.utility.log.Log;
|
import com.djrapitops.plugin.api.utility.log.Log;
|
||||||
import com.djrapitops.plugin.utilities.Verify;
|
import com.djrapitops.plugin.utilities.Verify;
|
||||||
import com.google.common.cache.Cache;
|
|
||||||
import com.maxmind.geoip2.DatabaseReader;
|
import com.maxmind.geoip2.DatabaseReader;
|
||||||
import com.maxmind.geoip2.exception.GeoIp2Exception;
|
import com.maxmind.geoip2.exception.GeoIp2Exception;
|
||||||
import com.maxmind.geoip2.model.CountryResponse;
|
import com.maxmind.geoip2.model.CountryResponse;
|
||||||
@ -28,8 +27,6 @@ import java.util.zip.GZIPInputStream;
|
|||||||
* This class contains the geolocation cache.
|
* This class contains the geolocation cache.
|
||||||
* <p>
|
* <p>
|
||||||
* It caches all IPs with their matching country.
|
* It caches all IPs with their matching country.
|
||||||
* <p>
|
|
||||||
* This cache uses the Google Guava {@link Cache}.
|
|
||||||
*
|
*
|
||||||
* @author Fuzzlemann
|
* @author Fuzzlemann
|
||||||
* @since 3.5.5
|
* @since 3.5.5
|
||||||
|
@ -9,10 +9,7 @@ import com.djrapitops.plugin.api.utility.log.Log;
|
|||||||
import com.djrapitops.plugin.utilities.Verify;
|
import com.djrapitops.plugin.utilities.Verify;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.Future;
|
|
||||||
|
|
||||||
public class Processing implements SubSystem {
|
public class Processing implements SubSystem {
|
||||||
|
|
||||||
@ -24,6 +21,7 @@ public class Processing implements SubSystem {
|
|||||||
criticalExecutor = Executors.newFixedThreadPool(2);
|
criticalExecutor = Executors.newFixedThreadPool(2);
|
||||||
saveInstance(nonCriticalExecutor);
|
saveInstance(nonCriticalExecutor);
|
||||||
saveInstance(criticalExecutor);
|
saveInstance(criticalExecutor);
|
||||||
|
saveInstance(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void submit(Runnable runnable) {
|
public static void submit(Runnable runnable) {
|
||||||
@ -40,27 +38,27 @@ public class Processing implements SubSystem {
|
|||||||
|
|
||||||
public static void submitNonCritical(Runnable runnable) {
|
public static void submitNonCritical(Runnable runnable) {
|
||||||
saveInstance(runnable);
|
saveInstance(runnable);
|
||||||
getInstance().nonCriticalExecutor.submit(runnable);
|
CompletableFuture.supplyAsync(() -> runnable, getInstance().nonCriticalExecutor)
|
||||||
|
.thenAccept(Runnable::run)
|
||||||
|
.handle(Processing::exceptionHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void submitCritical(Runnable runnable) {
|
public static void submitCritical(Runnable runnable) {
|
||||||
saveInstance(runnable);
|
saveInstance(runnable);
|
||||||
getInstance().criticalExecutor.submit(runnable);
|
CompletableFuture.supplyAsync(() -> runnable, getInstance().criticalExecutor)
|
||||||
|
.thenAccept(Runnable::run)
|
||||||
|
.handle(Processing::exceptionHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void submitNonCritical(Runnable... runnables) {
|
public static void submitNonCritical(Runnable... runnables) {
|
||||||
ExecutorService nonCriticalExecutor = getInstance().nonCriticalExecutor;
|
|
||||||
for (Runnable runnable : runnables) {
|
for (Runnable runnable : runnables) {
|
||||||
saveInstance(runnable);
|
submitNonCritical(runnable);
|
||||||
nonCriticalExecutor.submit(runnable);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void submitCritical(Runnable... runnables) {
|
public static void submitCritical(Runnable... runnables) {
|
||||||
ExecutorService criticalExecutor = getInstance().criticalExecutor;
|
|
||||||
for (Runnable runnable : runnables) {
|
for (Runnable runnable : runnables) {
|
||||||
saveInstance(runnable);
|
submitCritical(runnable);
|
||||||
criticalExecutor.submit(runnable);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,12 +72,35 @@ public class Processing implements SubSystem {
|
|||||||
|
|
||||||
public static <T> Future<T> submitNonCritical(Callable<T> task) {
|
public static <T> Future<T> submitNonCritical(Callable<T> task) {
|
||||||
saveInstance(task);
|
saveInstance(task);
|
||||||
return getInstance().nonCriticalExecutor.submit(task);
|
return CompletableFuture.supplyAsync(() -> task, getInstance().nonCriticalExecutor)
|
||||||
|
.thenApply(tCallable -> {
|
||||||
|
try {
|
||||||
|
return tCallable.call();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IllegalStateException(e);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.handle(Processing::exceptionHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> T exceptionHandler(T t, Throwable throwable) {
|
||||||
|
if (throwable != null) {
|
||||||
|
Log.toLog(Processing.class, throwable.getCause());
|
||||||
|
}
|
||||||
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> Future<T> submitCritical(Callable<T> task) {
|
public static <T> Future<T> submitCritical(Callable<T> task) {
|
||||||
saveInstance(task);
|
saveInstance(task);
|
||||||
return getInstance().criticalExecutor.submit(task);
|
return CompletableFuture.supplyAsync(() -> task, getInstance().criticalExecutor)
|
||||||
|
.thenApply(tCallable -> {
|
||||||
|
try {
|
||||||
|
return tCallable.call();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IllegalStateException(e);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.handle(Processing::exceptionHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Processing getInstance() {
|
public static Processing getInstance() {
|
||||||
|
48
Plan/src/test/java/com/djrapitops/plan/system/cache/GeolocationCacheTest.java
vendored
Normal file
48
Plan/src/test/java/com/djrapitops/plan/system/cache/GeolocationCacheTest.java
vendored
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package com.djrapitops.plan.system.cache;
|
||||||
|
|
||||||
|
import com.djrapitops.plan.Plan;
|
||||||
|
import com.djrapitops.plan.api.exceptions.EnableException;
|
||||||
|
import com.djrapitops.plan.system.BukkitSystem;
|
||||||
|
import com.djrapitops.plugin.StaticHolder;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.ClassRule;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.TemporaryFolder;
|
||||||
|
import utilities.mocks.BukkitMockUtil;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for GeolocationCache.
|
||||||
|
*
|
||||||
|
* @author Rsl1122
|
||||||
|
*/
|
||||||
|
public class GeolocationCacheTest {
|
||||||
|
|
||||||
|
@ClassRule
|
||||||
|
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||||
|
private static Plan planMock;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setUpClass() throws Exception {
|
||||||
|
BukkitMockUtil mockUtil = BukkitMockUtil.setUp()
|
||||||
|
.withDataFolder(temporaryFolder.getRoot())
|
||||||
|
.withLogging()
|
||||||
|
.withPluginDescription()
|
||||||
|
.withResourceFetchingFromJar()
|
||||||
|
.withServer();
|
||||||
|
planMock = mockUtil.getPlanMock();
|
||||||
|
StaticHolder.saveInstance(GeolocationCacheTest.class, planMock.getClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGeolocationCache() throws EnableException {
|
||||||
|
BukkitSystem system = new BukkitSystem(planMock);
|
||||||
|
system.enable();
|
||||||
|
|
||||||
|
String expected = "Germany";
|
||||||
|
String result = GeolocationCache.getCountry("141.52.255.1");
|
||||||
|
assertEquals(expected, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user