File: ProcessRunner.h

package info (click to toggle)
poco 1.14.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 56,460 kB
  • sloc: cpp: 340,542; ansic: 245,601; makefile: 1,742; yacc: 1,005; sh: 698; sql: 312; lex: 282; xml: 128; perl: 29; python: 24
file content (231 lines) | stat: -rw-r--r-- 6,079 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//
// ProcessRunner.h
//
// Library: Foundation
// Package: Processes
// Module:  ProcessRunner
//
// Definition of the ProcessRunner class.
//
// Copyright (c) 2023, Applied Informatics Software Engineering GmbH.
// Aleph ONE Software Engineering d.o.o.,
// and Contributors.
//
// SPDX-License-Identifier:    BSL-1.0
//


#ifndef Foundation_ProcessRunner_INCLUDED
#define Foundation_ProcessRunner_INCLUDED


#include "Poco/Foundation.h"
#include "Poco/Process.h"
#include "Poco/ProcessOptions.h"
#include "Poco/Runnable.h"
#include "Poco/Thread.h"
#include "Poco/Format.h"
#include "Poco/Stopwatch.h"
#include <atomic>
#include <vector>


namespace Poco {


class Foundation_API ProcessRunner: public Poco::Runnable
	/// ProcessRunner is a wrapper class for `Poco::ProcessHandle.
	/// It starts and terminates a process with enabled or disabled (default)
	/// `stdio` pipes, and optionally waits for process PID to be created before
	/// returning control to the caller. The process is spawned from an
	/// internal thread. Starting/stopping the process may block up to
	/// a certain (configurable) period of time.
	///
	/// ProcessRunner can hold and control only one process at a time, which
	/// can be started/stopped multiple times during the ProcessRunner lifetime.
{
public:
	using Args = Poco::Process::Args;
	using PID = Poco::ProcessHandle::PID;

	static const int NO_OUT = Poco::PROCESS_CLOSE_STDOUT|Poco::PROCESS_CLOSE_STDERR;
		/// Constant to prevent std out and err from being received from the process.

	ProcessRunner(const std::string& cmd,
		const Args& args,
		const std::string& pidFile = "",
		int options = NO_OUT,
		int timeout = 10, /*seconds*/
		bool startProcess = true,
		const Args& pidArgFmt = pidArgFormat());
		/// Creates the ProcessRunner.
		///
		/// If `pidFile` is not empty, the starting of the process waits
		/// until the pid file has been updated with the new pid, and
		/// the stopping of the process waits until the pid file is gone.
		/// Waiting is terminated after timeout seconds.
		///
		/// If `pidFile` is empty and `pidArgFmt` is not empty, autodetect
		/// of PID file from `args` is attempted; the default PID file
		/// argument format corresponds to the one used by
		/// `Poco::Util::Application`
		///
		/// The `options` are passed to the process, defaulting to
		/// closed stdio output pipes.
		///
		/// The `timeout` in seconds determines how long the ProcessRunner
		/// waits for the process to start; if PID file name is provided or
		/// autodetected from arguments, ProcessRunner will wait until the file
		/// exists and contains the process PID or timeout expires (in which
		/// case a TimeoutException is thrown).
		///
		/// If `startProcess` is true, the process is started on object creation.

	~ProcessRunner();
		/// Destroys the ProcessRunner.

	PID pid() const;
		/// Returns the process PID.

	const std::string& pidFile() const;
		/// Returns the process PID filename.
		/// Returns empty string when pid filename
		/// is not specified at construction, either
		/// explicitly, or implicitly through
		/// command line argument.

	bool running() const;
		/// Returns true if process is running.

	void start();
		/// Starts the process and waits for it to be fully initialized.
		/// Process initialization completion is indicated by a new pid in
		/// the pid file (if specified at construction, otherwise there
		/// is no wating for pid).
		/// If pid file is not specified, there is no waiting.
		///
		/// Attempting to start a started process results in
		/// Poco::InvalidAccessException being thrown.

	void stop();
		/// Stops the process.
		///
		/// Calling stop() on a stopped process is a no-op.

	std::string cmdLine() const;
		/// Returns process full command line.

	int result() const;
		/// Returns process return code.

	int runCount() const;
		/// Returns the number of times the process has been executed.

	const std::string& error() const;
		/// Returns the error message.

private:
	static const Poco::ProcessHandle::PID INVALID_PID = -1;
	static const int RESULT_UNKNOWN = -1;

	static Args pidArgFormat()
	{
#if defined(POCO_OS_FAMILY_WINDOWS)
		return Args{"-p", "--pidfile=", "/p", "/pidfile="};
#else
		return Args{"-p", "--pidfile="};
#endif
	}


	void run();
		/// Starts the process and waits for it to be fully initialized.
		/// Process initialization completion is indicated by new pid in
		/// the pid file. If pid file is not specified, there is no waiting.

	void checkError();
		/// If timeout is exceeded, throws TimeoutException with `msg`
		/// message.

	void checkTimeout(const std::string& msg);
		/// If timeout is exceeded, throws TimeoutException with `msg`
		/// message.

	void checkStatus(const std::string& msg, bool tOut = true);
		/// If there were andy errors during process start/stop,
		/// throws RuntimeException with the error message;
		/// otherwise, if tOut is true and timeout is exceeded, throws
		/// TimeoutException with `msg` message.

	void setError(const std::string& msg);
		/// Sets the error message.

	Poco::Thread _t;
	std::string _cmd;
	Args _args;
	std::atomic<PID> _pid;
	std::string _pidFile;
	int _options;
	int _timeout;
	std::atomic<Poco::ProcessHandle*> _pPH;
	std::atomic<bool> _started;
	std::atomic<int> _rc;
	std::atomic<int> _runCount;
	Stopwatch _sw;
	std::string _error;
	mutable Poco::FastMutex _mutex;
};


//
// inlines
//

inline const std::string& ProcessRunner::pidFile() const
{
	return _pidFile;
}


inline bool ProcessRunner::running() const
{
	return _pid != INVALID_PID;
}


inline ProcessRunner::PID ProcessRunner::pid() const
{
	return _pid;
}


inline int ProcessRunner::result() const
{
	return _rc;
}


inline int ProcessRunner::runCount() const
{
	return _runCount;
}


inline void ProcessRunner::setError(const std::string& msg)
{
	_error = Poco::format("ProcessRunner(%s): %s", cmdLine(), msg);
}


inline const std::string& ProcessRunner::error() const
{
	Poco::FastMutex::ScopedLock l(_mutex);

	return _error;
}


} // namespace Poco


#endif // Foundation_ProcessRunner_INCLUDED