loolwsd: notify clients of shutdown

Change-Id: I8dbe5752e8e36e2a1f6dee05f5f355f5a7b4d1b6
Reviewed-on: https://gerrit.libreoffice.org/31001
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
This commit is contained in:
Ashod Nakashian 2016-11-19 10:22:53 -05:00 committed by Ashod Nakashian
parent 98020193d4
commit 3b90149229
5 changed files with 39 additions and 4 deletions

View file

@ -149,7 +149,7 @@ bool AdminRequestHandler::adminCommandHandler(const std::vector<char>& payload)
else if (tokens[0] == "shutdown")
{
LOG_INF("Shutdown requested by admin.");
ShutdownFlag = true;
SigUtil::requestShutdown();
return false;
}
else if (tokens[0] == "set" && tokens.count() > 1)

View file

@ -582,6 +582,7 @@ void DocumentBroker::alertAllUsers(const std::string& msg)
{
Util::assertIsLocked(_mutex);
LOG_DBG("Alerting all users: " << msg);
for (auto& it : _sessions)
{
try

View file

@ -1971,7 +1971,7 @@ int LOOLWSD::main(const std::vector<std::string>& /*args*/)
while (!TerminationFlag && !ShutdownFlag)
{
UnitWSD::get().invokeTest();
if (TerminationFlag)
if (TerminationFlag || SigUtil::handleShutdownRequest())
{
break;
}
@ -2078,7 +2078,6 @@ int LOOLWSD::main(const std::vector<std::string>& /*args*/)
// and wait until sockets close.
LOG_INF("Stopping server socket listening. ShutdownFlag: " <<
ShutdownFlag << ", TerminationFlag: " << TerminationFlag);
Util::alertAllUsers("close: shutdown");
srv.stop();
srv2.stop();

View file

@ -54,6 +54,8 @@ std::atomic<bool> TerminationFlag(false);
std::atomic<bool> ShutdownFlag(false);
std::mutex SigHandlerTrap;
static std::atomic<bool> ShutdownRequestFlag(false);
namespace SigUtil
{
const char *signalName(const int signo)
@ -125,7 +127,7 @@ namespace SigUtil
Log::signalLog(" Shutdown signal received: ");
Log::signalLog(signalName(signal));
Log::signalLog("\n");
ShutdownFlag = true;
SigUtil::requestShutdown();
}
else
{
@ -242,6 +244,24 @@ namespace SigUtil
strncpy(FatalGdbString, streamStr.c_str(), sizeof(FatalGdbString));
}
void requestShutdown()
{
ShutdownRequestFlag = true;
}
bool handleShutdownRequest()
{
if (ShutdownRequestFlag)
{
LOG_INF("Shutdown requested. Initiating WSD shutdown.");
Util::alertAllUsers("close: shutdown");
ShutdownFlag = true;
return true;
}
return false;
}
bool killChild(const int pid)
{
LOG_DBG("Killing PID: " << pid);

View file

@ -37,6 +37,21 @@ namespace SigUtil
/// Trap all fatal signals to assist debugging.
void setFatalSignals();
/// Requests the server to initiate graceful shutdown.
/// Shutting down is a multi-stage process, because
/// it can be requested via signals.
/// Since we need to notify clients, we can't
/// invoke the sockets while in a signal handler.
/// This flags the server to notify clients first
/// then flags for shutdown.
void requestShutdown();
/// Checks for shutdown request and,
/// after notifying clients, flags for
/// shutting down.
/// Returns true if shutdown is requested.
bool handleShutdownRequest();
/// Kills a child process and returns true when
/// child pid is removed from the process table
/// after a certain (short) timeout.