mirror of
https://github.com/ohwgiles/laminar.git
synced 2026-11-02 16:44:45 +00:00
resolves #29: graceful shutdown
on SIGINT/SIGTERM: 1. stop accepting new connections 2. send SIGTERM to all child tasks 3. wait for processes to end 4. drop all websockets
This commit is contained in:
25
src/run.cpp
25
src/run.cpp
@@ -58,9 +58,6 @@ std::string Run::reason() const {
|
||||
}
|
||||
|
||||
bool Run::step() {
|
||||
if(!currentScript.path.empty() && procStatus != 0)
|
||||
result = RunState::FAILED;
|
||||
|
||||
if(scripts.size()) {
|
||||
currentScript = scripts.front();
|
||||
scripts.pop();
|
||||
@@ -75,6 +72,9 @@ bool Run::step() {
|
||||
sigaddset(&mask, SIGCHLD);
|
||||
sigprocmask(SIG_UNBLOCK, &mask, nullptr);
|
||||
|
||||
// set pgid == pid for easy killing on abort
|
||||
setpgid(0, 0);
|
||||
|
||||
close(pfd[0]);
|
||||
dup2(pfd[1], 1);
|
||||
dup2(pfd[1], 2);
|
||||
@@ -127,14 +127,31 @@ bool Run::step() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void Run::addScript(std::string scriptPath, std::string scriptWorkingDir) {
|
||||
scripts.push({scriptPath, scriptWorkingDir});
|
||||
}
|
||||
|
||||
void Run::addEnv(std::string path) {
|
||||
env.push_back(path);
|
||||
}
|
||||
|
||||
void Run::abort() {
|
||||
// clear all pending scripts
|
||||
std::queue<Script>().swap(scripts);
|
||||
kill(-pid, SIGTERM);
|
||||
}
|
||||
|
||||
void Run::reaped(int status) {
|
||||
procStatus = status;
|
||||
// once state is non-success it cannot change again
|
||||
if(result != RunState::SUCCESS)
|
||||
return;
|
||||
|
||||
if(WIFSIGNALED(status) && (WTERMSIG(status) == SIGTERM || WTERMSIG(status) == SIGKILL))
|
||||
result = RunState::ABORTED;
|
||||
else if(status != 0)
|
||||
result = RunState::FAILED;
|
||||
// otherwise preserve earlier status
|
||||
}
|
||||
|
||||
void Run::complete() {
|
||||
|
||||
Reference in New Issue
Block a user