graceful shutdown by SIGINT

This commit is contained in:
orignal 2016-03-29 12:34:53 -04:00
parent 25dbf62274
commit db88183a23
2 changed files with 39 additions and 21 deletions

View File

@ -50,26 +50,31 @@ namespace i2p
bool init(int argc, char* argv[]);
bool start();
bool stop();
bool stop();
void run ();
};
#else
class DaemonLinux : public Daemon_Singleton
{
public:
static DaemonLinux& Instance()
{
static DaemonLinux instance;
return instance;
}
public:
static DaemonLinux& Instance()
{
static DaemonLinux instance;
return instance;
}
bool start();
bool stop();
; void run ();
bool start();
bool stop();
void run ();
private:
std::string pidfile;
int pidFH;
private:
std::string pidfile;
int pidFH;
public:
int gracefullShutdownInterval; // in seconds
};
#endif

View File

@ -17,14 +17,17 @@ void handle_signal(int sig)
{
switch (sig)
{
case SIGHUP:
LogPrint(eLogInfo, "Daemon: Got SIGHUP, reopening log...");
i2p::log::Logger().Reopen ();
break;
case SIGABRT:
case SIGTERM:
case SIGINT:
Daemon.running = 0; // Exit loop
case SIGHUP:
LogPrint(eLogInfo, "Daemon: Got SIGHUP, reopening log...");
i2p::log::Logger().Reopen ();
break;
case SIGINT:
Daemon.gracefullShutdownInterval = 10*60; // 10 minutes
LogPrint(eLogInfo, "Graceful shutdown after ", Daemon.gracefullShutdownInterval, " seconds");
break;
case SIGABRT:
case SIGTERM:
Daemon.running = 0; // Exit loop
break;
}
}
@ -96,6 +99,7 @@ namespace i2p
return false;
}
}
gracefullShutdownInterval = 0; // not specified
// Signal handler
struct sigaction sa;
@ -122,6 +126,15 @@ namespace i2p
while (running)
{
std::this_thread::sleep_for (std::chrono::seconds(1));
if (gracefullShutdownInterval)
{
gracefullShutdownInterval--; // - 1 second
if (gracefullShutdownInterval <= 0)
{
LogPrint(eLogInfo, "Graceful shutdown");
return;
}
}
}
}
}