Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
MonitoredProcess.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Containers/UnrealString.h"
6#include "CoreTypes.h"
7#include "Delegates/Delegate.h"
8#include "HAL/CriticalSection.h"
9#include "HAL/PlatformProcess.h"
10#include "HAL/Runnable.h"
11#include "Misc/DateTime.h"
12#include "Misc/SingleThreadRunnable.h"
13#include "Misc/Timespan.h"
14
15class FRunnableThread;
16
17/**
18 * Declares a delegate that is executed when a monitored process completed.
19 *
20 * The first parameter is the process return code.
21 */
22DECLARE_DELEGATE_OneParam(FOnMonitoredProcessCompleted, int32)
23
24/**
25 * Declares a delegate that is executed when a monitored process produces output.
26 *
27 * The first parameter is the produced output.
28 */
29DECLARE_DELEGATE_OneParam(FOnMonitoredProcessOutput, FString)
30
31
32/**
33 * Implements an external process that can be monitored.
34 */
37{
38public:
39
40 /**
41 * Creates a new monitored process.
42 *
43 * @param InURL The URL of the executable to launch.
44 * @param InParams The command line parameters.
45 * @param InHidden Whether the window of the process should be hidden.
46 * @param InCreatePipes Whether the output should be redirected to the caller.
47 */
48 FMonitoredProcess( const FString& InURL, const FString& InParams, bool InHidden, bool InCreatePipes = true );
49
50 /**
51 * Creates a new monitored process.
52 *
53 * @param InURL The URL of the executable to launch.
54 * @param InParams The command line parameters.
55 * @param InHidden Whether the window of the process should be hidden.
56 * @param InWorkingDir The URL of the working dir where the executable should launch.
57 * @param InCreatePipes Whether the output should be redirected to the caller.
58 */
59 FMonitoredProcess( const FString& InURL, const FString& InParams, const FString& InWorkingDir, bool InHidden, bool InCreatePipes = true );
60
61 /** Destructor. */
63
64public:
65
66 /**
67 * Cancels the process.
68 *
69 * @param InKillTree Whether to kill the entire process tree when canceling this process.
70 */
71 void Cancel( bool InKillTree = false )
72 {
73 Canceling = true;
74 KillTree = InKillTree;
75 }
76
77 /**
78 * Gets the duration of time that the task has been running.
79 *
80 * @return Time duration.
81 */
83
84 /**
85 * Gets the Process Handle. The instance can be invalid if the process was not created.
86 *
87 * @return The Process Handle
88 */
90 {
91 return ProcessHandle;
92 }
93
94 /**
95 * Returns the commandline of the process which will be executed if Launch is called
96 */
98 {
99 return FString::Printf(TEXT("%s %s"), *URL, *Params);
100 }
101
102 /**
103 * Checks whether the process is still running. In single threaded mode, this will tick the thread processing
104 *
105 * @return true if the process is running, false otherwise.
106 */
107 bool Update();
108
109 /** Launches the process. */
110 virtual bool Launch();
111
112 /**
113 * Sets the sleep interval to be used in the main thread loop.
114 *
115 * @param InSleepInterval The Sleep interval to use.
116 */
117 void SetSleepInterval( float InSleepInterval )
118 {
119 SleepInterval = InSleepInterval;
120 }
121
122public:
123
124 /**
125 * Returns a delegate that is executed when the process has been canceled.
126 *
127 * @return The delegate.
128 */
129 FSimpleDelegate& OnCanceled()
130 {
131 return CanceledDelegate;
132 }
133
134 /**
135 * Returns a delegate that is executed when a monitored process completed.
136 *
137 * @return The delegate.
138 */
139 FOnMonitoredProcessCompleted& OnCompleted()
140 {
141 return CompletedDelegate;
142 }
143
144 /**
145 * Returns a delegate that is executed when a monitored process produces output.
146 *
147 * @return The delegate.
148 */
149 FOnMonitoredProcessOutput& OnOutput()
150 {
151 return OutputDelegate;
152 }
153
154 /**
155 * Returns the return code from the exited process
156 *
157 * @return Process return code
158 */
159 int GetReturnCode() const
160 {
161 return ReturnCode;
162 }
163
164 /**
165 * Returns the full output, wihtout needing to hookup a delegate and buffer it externally. Note that if OutputDelegate is
166 * bound, this _will not_ have the entire output
167 */
169 {
170 return OutputBuffer;
171 }
172
173public:
174
175 // FRunnable interface
176
177 virtual bool Init() override
178 {
179 return true;
180 }
181
182 virtual uint32 Run() override;
183
184 virtual void Stop() override
185 {
186 Cancel();
187 }
188
189 virtual void Exit() override { }
190
192 {
193 return this;
194 }
195
196protected:
197
198 /**
199 * FSingleThreadRunnable interface
200 */
201 void Tick() override;
202
203 /**
204 * Processes the given output string.
205 *
206 * @param Output The output string to process.
207 */
208 void ProcessOutput( const FString& Output );
209
210protected:
212
213
214 // Whether the process is being canceled. */
215 bool Canceling = false;
216
217 // Holds the time at which the process ended. */
219
220 // Whether the window of the process should be hidden. */
221 bool Hidden = false;
222
223 // Whether to kill the entire process tree when cancelling this process. */
224 bool KillTree = false;
225
226 // Holds the command line parameters. */
228
229 // Holds the handle to the process. */
231
232 // Holds the read pipe. */
233 void* ReadPipe = nullptr;
234
235 // Holds the return code. */
236 int ReturnCode = 0;
237
238 // Holds the time at which the process started. */
240
241 // Holds the monitoring thread object. */
243
244 // Is the thread running?
246
247 // Holds the URL of the executable to launch. */
249
250 // Holds the URL of the working dir for the process. */
252
253 // Holds the write pipe. */
254 void* WritePipe = nullptr;
255
256 // Holds if we should create pipes
257 bool bCreatePipes = false;
258
259 // Sleep interval to use
260 float SleepInterval = 0.01f;
261
262 // Buffered output text which does not contain a newline
264
265protected:
266
267 // Holds a delegate that is executed when the process has been canceled. */
268 FSimpleDelegate CanceledDelegate;
269
270 // Holds a delegate that is executed when a monitored process completed. */
271 FOnMonitoredProcessCompleted CompletedDelegate;
272
273 // Holds a delegate that is executed when a monitored process produces output. */
274 FOnMonitoredProcessOutput OutputDelegate;
275};
276
277
279{
280public:
281 /**
282 * Get the host-platform-specific path to the UAT running script
283 */
285
286public:
287 FSerializedUATProcess(const FString& RunUATCommandline);
288
289 /**
290 * Run UAT, serially with other FSerializedUATProcess objects. Because the actual call is delayed, this will
291 * always return true, and the LaunchFailedDelegate will be called later if an error happens
292 */
293 virtual bool Launch() override;
294
295 /**
296 * Returns a delegate that is executed when the process has been canceled.
297 *
298 * @return The delegate.
299 */
300 FSimpleDelegate& OnLaunchFailed()
301 {
303 }
304
305
306private:
307
310 static void CancelQueue();
311
312 // When this one completes, run the next in line
314
315 // Holds a delegate that is executed when the process fails to launch (delayed, in a thread). Used in place of the return value
316 // of Launch in the parent class, since it's async
317 FSimpleDelegate LaunchFailedDelegate;
318
320 static bool bHasSucceededOnce;
322};
#define TSAN_ATOMIC(Type)
#define DECLARE_DELEGATE_OneParam(DelegateName, Param1Type)
#define TEXT(x)
Definition Platform.h:1108
FWindowsCriticalSection FCriticalSection
void ProcessOutput(const FString &Output)
FMonitoredProcess(const FString &InURL, const FString &InParams, bool InHidden, bool InCreatePipes=true)
virtual ~FMonitoredProcess()
virtual uint32 Run() override
void SetSleepInterval(float InSleepInterval)
virtual bool Init() override
virtual void Stop() override
int GetReturnCode() const
FOnMonitoredProcessOutput & OnOutput()
virtual FSingleThreadRunnable * GetSingleThreadInterface() override
FOnMonitoredProcessOutput OutputDelegate
virtual bool Launch()
FTimespan GetDuration() const
FOnMonitoredProcessCompleted & OnCompleted()
FSimpleDelegate & OnCanceled()
const FString & GetFullOutputWithoutDelegate() const
FRunnableThread * Thread
FSimpleDelegate CanceledDelegate
void Tick() override
virtual void Exit() override
FOnMonitoredProcessCompleted CompletedDelegate
FProcHandle ProcessHandle
void Cancel(bool InKillTree=false)
FMonitoredProcess(const FString &InURL, const FString &InParams, const FString &InWorkingDir, bool InHidden, bool InCreatePipes=true)
FString GetCommandline() const
FProcHandle GetProcessHandle() const
static void CancelQueue()
FSerializedUATProcess * NextProcessToRun
static FString GetUATPath()
virtual bool Launch() override
static FCriticalSection Serializer
FSimpleDelegate LaunchFailedDelegate
static FSerializedUATProcess * HeadProcess
FSimpleDelegate & OnLaunchFailed()
FSerializedUATProcess(const FString &RunUATCommandline)
FDateTime(int64 InTicks)
Definition DateTime.h:89