add spawn callback to Process
This commit is contained in:
parent
4c1480d364
commit
ef904f5403
|
@ -53,6 +53,8 @@ namespace nanosm {
|
||||||
} else {
|
} else {
|
||||||
// Parent: monitor FD
|
// Parent: monitor FD
|
||||||
eventLoop.AddObject(IoObject::Ptr(shared_from_this()));
|
eventLoop.AddObject(IoObject::Ptr(shared_from_this()));
|
||||||
|
if(onProcessSpawn)
|
||||||
|
eventLoop.Post(onProcessSpawn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,6 +79,10 @@ namespace nanosm {
|
||||||
kill(pid, SIGTERM);
|
kill(pid, SIGTERM);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Process::SetSpawnCallback(std::function<void()> f) {
|
||||||
|
onProcessSpawn = f;
|
||||||
|
}
|
||||||
|
|
||||||
void Process::SetExitCallback(std::function<void(int)> f) {
|
void Process::SetExitCallback(std::function<void(int)> f) {
|
||||||
onProcessExit = f;
|
onProcessExit = f;
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,9 @@ namespace nanosm {
|
||||||
|
|
||||||
void Kill();
|
void Kill();
|
||||||
|
|
||||||
|
|
||||||
|
void SetSpawnCallback(std::function<void()> f);
|
||||||
|
|
||||||
void SetExitCallback(std::function<void(int)> f);
|
void SetExitCallback(std::function<void(int)> f);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -35,6 +38,7 @@ namespace nanosm {
|
||||||
siginfo_t siginfo {};
|
siginfo_t siginfo {};
|
||||||
std::string commLine;
|
std::string commLine;
|
||||||
|
|
||||||
|
std::function<void()> onProcessSpawn;
|
||||||
std::function<void(int)> onProcessExit;
|
std::function<void(int)> onProcessExit;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
24
src/main.cpp
24
src/main.cpp
|
@ -4,10 +4,9 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "EventLoop.hpp"
|
#include "EventLoop.hpp"
|
||||||
#include "WordExp.hpp"
|
|
||||||
|
|
||||||
#include "Timer.hpp"
|
|
||||||
#include "Process.hpp"
|
#include "Process.hpp"
|
||||||
|
#include "Timer.hpp"
|
||||||
|
#include "WordExp.hpp"
|
||||||
|
|
||||||
nanosm::EventLoop ev;
|
nanosm::EventLoop ev;
|
||||||
|
|
||||||
|
@ -17,18 +16,19 @@ auto timer = std::make_shared<nanosm::Timer>(ev);
|
||||||
auto process = std::make_shared<nanosm::Process>(ev);
|
auto process = std::make_shared<nanosm::Process>(ev);
|
||||||
|
|
||||||
void test() {
|
void test() {
|
||||||
|
process->SetSpawnCallback([p = process]() {
|
||||||
|
// Do magic
|
||||||
|
process->SetExitCallback([p = process](int exitCode) {
|
||||||
|
printf("exited with %d exitcode\n", exitCode);
|
||||||
|
|
||||||
// Do magic
|
timer->SetExpiryCallback([pp = p]() {
|
||||||
process->SetExitCallback([p = process](int exitCode) {
|
printf("Timer elapsed, restarting Nowr\n");
|
||||||
printf("exited with %d exitcode\n", exitCode);
|
pp->Respawn();
|
||||||
|
});
|
||||||
|
|
||||||
timer->SetExpiryCallback([pp = p]() {
|
// Start the timer to wait a bit before restarting the process
|
||||||
printf("Timer elapsed, restarting Nowr\n");
|
timer->Arm(1);
|
||||||
pp->Respawn();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Start the timer to wait a bit before restarting the process
|
|
||||||
timer->Arm(1);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
ev.AddObject(timer);
|
ev.AddObject(timer);
|
||||||
|
|
Loading…
Reference in New Issue