UniSet 2.44.3
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 class ProcessManager
43 {
44 public:
45 explicit ProcessManager(std::shared_ptr<Configuration> conf = nullptr);
46 ~ProcessManager();
47
48 // Configuration
49 void setNodeName(const std::string& name);
50 std::string getNodeName() const;
51
52 void setHealthCheckInterval(size_t msec);
53 void setRestartWindow(size_t msec);
54 void setStopTimeout(size_t msec);
55 void setCommonArgs(const std::vector<std::string>& args);
56 void setPassthroughArgs(const std::string& args);
57 void setForwardArgs(const std::vector<std::string>& args);
58
59 // Process registration
60 void addProcess(const ProcessInfo& proc);
61 void addGroup(const ProcessGroup& group);
62
63 // Lifecycle management
64 bool startAll();
65 void stopAll();
66 void restartAll();
67 void reloadAll();
68 bool restartProcess(const std::string& name);
69 bool stopProcess(const std::string& name);
70 bool startProcess(const std::string& name);
71
72 // Monitoring
73 void startMonitoring();
74 void stopMonitoring();
75 bool isMonitoring() const;
76
77 // State queries
78 ProcessState getProcessState(const std::string& name) const;
79 ProcessInfo getProcessInfo(const std::string& name) const;
80 std::vector<ProcessInfo> getAllProcesses() const;
81 std::vector<ProcessGroup> getAllGroups() const;
82
83 bool allRunning() const;
84 bool anyCriticalFailed() const;
85
87 std::vector<std::string> getFullArgs(const std::string& name) const;
88
94 void printRunList(std::ostream& out) const;
95
96 // Callbacks
97 using ProcessCallback = std::function<void(const ProcessInfo&)>;
98 void setOnProcessStarted(ProcessCallback cb);
99 void setOnProcessStopped(ProcessCallback cb);
100 void setOnProcessFailed(ProcessCallback cb);
101
102 // Debug
103 std::shared_ptr<DebugStream> log();
104
105 private:
106 bool startProcessWithUnlock(ProcessInfo& proc, std::unique_lock<std::mutex>& lock);
107 bool startOneshotWithUnlock(ProcessInfo& proc, std::unique_lock<std::mutex>& lock);
108 void stopProcess(ProcessInfo& proc);
109 void handleProcessExitByName(const std::string& name, int exitCode);
110 void monitorLoop();
111
112 // Helper methods for process startup
113 std::vector<std::string> prepareProcessArgs(const ProcessInfo& proc);
114 bool launchDaemonProcess(ProcessInfo& proc);
115
116 std::vector<std::string> resolveStartOrder();
117 void expandEnvironment(std::vector<std::string>& args);
118 std::string expandEnvVar(const std::string& s);
119
120 std::shared_ptr<Configuration> conf_;
121 std::unique_ptr<HealthChecker> healthChecker_;
122 DependencyResolver depResolver_;
123
124 std::string nodeName_;
125 std::map<std::string, ProcessInfo> processes_;
126 std::map<std::string, ProcessGroup> groups_;
127
128 std::thread monitorThread_;
129 std::atomic<bool> running_{false};
130 std::atomic<bool> stopping_{false};
131 mutable std::mutex mutex_;
132
133 size_t healthCheckInterval_msec_ = 5000;
134 size_t restartWindow_msec_ = 60000;
135 size_t stopTimeout_msec_ = 5000; // Time to wait for graceful shutdown before SIGKILL
136 std::vector<std::string> commonArgs_;
137 std::string passthroughArgs_; // Arguments after "--" passed to all child processes
138 std::vector<std::string> forwardArgs_; // Unknown arguments forwarded to child processes
139
140 ProcessCallback onStarted_;
141 ProcessCallback onStopped_;
142 ProcessCallback onFailed_;
143
144 std::shared_ptr<DebugStream> mylog;
145 };
146
147} // end of namespace uniset
148// -------------------------------------------------------------------------
149#endif // ProcessManager_H_
150// -------------------------------------------------------------------------
Определения DependencyResolver.h:39
void restartAll()
Restart all running processes.
Определения ProcessManager.cc:884
void reloadAll()
Stop all, then start all (except skip, manual).
Определения ProcessManager.cc:1087
std::vector< std::string > getFullArgs(const std::string &name) const
Get full arguments list for a process (commonArgs + args + forwardArgs).
Определения ProcessManager.cc:1573
void printRunList(std::ostream &out) const
Определения ProcessManager.cc:1675
Определения Calibration.h:27
Определения ProcessInfo.h:113
Определения ProcessInfo.h:66