add spawn callback to Process
This commit is contained in:
parent
4c1480d364
commit
ef904f5403
|
@ -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<void()> f) {
|
||||
onProcessSpawn = f;
|
||||
}
|
||||
|
||||
void Process::SetExitCallback(std::function<void(int)> f) {
|
||||
onProcessExit = f;
|
||||
}
|
||||
|
|
|
@ -25,6 +25,9 @@ namespace nanosm {
|
|||
|
||||
void Kill();
|
||||
|
||||
|
||||
void SetSpawnCallback(std::function<void()> f);
|
||||
|
||||
void SetExitCallback(std::function<void(int)> f);
|
||||
|
||||
private:
|
||||
|
@ -35,6 +38,7 @@ namespace nanosm {
|
|||
siginfo_t siginfo {};
|
||||
std::string commLine;
|
||||
|
||||
std::function<void()> onProcessSpawn;
|
||||
std::function<void(int)> onProcessExit;
|
||||
};
|
||||
|
||||
|
|
24
src/main.cpp
24
src/main.cpp
|
@ -4,10 +4,9 @@
|
|||
#include <memory>
|
||||
|
||||
#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<nanosm::Timer>(ev);
|
|||
auto process = std::make_shared<nanosm::Process>(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);
|
||||
|
|
Loading…
Reference in New Issue