fix: Prevent data loss on server shutdown in DataManager

We should wait for the async pool to cleanly exit and finish all running tasks.
30 seconds is a lot but we don't want any data loss – If 30 seconds are exceeded there might just be something
fundamentally broken in the (plugin) implementation (or a huge bulk action?)
This commit is contained in:
Christian Koop 2024-02-10 19:26:44 +01:00
parent c59884b87c
commit 4b738fdf78
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3

View File

@ -532,18 +532,28 @@ public class DataManager {
* Close the database and shutdown the async pool
*/
public void shutdown() {
asyncPool.shutdown();
databaseConnector.closeConnection();
this.asyncPool.shutdown();
try {
if (!this.asyncPool.awaitTermination(30, TimeUnit.SECONDS)) {
this.plugin.getLogger().warning("Failed to shutdown the async DataManager pool in time. Forcing shutdown");
}
} catch (InterruptedException ex) {
this.plugin.getLogger().warning("Error while shutting down the async DataManager pool: " + ex.getMessage());
}
this.asyncPool.shutdownNow();
this.databaseConnector.closeConnection();
}
/**
* Force shutdown the async pool and close the database
*
* @return Tasks that didn't finish in the async pool
* @return Tasks that were still in the pool's queue
*/
public List<Runnable> shutdownNow() {
databaseConnector.closeConnection();
return asyncPool.shutdownNow();
List<Runnable> tasksLeftInQueue = this.asyncPool.shutdownNow();
this.databaseConnector.closeConnection();
return tasksLeftInQueue;
}
public void shutdownTaskQueue() {