Bastian / bstats-metrics

The different bStats Metrics classes
https://bStats.org
MIT License
98 stars 114 forks source link

Change how scheduler is shutdown #118

Closed Konicai closed 1 year ago

Konicai commented 1 year ago

shutdown() doesn't seem to suffice - the server still hangs for some unknown amount of time at shutdown, on Fabric. I suspect this is because shutdown() only stops the scheduler from accepting new tasks.

Using ScheduledThreadPoolExecutor allows us to change the behaviour so that delayed tasks (that will execute in the future) are cancelled on #shutdown()

Delayed tasks are now cancelled by explicitly changing a setting. Periodic tasks are already cancelled by default.

Tim203 commented 1 year ago

~It looks like the reason that shutdown() doesn't suffice is because bStats is using a custom ThreadFactory. When I switch bStats to using the default ThreadFactory, bStats does properly shut down when shutdown() is called.~

The ScheduledThreadPoolExecutor does shut down queued periodic tasks, but it doesn't shut down delayed tasks by default. You could use the following code:

private final ScheduledThreadPoolExecutor executor =
        new ScheduledThreadPoolExecutor(1, task -> new Thread(task, "bStats-Metrics"));

executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);

Or you can use shutdownNow, but that will also try to stop running tasks. Which might not be what you'd want.