Repos
C++
1.2 KB39 lines Opens in site
Directory
Raw
Download
#include "mologram/DaemonSupervisor.hpp"
#include <chrono>
#include "mologram/DaemonLauncher.hpp"
#include "mologram/SocketUtil.hpp"
namespace mologram {
DaemonSupervisor::DaemonSupervisor(RemoteService& remote, std::string socketPath, std::vector<std::string> daemonArgs)
: remote_(remote), socket_(std::move(socketPath)), args_(std::move(daemonArgs)) {
thread_ = std::thread([this]() { loop(); });
}
DaemonSupervisor::~DaemonSupervisor() {
stop_ = true;
if (thread_.joinable()) thread_.join();
}
std::string DaemonSupervisor::lastError() {
std::lock_guard<std::mutex> g(mutex_);
return lastError_;
}
void DaemonSupervisor::loop() {
while (!stop_.load()) {
for (int i = 0; i < 20 && !stop_.load(); i++) std::this_thread::sleep_for(std::chrono::milliseconds(50)); // once a second
if (stop_.load() || remote_.connected()) continue;
int e = 0;
if (SocketUtil::connectUnix(socket_, e).valid()) continue; // a daemon answers: the RemoteService reconnects by itself
std::string err = DaemonLauncher::ensure(socket_, args_, [this]() { return stop_.load(); });
if (err == "cancelled") return;
if (err.empty()) restarts_++;
std::lock_guard<std::mutex> g(mutex_);
lastError_ = err;
}
}
} // namespace mologram