UniSet @VERSION@
ProcessManager.h
1/*
2 * Copyright (c) 2026 Pavel Vainerman.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation, version 2.1.
7 */
8// -------------------------------------------------------------------------
9#ifndef ProcessManager_H_
10#define ProcessManager_H_
11// -------------------------------------------------------------------------
12#include <memory>
13#include <string>
14#include <vector>
15#include <map>
16#include <mutex>
17#include <atomic>
18#include <thread>
19#include <functional>
20#include <iostream>
21#include <Poco/Process.h>
22#include "ProcessInfo.h"
23#include "DependencyResolver.h"
24#include "HealthChecker.h"
25#include "Configuration.h"
26#include "Debug.h"
27// -------------------------------------------------------------------------
28namespace uniset
29{
42 enum class BulkOperation : int
43 {
44 None = 0,
45 Restart,
46 Reload,
47 Stop
48 };
49
51 {
52 public:
53 explicit ProcessManager(std::shared_ptr<Configuration> conf = nullptr);
55
56 // Configuration
57 void setNodeName(const std::string& name);
58 std::string getNodeName() const;
59
60 void setHealthCheckInterval(size_t msec);
61 void setRestartWindow(size_t msec);
62 void setStopTimeout(size_t msec);
63 void setCommonArgs(const std::vector<std::string>& args);
64 void setPassthroughArgs(const std::string& args);
65 void setForwardArgs(const std::vector<std::string>& args);
66
67 // Process registration
68 void addProcess(const ProcessInfo& proc);
69 void addGroup(const ProcessGroup& group);
70
71 // Lifecycle management
72 bool startAll();
73 void stopAll();
74 void restartAll();
75 void reloadAll();
76 void requestStop();
77 bool isBulkOperationInProgress() const;
78 BulkOperation currentBulkOperation() const;
79 bool restartProcess(const std::string& name);
80 bool stopProcess(const std::string& name);
81 bool startProcess(const std::string& name);
82
83 // Monitoring
84 void startMonitoring();
85 void stopMonitoring();
86 bool isMonitoring() const;
87
88 // State queries
89 ProcessState getProcessState(const std::string& name) const;
90 ProcessInfo getProcessInfo(const std::string& name) const;
91 std::vector<ProcessInfo> getAllProcesses() const;
92 std::vector<ProcessGroup> getAllGroups() const;
93
94 bool allRunning() const;
95 bool anyCriticalFailed() const;
96
98 std::vector<std::string> getFullArgs(const std::string& name) const;
99
105 void printRunList(std::ostream& out) const;
106
107 // Callbacks
108 using ProcessCallback = std::function<void(const ProcessInfo&)>;
109 void setOnProcessStarted(ProcessCallback cb);
110 void setOnProcessStopped(ProcessCallback cb);
111 void setOnProcessFailed(ProcessCallback cb);
112
113 // Debug
114 std::shared_ptr<DebugStream> log();
115
116 private:
117 bool startProcessWithUnlock(ProcessInfo& proc, std::unique_lock<std::mutex>& lock);
118 bool startOneshotWithUnlock(ProcessInfo& proc, std::unique_lock<std::mutex>& lock);
119 void stopProcess(ProcessInfo& proc);
120 void doStopAll();
121 void handleProcessExitByName(const std::string& name, int exitCode);
122 void monitorLoop();
123
124 // Helper methods for process startup
125 std::vector<std::string> assembleArgs(const ProcessInfo& proc) const;
126 std::vector<std::string> prepareProcessArgs(const ProcessInfo& proc);
127 bool launchDaemonProcess(ProcessInfo& proc);
128
130 static bool interruptibleSleep(size_t msec, const std::atomic<bool>& cancelFlag,
131 size_t pollInterval_msec = 500);
132
133 std::vector<std::string> resolveStartOrder();
134 void expandEnvironment(std::vector<std::string>& args);
135 std::string expandEnvVar(const std::string& s);
136
137 std::shared_ptr<Configuration> conf_;
138 std::unique_ptr<HealthChecker> healthChecker_;
139 DependencyResolver depResolver_;
140
141 std::string nodeName_;
142 std::map<std::string, ProcessInfo> processes_;
143 std::map<std::string, ProcessGroup> groups_;
144
145 std::thread monitorThread_;
146 std::atomic<bool> running_{false};
147 std::atomic<bool> stopping_{false};
148 std::atomic<BulkOperation> currentBulkOp_{BulkOperation::None};
149 mutable std::mutex mutex_;
150
151 size_t healthCheckInterval_msec_ = 5000;
152 size_t restartWindow_msec_ = 60000;
153 size_t stopTimeout_msec_ = 5000; // Time to wait for graceful shutdown before SIGKILL
154 std::vector<std::string> commonArgs_;
155 std::string passthroughArgs_; // Arguments after "--" passed to all child processes
156 std::vector<std::string> forwardArgs_; // Unknown arguments forwarded to child processes
157
158 ProcessCallback onStarted_;
159 ProcessCallback onStopped_;
160 ProcessCallback onFailed_;
161
162 std::shared_ptr<DebugStream> mylog;
163 };
164
165} // end of namespace uniset
166// -------------------------------------------------------------------------
167#endif // ProcessManager_H_
168// -------------------------------------------------------------------------
Definition DependencyResolver.h:39
Definition ProcessManager.h:51
void restartAll()
Restart all running processes.
Definition ProcessManager.cc:993
void reloadAll()
Stop all, then start all (except skip, manual)
Definition ProcessManager.cc:1235
std::vector< std::string > getFullArgs(const std::string &name) const
Get full arguments list for a process (commonArgs + args + forwardArgs)
Definition ProcessManager.cc:1741
void requestStop()
Set stopping_ flag (async-signal-safe)
Definition ProcessManager.cc:830
void printRunList(std::ostream &out) const
Definition ProcessManager.cc:1815
Definition AccessConfig.h:30
BulkOperation
Definition ProcessManager.h:43
Definition ProcessInfo.h:113
Definition ProcessInfo.h:66