ReloadableApplications: supervised out-of-process provider with bounded restart policy and explicit unknown effects (P-RELOADABLE-06)
This commit is contained in:
1 parent
aad91c7d01
commit
9ba4738415
6 files changed
+611
-3
No files matched your search
@@ -0,0 +1,75 @@
|
||||
#include "reloadable_host/posix_worker.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace ReloadableHost
|
||||
{
|
||||
namespace
|
||||
{
|
||||
[[nodiscard]] Void::Result::Error state_error(std::string message)
|
||||
{
|
||||
return Void::Result::Error{Void::Result::ErrorKind::conflict,
|
||||
std::move(message)};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
PosixWorkerPort::~PosixWorkerPort()
|
||||
{
|
||||
if (m_child && m_child->valid()) {
|
||||
(void)m_child->terminate(std::chrono::milliseconds{2000});
|
||||
}
|
||||
}
|
||||
|
||||
Void::Result::Result<void>
|
||||
PosixWorkerPort::spawn(const std::string& command)
|
||||
{
|
||||
if (m_child && m_child->valid() && m_child->running()) {
|
||||
return std::unexpected{
|
||||
state_error("PosixWorkerPort: a worker is already running")};
|
||||
}
|
||||
m_child.reset();
|
||||
|
||||
Void::Process::SpawnOptions options;
|
||||
options.executable = "/bin/sh";
|
||||
options.arguments = {"-c", command};
|
||||
options.stdin_pipe = false;
|
||||
options.stdout_pipe = false;
|
||||
options.stderr_pipe = false;
|
||||
auto child = Void::Process::spawn(options);
|
||||
if (!child) {
|
||||
return std::unexpected{std::move(child).error()};
|
||||
}
|
||||
m_child.emplace(std::move(*child));
|
||||
return {};
|
||||
}
|
||||
|
||||
Void::Result::Result<std::optional<WorkerExit>> PosixWorkerPort::try_wait()
|
||||
{
|
||||
if (!m_child) {
|
||||
return std::optional<WorkerExit>{};
|
||||
}
|
||||
auto status = m_child->try_wait();
|
||||
if (!status) {
|
||||
return std::unexpected{std::move(status).error()};
|
||||
}
|
||||
if (!status->has_value()) {
|
||||
return std::optional<WorkerExit>{};
|
||||
}
|
||||
const auto& exit = **status;
|
||||
return std::optional<WorkerExit>{WorkerExit{
|
||||
.exited = exit.exited, .code = exit.code, .signal = exit.signal}};
|
||||
}
|
||||
|
||||
void PosixWorkerPort::terminate(std::chrono::milliseconds grace) noexcept
|
||||
{
|
||||
if (m_child && m_child->valid()) {
|
||||
(void)m_child->terminate(grace);
|
||||
}
|
||||
}
|
||||
|
||||
bool PosixWorkerPort::running() const noexcept
|
||||
{
|
||||
return m_child && m_child->running();
|
||||
}
|
||||
} // namespace ReloadableHost
|
||||
Reference in new issue
Block a user