Process monitors itself, restarts if it faults, preserves logs

and cores. Rate-limiting for safety.
This commit is contained in:
JoelKatz
2013-03-20 06:33:29 -07:00
parent 0e34de512e
commit c5429032ed
4 changed files with 88 additions and 0 deletions

View File

@@ -64,6 +64,7 @@ bool Instance::running = true;
void Application::stop()
{
cLog(lsINFO) << "Received shutdown request";
StopSustain();
mShutdown = true;
mIOService.stop();
mHashedObjectStore.bulkWrite();

View File

@@ -149,6 +149,7 @@ int main(int argc, char* argv[])
("ledger", po::value<std::string>(), "Load the specified ledger and start from .")
("start", "Start from a fresh Ledger.")
("net", "Get the initial ledger from the network.")
("fg", "Run in the foreground.")
;
// Interpret positional arguments as --parameters.
@@ -186,6 +187,13 @@ int main(int argc, char* argv[])
}
}
if (HaveSustain() && !vm.count("parameters") && !vm.count("fg") && !vm.count("standalone"))
{
std::string logMe = DoSustain();
if (!logMe.empty())
Log(lsWARNING) << logMe;
}
if (vm.count("quiet"))
Log::setMinSeverity(lsFATAL, true);
else if (vm.count("verbose"))

View File

@@ -1,6 +1,8 @@
#ifdef __linux__
#include <sys/types.h>
#include <sys/prctl.h>
#include <sys/wait.h>
#endif
#include <fstream>
@@ -373,6 +375,79 @@ extern void NameThread(const char*)
{ ; }
#endif
#ifdef __unix__
static pid_t pManager = static_cast<pid_t>(0);
static pid_t pChild = static_cast<pid_t>(0);
static void pass_signal(int a)
{
kill(pChild, a);
}
static void stop_manager(int)
{
kill(pChild, SIGINT);
_exit(0);
}
bool HaveSustain()
{
return true;
}
std::string StopSustain()
{
if (getppid() != pManager)
return std::string();
kill(pManager, SIGHUP);
return "Terminating monitor";
}
std::string DoSustain()
{
int childCount = 0;
pManager = getpid();
signal(SIGINT, stop_manager);
signal(SIGHUP, stop_manager);
signal(SIGUSR1, pass_signal);
signal(SIGUSR2, pass_signal);
while (1)
{
++childCount;
pChild = fork();
if (pChild == -1)
_exit(0);
if (pChild == 0)
{
NameThread("child");
signal(SIGINT, SIG_DFL);
signal(SIGHUP, SIG_DFL);
signal(SIGUSR1, SIG_DFL);
signal(SIGUSR2, SIG_DFL);
return str(boost::format("Launching child %d") % childCount);;
}
NameThread(boost::str(boost::format("sust%d") % childCount).c_str());
do
{
int i;
sleep(10);
waitpid(-1, &i, 0);
}
while (kill(pChild, 0) == 0);
rename("core", boost::str(boost::format("core.%d") % static_cast<int>(pChild)).c_str());
rename("debug.log", boost::str(boost::format("debug.log.%d") % static_cast<int>(pChild)).c_str());
}
}
#else
bool HaveSustain() { return false; }
std::string DoSustain() { return std::string; }
std::string StopSustain() { return std::string; }
#endif
BOOST_AUTO_TEST_SUITE( Utils)
BOOST_AUTO_TEST_CASE( ParseUrl )

View File

@@ -288,6 +288,10 @@ bool parseUrl(const std::string& strUrl, std::string& strScheme, std::string& st
extern void NameThread(const char *);
extern bool HaveSustain();
extern std::string StopSustain();
extern std::string DoSustain();
#if (!defined(FORCE_NO_C11X) && (__cplusplus > 201100L)) || defined(FORCE_C11X)
#define C11X