Graceful shutdown running async jobs

Former-commit-id: 2ce5b94741d384b2dfd6d3b347fa1582cd45df8f
This commit is contained in:
John Sully 2020-07-10 00:17:39 +00:00
parent 993359e1bb
commit 2db9eef2b1
3 changed files with 21 additions and 0 deletions

View File

@ -73,6 +73,18 @@ bool AsyncWorkQueue::removeClientAsyncWrites(client *c)
return fFound;
}
void AsyncWorkQueue::shutdown()
{
std::unique_lock<std::mutex> lock(m_mutex);
serverAssert(!GlobalLocksAcquired());
m_fQuitting = true;
m_cvWakeup.notify_all();
lock.unlock();
for (auto &thread : m_vecthreads)
thread.join();
}
void AsyncWorkQueue::abandonThreads()
{
std::unique_lock<std::mutex> lock(m_mutex);

View File

@ -34,5 +34,7 @@ public:
void AddWorkFunction(std::function<void()> &&fnAsync, bool fHiPri = false);
bool removeClientAsyncWrites(struct client *c);
void shutdown();
void abandonThreads();
};

View File

@ -4225,6 +4225,13 @@ int prepareForShutdown(int flags) {
/* Close the listening sockets. Apparently this allows faster restarts. */
closeListeningSockets(1);
if (g_pserver->asyncworkqueue)
{
aeReleaseLock();
g_pserver->asyncworkqueue->shutdown();
aeAcquireLock();
}
for (int iel = 0; iel < cserver.cthreads; ++iel)
{
aePostFunction(g_pserver->rgthreadvar[iel].el, [iel]{