shutdown logback manually

This commit is contained in:
Sebastian Stenzel 2019-12-11 13:53:32 +01:00
parent ac536ba125
commit 1a4d1fffb3
No known key found for this signature in database
GPG Key ID: 667B866EA8240A09
3 changed files with 61 additions and 17 deletions

4
.idea/compiler.xml generated
View File

@ -31,10 +31,10 @@
<entry name="$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0.jar" /> <entry name="$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0.jar" />
<entry name="$MAVEN_REPOSITORY$/org/jetbrains/kotlinx/kotlinx-metadata-jvm/0.1.0/kotlinx-metadata-jvm-0.1.0.jar" /> <entry name="$MAVEN_REPOSITORY$/org/jetbrains/kotlinx/kotlinx-metadata-jvm/0.1.0/kotlinx-metadata-jvm-0.1.0.jar" />
</processorPath> </processorPath>
<module name="commons" />
<module name="keychain" /> <module name="keychain" />
<module name="ui" />
<module name="launcher" /> <module name="launcher" />
<module name="commons" />
<module name="ui" />
</profile> </profile>
</annotationProcessing> </annotationProcessing>
</component> </component>

View File

@ -12,15 +12,18 @@ import org.slf4j.LoggerFactory;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.PriorityBlockingQueue;
@Singleton @Singleton
public class ShutdownHook extends Thread { public class ShutdownHook extends Thread {
private static final int PRIO_VERY_LAST = Integer.MIN_VALUE;
public static final int PRIO_LAST = PRIO_VERY_LAST + 1;
public static final int PRIO_DEFAULT = 0;
public static final int PRIO_FIRST = Integer.MAX_VALUE;
private static final Logger LOG = LoggerFactory.getLogger(ShutdownHook.class); private static final Logger LOG = LoggerFactory.getLogger(ShutdownHook.class);
private static final Runnable POISON = Runnables.doNothing(); private static final OrderedTask POISON = new OrderedTask(PRIO_VERY_LAST, Runnables.doNothing());
private final Queue<OrderedTask> tasks = new PriorityBlockingQueue<>();
private final Queue<Runnable> tasks = new ConcurrentLinkedQueue<>();
@Inject @Inject
ShutdownHook() { ShutdownHook() {
@ -43,8 +46,51 @@ public class ShutdownHook extends Thread {
} }
} }
/**
* Schedules a task to be run during shutdown with default order
*
* @param task The task to be scheduled
*/
public void runOnShutdown(Runnable task) { public void runOnShutdown(Runnable task) {
tasks.add(task); runOnShutdown(PRIO_DEFAULT, task);
} }
/**
* Schedules a task to be run with the given priority
*
* @param priority Tasks with high priority will be run before task with lower priority
* @param task The task to be scheduled
*/
public void runOnShutdown(int priority, Runnable task) {
tasks.add(new OrderedTask(priority, task));
}
private static class OrderedTask implements Comparable<OrderedTask>, Runnable {
private final int priority;
private final Runnable task;
public OrderedTask(int priority, Runnable task) {
this.priority = priority;
this.task = task;
}
@Override
public int compareTo(OrderedTask other) {
// overflow-safe signum impl:
if (this.priority > other.priority) {
return -1; // higher prio -> this before other
} else if (this.priority < other.priority) {
return +1; // lower prio -> other before this
} else {
return 0; // same prio
}
}
@Override
public void run() {
task.run();
}
}
} }

View File

@ -5,9 +5,8 @@ import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender; import ch.qos.logback.core.Appender;
import ch.qos.logback.core.hook.DelayingShutdownHook;
import ch.qos.logback.core.util.Duration;
import org.cryptomator.common.Environment; import org.cryptomator.common.Environment;
import org.cryptomator.common.ShutdownHook;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -16,26 +15,27 @@ import java.util.Map;
@Singleton @Singleton
public class LoggerConfiguration { public class LoggerConfiguration {
private static final double SHUTDOWN_DELAY_MS = 100;
private final LoggerContext context; private final LoggerContext context;
private final Environment environment; private final Environment environment;
private final Appender<ILoggingEvent> stdout; private final Appender<ILoggingEvent> stdout;
private final Appender<ILoggingEvent> upgrade; private final Appender<ILoggingEvent> upgrade;
private final Appender<ILoggingEvent> file; private final Appender<ILoggingEvent> file;
private final ShutdownHook shutdownHook;
@Inject @Inject
LoggerConfiguration(LoggerContext context, // LoggerConfiguration(LoggerContext context, //
Environment environment, // Environment environment, //
@Named("stdoutAppender") Appender<ILoggingEvent> stdout, // @Named("stdoutAppender") Appender<ILoggingEvent> stdout, //
@Named("upgradeAppender") Appender<ILoggingEvent> upgrade, // @Named("upgradeAppender") Appender<ILoggingEvent> upgrade, //
@Named("fileAppender") Appender<ILoggingEvent> file) { @Named("fileAppender") Appender<ILoggingEvent> file, //
ShutdownHook shutdownHook) {
this.context = context; this.context = context;
this.environment = environment; this.environment = environment;
this.stdout = stdout; this.stdout = stdout;
this.upgrade = upgrade; this.upgrade = upgrade;
this.file = file; this.file = file;
this.shutdownHook = shutdownHook;
} }
public void init() { public void init() {
@ -62,9 +62,7 @@ public class LoggerConfiguration {
upgrades.setAdditive(false); upgrades.setAdditive(false);
// add shutdown hook // add shutdown hook
DelayingShutdownHook shutdownHook = new DelayingShutdownHook(); shutdownHook.runOnShutdown(ShutdownHook.PRIO_LAST, context::stop);
shutdownHook.setContext(context);
shutdownHook.setDelay(Duration.buildByMilliseconds(SHUTDOWN_DELAY_MS));
} }
} }