/workspaces/astro/sol3-sdk/cpp/sol3/process_manager/process.h Source File

Space-ng SDK: /workspaces/astro/sol3-sdk/cpp/sol3/process_manager/process.h Source File
Space-ng SDK
process.h
Go to the documentation of this file.
1 // Copyright (c) Space-ng, inc. All rights reserved.
2 
3 #pragma once
4 
5 #include "sol3/asio/strand.h"
7 #include "sol3/core/msg/stamp.h"
8 #include "sol3/process_manager/msg/process_manager_config.h"
9 #include "sol3/process_manager/msg/process_manager_telemetry.h"
10 
11 #include <boost/asio/io_context.hpp>
12 #include <boost/process/child.hpp>
13 #include <boost/process/v2.hpp>
14 #include <boost/process/v2/stdio.hpp>
15 
16 #include <cstdint>
17 #include <memory>
18 #include <optional>
19 
21 
22 namespace bpv2 = boost::process::v2;
23 
24 class Process;
25 
28 using ExitHandler = std::function<void(std::shared_ptr<Process> process)>;
29 
33  running,
34  stopping,
36  stopped,
38 };
39 
47  boost::process::v2::process child;
49  std::vector<ExitHandler> exit_handlers;
51  core::msg::Stamp started;
55  std::optional<core::msg::Stamp> stopped = std::nullopt;
59  bool sent_sigterm = false;
63  bool sent_sigkill = false;
65  bool auto_restart_disabled = false;
66 };
67 
69  public:
70  virtual ~IStdioContext() = default;
71  virtual bpv2::process_stdio getProcessStdio() = 0;
72 };
73 
81 class Process : public std::enable_shared_from_this<Process> {
82  public:
89  static std::shared_ptr<Process> create(
90  asio::Strand strand,
91  msg::ProcessConfigT config,
92  std::optional<ExitHandler> exit_handler = {},
93  std::shared_ptr<IStdioContext> stdio_context = {});
94 
97  void asyncStart(bool reset_restart_count = false);
98 
102  void asyncRestart(bool reset_restart_count = false);
103 
106  void asyncStop(std::optional<ExitHandler> on_exit = {});
107 
110  void asyncSetLifecycleManagement(bool enabled);
111 
115  msg::ProcessTelemetryT telemetry() const;
116 
119  msg::ProcessConfigT const& config() const;
120 
121  private:
126  Process(
127  asio::Strand strand,
128  msg::ProcessConfigT config,
129  std::optional<ExitHandler> exit_handler,
130  std::shared_ptr<IStdioContext> stdio_context);
131 
135  void start(bool reset_restarts);
136 
140  void restart(bool reset_restarts);
141 
145  void stop(std::optional<ExitHandler> on_exit);
146 
149  void kill();
150 
152  msg::ProcessConfigT config_;
154  std::vector<ExitHandler> exit_handlers_;
156  std::shared_ptr<IStdioContext> process_stdio_;
158  bool lifecycle_managed_{false};
160  uint16_t restart_count_{0};
162  std::optional<ProcessInvocation> current_;
163 
165  asio::Strand strand_;
167  boost::asio::steady_timer restart_timer_;
168 };
169 
170 } // namespace sol3::process_manager
Definition: process.h:68
virtual bpv2::process_stdio getProcessStdio()=0
Manages the lifecycle of a single child process with restart capabilities.
Definition: process.h:81
void asyncStart(bool reset_restart_count=false)
Asynchronously starts the process without resetting restart count.
void asyncSetLifecycleManagement(bool enabled)
Asynchronously sets lifecycle management (auto-restart, etc.).
static std::shared_ptr< Process > create(asio::Strand strand, msg::ProcessConfigT config, std::optional< ExitHandler > exit_handler={}, std::shared_ptr< IStdioContext > stdio_context={})
Create a reference counted Process.
void asyncRestart(bool reset_restart_count=false)
Asynchronously restarts the process without resetting restart count.
void asyncStop(std::optional< ExitHandler > on_exit={})
Asynchronously stops the process and invokes a callback on exit.
msg::ProcessConfigT const & config() const
Retrieves current process config.
msg::ProcessTelemetryT telemetry() const
Retrieves current process telemetry.
boost::asio::strand< boost::asio::io_context::executor_type > Strand
Definition: strand.h:8
Definition: process.h:20
std::function< void(std::shared_ptr< Process > process)> ExitHandler
Callback invoked when a process exits.
Definition: process.h:28
ProcessInvocationState
Current process lifecycle state.
Definition: process.h:31
@ running
Process has been started, and hasn't been stopped or reaped.
@ failed_to_start
Process failed to ever start.
Represents a single invocation (execution) of a process.
Definition: process.h:45
bool sent_sigterm
Definition: process.h:59
boost::process::v2::process child
The boost::process child handle.
Definition: process.h:47
bool auto_restart_disabled
True if auto restart was disabled for this process invocation.
Definition: process.h:65
bool sent_sigkill
Definition: process.h:63
std::optional< core::msg::Stamp > stopped
Timestamp when this invocation was stopped.
Definition: process.h:55
std::vector< ExitHandler > exit_handlers
Handlers to invoke when this process invocation exits.
Definition: process.h:49
ProcessInvocationState state
Process state.
Definition: process.h:53
core::msg::Stamp started
Timestamp when this invocation was started.
Definition: process.h:51