diff --git a/src/Process.cpp b/src/Process.cpp index 4f6d352..6fff459 100644 --- a/src/Process.cpp +++ b/src/Process.cpp @@ -53,6 +53,8 @@ namespace nanosm { } else { // Parent: monitor FD eventLoop.AddObject(IoObject::Ptr(shared_from_this())); + if(onProcessSpawn) + eventLoop.Post(onProcessSpawn); } } @@ -77,6 +79,10 @@ namespace nanosm { kill(pid, SIGTERM); } + void Process::SetSpawnCallback(std::function f) { + onProcessSpawn = f; + } + void Process::SetExitCallback(std::function f) { onProcessExit = f; } diff --git a/src/Process.hpp b/src/Process.hpp index 3437c3d..02bd071 100644 --- a/src/Process.hpp +++ b/src/Process.hpp @@ -25,6 +25,9 @@ namespace nanosm { void Kill(); + + void SetSpawnCallback(std::function f); + void SetExitCallback(std::function f); private: @@ -35,6 +38,7 @@ namespace nanosm { siginfo_t siginfo {}; std::string commLine; + std::function onProcessSpawn; std::function onProcessExit; }; diff --git a/src/main.cpp b/src/main.cpp index 3dc14c5..1549259 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,10 +4,9 @@ #include #include "EventLoop.hpp" -#include "WordExp.hpp" - -#include "Timer.hpp" #include "Process.hpp" +#include "Timer.hpp" +#include "WordExp.hpp" nanosm::EventLoop ev; @@ -17,18 +16,19 @@ auto timer = std::make_shared(ev); auto process = std::make_shared(ev); void test() { + process->SetSpawnCallback([p = process]() { + // Do magic + process->SetExitCallback([p = process](int exitCode) { + printf("exited with %d exitcode\n", exitCode); - // Do magic - process->SetExitCallback([p = process](int exitCode) { - printf("exited with %d exitcode\n", exitCode); + timer->SetExpiryCallback([pp = p]() { + printf("Timer elapsed, restarting Nowr\n"); + pp->Respawn(); + }); - timer->SetExpiryCallback([pp = p]() { - printf("Timer elapsed, restarting Nowr\n"); - pp->Respawn(); + // Start the timer to wait a bit before restarting the process + timer->Arm(1); }); - - // Start the timer to wait a bit before restarting the process - timer->Arm(1); }); ev.AddObject(timer);