mirror of
https://github.com/mod-playerbots/azerothcore-wotlk.git
synced 2026-01-25 22:56:24 +00:00
fix(Core/Database): Gracefully close database workers (#20936)
* Gracefully close database workers * Change init order. Such a silly compiler flag * Fix hang if db connection failed to open
This commit is contained in:
@@ -23,16 +23,11 @@ DatabaseWorker::DatabaseWorker(ProducerConsumerQueue<SQLOperation*>* newQueue, M
|
||||
{
|
||||
_connection = connection;
|
||||
_queue = newQueue;
|
||||
_cancelationToken = false;
|
||||
_workerThread = std::thread(&DatabaseWorker::WorkerThread, this);
|
||||
}
|
||||
|
||||
DatabaseWorker::~DatabaseWorker()
|
||||
{
|
||||
_cancelationToken = true;
|
||||
|
||||
_queue->Cancel();
|
||||
|
||||
_workerThread.join();
|
||||
}
|
||||
|
||||
@@ -47,7 +42,7 @@ void DatabaseWorker::WorkerThread()
|
||||
|
||||
_queue->WaitAndPop(operation);
|
||||
|
||||
if (_cancelationToken || !operation)
|
||||
if (!operation)
|
||||
return;
|
||||
|
||||
operation->SetConnection(_connection);
|
||||
|
||||
@@ -41,8 +41,6 @@ private:
|
||||
void WorkerThread();
|
||||
std::thread _workerThread;
|
||||
|
||||
std::atomic<bool> _cancelationToken;
|
||||
|
||||
DatabaseWorker(DatabaseWorker const& right) = delete;
|
||||
DatabaseWorker& operator=(DatabaseWorker const& right) = delete;
|
||||
};
|
||||
|
||||
@@ -112,7 +112,11 @@ uint32 DatabaseWorkerPool<T>::Open()
|
||||
template <class T>
|
||||
void DatabaseWorkerPool<T>::Close()
|
||||
{
|
||||
LOG_INFO("sql.driver", "Closing down DatabasePool '{}'.", GetDatabaseName());
|
||||
LOG_INFO("sql.driver", "Closing down DatabasePool '{}'. Waiting for {} queries to finish...", GetDatabaseName(), _queue->Size());
|
||||
|
||||
// Gracefully close async query queue, worker threads will block when the destructor
|
||||
// is called from the .clear() functions below until the queue is empty
|
||||
_queue->Shutdown();
|
||||
|
||||
//! Closes the actualy MySQL connection.
|
||||
_connections[IDX_ASYNC].clear();
|
||||
@@ -432,6 +436,7 @@ uint32 DatabaseWorkerPool<T>::OpenConnections(InternalIndex type, uint8 numConne
|
||||
if (uint32 error = connection->Open())
|
||||
{
|
||||
// Failed to open a connection or invalid version, abort and cleanup
|
||||
_queue->Cancel();
|
||||
_connections[type].clear();
|
||||
return error;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user