Give thread pools time to terminate & Save all data before shutting down

This commit is contained in:
KingRainbow44 2023-09-16 19:34:30 -04:00
parent 539fa16160
commit dad7821e26
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE

View File

@ -191,6 +191,9 @@ public final class Grasscutter {
/** Server shutdown event. */ /** Server shutdown event. */
private static void onShutdown() { private static void onShutdown() {
// Save all data.
Database.saveAll();
// Disable all plugins. // Disable all plugins.
if (pluginManager != null) pluginManager.disablePlugins(); if (pluginManager != null) pluginManager.disablePlugins();
// Shutdown the game server. // Shutdown the game server.
@ -200,14 +203,14 @@ public final class Grasscutter {
// Wait for Grasscutter's thread pool to finish. // Wait for Grasscutter's thread pool to finish.
var executor = Grasscutter.getThreadPool(); var executor = Grasscutter.getThreadPool();
executor.shutdown(); executor.shutdown();
if (!executor.awaitTermination(5, TimeUnit.SECONDS)) { if (!executor.awaitTermination(1, TimeUnit.MINUTES)) {
executor.shutdownNow(); executor.shutdownNow();
} }
// Wait for database operations to finish. // Wait for database operations to finish.
var dbExecutor = DatabaseHelper.getEventExecutor(); var dbExecutor = DatabaseHelper.getEventExecutor();
dbExecutor.shutdown(); dbExecutor.shutdown();
if (!dbExecutor.awaitTermination(5, TimeUnit.SECONDS)) { if (!dbExecutor.awaitTermination(2, TimeUnit.MINUTES)) {
dbExecutor.shutdownNow(); dbExecutor.shutdownNow();
} }
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {