Add uncaught exception handlers to executor services

This commit is contained in:
Nassim Jahnke 2023-04-27 14:18:11 +02:00
parent dfb55b8149
commit 91a872370e
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
3 changed files with 38 additions and 3 deletions

View File

@ -6,6 +6,7 @@ import io.papermc.hangar.model.db.JobTable;
import io.papermc.hangar.model.internal.job.Job;
import io.papermc.hangar.model.internal.job.JobException;
import io.papermc.hangar.model.internal.job.SendMailJob;
import io.papermc.hangar.util.ThreadFactory;
import jakarta.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.ExecutorService;
@ -35,7 +36,7 @@ public class JobService extends HangarComponent {
@PostConstruct
public void initThreadPool() {
this.executorService = new ThreadPoolExecutor(1, this.config.jobs.maxConcurrentJobs(), 60, TimeUnit.SECONDS, new SynchronousQueue<>());
this.executorService = new ThreadPoolExecutor(1, this.config.jobs.maxConcurrentJobs(), 60, TimeUnit.SECONDS, new SynchronousQueue<>(), new ThreadFactory("JobService", false));
}
public void checkAndProcess() {

View File

@ -23,6 +23,7 @@ import io.papermc.hangar.scanner.model.Severity;
import io.papermc.hangar.service.internal.file.FileService;
import io.papermc.hangar.service.internal.uploads.ProjectFiles;
import io.papermc.hangar.service.internal.users.UserService;
import io.papermc.hangar.util.ThreadFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Comparator;
@ -40,13 +41,12 @@ import org.springframework.context.event.EventListener;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import software.amazon.awssdk.utils.ThreadFactoryBuilder;
@Service
public class JarScanningService {
public static final UUID JAR_SCANNER_USER = new UUID(952332837L, -376012533328L);
private static final ExecutorService EXECUTOR_SERVICE = Executors.newCachedThreadPool(new ThreadFactoryBuilder().threadNamePrefix("Scanner").build());
private static final ExecutorService EXECUTOR_SERVICE = Executors.newCachedThreadPool(new ThreadFactory("Scanner", true));
private static final Logger LOGGER = LoggerFactory.getLogger(JarScanningService.class);
private final HangarJarScanner scanner = new HangarJarScanner();
private final JarScanResultDAO dao;

View File

@ -0,0 +1,34 @@
package io.papermc.hangar.util;
import io.sentry.Sentry;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import org.jetbrains.annotations.NotNull;
public class ThreadFactory implements java.util.concurrent.ThreadFactory {
private static final AtomicLong POOL_COUNTER = new AtomicLong(0L);
private final java.util.concurrent.ThreadFactory delegate = Executors.defaultThreadFactory();
private final AtomicInteger threadCounter = new AtomicInteger(0);
private final boolean daemonThreads;
private final String name;
public ThreadFactory(final String name, final boolean daemonThreads) {
this.name = name + "-" + POOL_COUNTER.getAndIncrement() % 10_000;
this.daemonThreads = daemonThreads;
}
@Override
public Thread newThread(final @NotNull Runnable runnable) {
final Thread thread = this.delegate.newThread(runnable);
thread.setName(this.name + "-" + this.threadCounter.getAndIncrement());
thread.setUncaughtExceptionHandler((t, e) -> {
e.printStackTrace();
Sentry.captureException(e);
});
if (this.daemonThreads) {
thread.setDaemon(true);
}
return thread;
}
}