/
ProcessUtils.cs
341 lines (303 loc) · 10.5 KB
/
ProcessUtils.cs
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using ServiceStack.Text;
namespace ServiceStack;
/// <summary>
/// Async Process Helper
/// - https://gist.github.com/Indigo744/b5f3bd50df4b179651c876416bf70d0a
/// </summary>
public static class ProcessUtils
{
/// <summary>
/// .NET Core / Win throws Win32Exception (193): The specified executable is not a valid application for this OS platform.
/// This method converts it to a cmd.exe /c execute to workaround this
/// </summary>
public static ProcessStartInfo ConvertToCmdExec(this ProcessStartInfo startInfo)
{
var to = new ProcessStartInfo {
FileName = Env.IsWindows
? "cmd.exe"
: "/bin/bash",
WorkingDirectory = startInfo.WorkingDirectory,
Arguments = Env.IsWindows
? $"/c \"\"{startInfo.FileName}\" {startInfo.Arguments}\""
: $"-c \"{startInfo.FileName} {startInfo.Arguments}\"",
};
return to;
}
/// <summary>
/// Returns path of executable if exists within PATH
/// </summary>
public static string FindExePath(string exeName)
{
try
{
var p = new Process
{
StartInfo =
{
UseShellExecute = false,
FileName = Env.IsWindows
? "where" //Win 7/Server 2003+
: "which", //macOS / Linux
Arguments = exeName,
RedirectStandardOutput = true
}
};
p.Start();
var output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
if (p.ExitCode == 0)
{
// just return first match
var fullPath = output.Substring(0, output.IndexOf(Environment.NewLine, StringComparison.Ordinal));
if (!string.IsNullOrEmpty(fullPath))
{
return fullPath;
}
}
}
catch {}
return null;
}
/// <summary>
/// Run the command with the OS's command runner
/// </summary>
public static string RunShell(string arguments, string workingDir=null)
{
if (string.IsNullOrEmpty(arguments))
throw new ArgumentNullException(nameof(arguments));
if (Env.IsWindows)
{
var cmdArgs = "/C " + arguments;
return Run("cmd.exe", cmdArgs, workingDir);
}
else
{
var escapedArgs = arguments.Replace("\"", "\\\"");
var cmdArgs = $"-c \"{escapedArgs}\"";
return Run("/bin/bash", cmdArgs, workingDir);
}
}
/// <summary>
/// Run the process and return the Standard Output, any Standard Error output will throw an Exception
/// </summary>
public static string Run(string fileName, string arguments=null, string workingDir=null)
{
var process = CreateProcess(fileName, arguments, workingDir);
using (process)
{
process.StartInfo.RedirectStandardError = true;
process.Start();
var output = process.StandardOutput.ReadToEnd();
var error = process.StandardError.ReadToEnd();
process.WaitForExit();
process.Close();
if (!string.IsNullOrEmpty(error))
throw new Exception($"`{fileName} {process.StartInfo.Arguments}` command failed, stderr: " + error + ", stdout:" + output);
return output;
}
}
public static Process CreateProcess(string fileName, string arguments, string workingDir)
{
var process = new Process
{
StartInfo =
{
FileName = fileName,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
}
};
if (arguments != null)
process.StartInfo.Arguments = arguments;
if (workingDir != null)
process.StartInfo.WorkingDirectory = workingDir;
return process;
}
/// <summary>
/// Run the command with the OS's command runner
/// </summary>
public static async Task RunShellAsync(string arguments, string workingDir=null, int? timeoutMs = null,
Action<string> onOut=null, Action<string> onError=null)
{
if (string.IsNullOrEmpty(arguments))
throw new ArgumentNullException(nameof(arguments));
if (Env.IsWindows)
{
var cmdArgs = "/C " + arguments;
await RunAsync(CreateProcess("cmd.exe", cmdArgs, workingDir).StartInfo, timeoutMs, onOut, onError);
}
else
{
var escapedArgs = arguments.Replace("\"", "\\\"");
var cmdArgs = $"-c \"{escapedArgs}\"";
await RunAsync(CreateProcess("/bin/bash", cmdArgs, workingDir).StartInfo, timeoutMs, onOut, onError);
}
}
/// <summary>
/// Run a Process asynchronously, returning entire captured process output, whilst streaming stdOut, stdErr callbacks
/// </summary>
public static async Task<ProcessResult> RunAsync(ProcessStartInfo startInfo, int? timeoutMs = null,
Action<string> onOut=null, Action<string> onError=null)
{
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
using var process = new Process
{
StartInfo = startInfo,
EnableRaisingEvents = true,
};
// List of tasks to wait for a whole process exit
var processTasks = new List<Task>();
// === EXITED Event handling ===
var processExitEvent = new TaskCompletionSource<object>();
process.Exited += (sender, args) => {
processExitEvent.TrySetResult(true);
};
processTasks.Add(processExitEvent.Task);
long callbackTicks = 0;
// === STDOUT handling ===
var stdOutBuilder = StringBuilderCache.Allocate();
var stdOutCloseEvent = new TaskCompletionSource<bool>();
process.OutputDataReceived += (s, e) => {
if (e.Data == null)
{
stdOutCloseEvent.TrySetResult(true);
}
else
{
stdOutBuilder.AppendLine(e.Data);
if (onOut != null)
{
var swCallback = Stopwatch.StartNew();
onOut(e.Data);
callbackTicks += swCallback.ElapsedTicks;
}
}
};
processTasks.Add(stdOutCloseEvent.Task);
// === STDERR handling ===
var stdErrBuilder = StringBuilderCacheAlt.Allocate();
var stdErrCloseEvent = new TaskCompletionSource<bool>();
process.ErrorDataReceived += (s, e) => {
if (e.Data == null)
{
stdErrCloseEvent.TrySetResult(true);
}
else
{
stdErrBuilder.AppendLine(e.Data);
if (onError != null)
{
var swCallback = Stopwatch.StartNew();
onError(e.Data);
callbackTicks += swCallback.ElapsedTicks;
}
}
};
processTasks.Add(stdErrCloseEvent.Task);
// === START OF PROCESS ===
var sw = Stopwatch.StartNew();
var result = new ProcessResult {
StartAt = DateTime.UtcNow,
};
if (!process.Start())
{
result.ExitCode = process.ExitCode;
return result;
}
// Reads the output stream first as needed and then waits because deadlocks are possible
process.BeginOutputReadLine();
process.BeginErrorReadLine();
// === ASYNC WAIT OF PROCESS ===
// Process completion = exit AND stdout (if defined) AND stderr (if defined)
var processCompletionTask = Task.WhenAll(processTasks);
// Task to wait for exit OR timeout (if defined)
var awaitingTask = timeoutMs.HasValue
? Task.WhenAny(Task.Delay(timeoutMs.Value), processCompletionTask)
: Task.WhenAny(processCompletionTask);
// Let's now wait for something to end...
if ((await awaitingTask.ConfigureAwait(false)) == processCompletionTask)
{
// -> Process exited cleanly
result.ExitCode = process.ExitCode;
}
else
{
// -> Timeout, let's kill the process
try
{
process.Kill();
}
catch
{
// ignored
}
}
// Read stdout/stderr
result.EndAt = DateTime.UtcNow;
if (callbackTicks > 0)
{
var callbackMs = (callbackTicks / Stopwatch.Frequency) * 1000;
result.CallbackDurationMs = callbackMs;
result.DurationMs = sw.ElapsedMilliseconds - callbackMs;
}
else
{
result.DurationMs = sw.ElapsedMilliseconds;
}
result.StdOut = StringBuilderCache.ReturnAndFree(stdOutBuilder);
result.StdErr = StringBuilderCacheAlt.ReturnAndFree(stdErrBuilder);
return result;
}
public static ProcessResult CreateErrorResult(Exception e)
{
return new ProcessResult {
StartAt = DateTime.UtcNow,
EndAt = DateTime.UtcNow,
DurationMs = 0,
CallbackDurationMs = 0,
StdErr = e.ToString(),
ExitCode = -1,
};
}
}
/// <summary>
/// Run process result
/// </summary>
public class ProcessResult
{
/// <summary>
/// Exit code
/// <para>If NULL, process exited due to timeout</para>
/// </summary>
public int? ExitCode { get; set; } = null;
/// <summary>
/// Standard error stream
/// </summary>
public string StdErr { get; set; }
/// <summary>
/// Standard output stream
/// </summary>
public string StdOut { get; set; }
/// <summary>
/// UTC Start
/// </summary>
public DateTime StartAt { get; set; }
/// <summary>
/// UTC End
/// </summary>
public DateTime EndAt { get; set; }
/// <summary>
/// Duration (ms)
/// </summary>
public long DurationMs { get; set; }
/// <summary>
/// Duration (ms)
/// </summary>
public long? CallbackDurationMs { get; set; }
}