|
4 | 4 | using System.IO; |
5 | 5 | using System.Reflection; |
6 | 6 | using System.Runtime.InteropServices; |
| 7 | +using System.Threading; |
7 | 8 | using System.Threading.Tasks; |
8 | 9 |
|
9 | 10 | namespace Akka.MultiNode.RemoteHost |
10 | 11 | { |
11 | 12 | public static class RemoteHost |
12 | 13 | { |
13 | 14 | #region Static functions |
14 | | - |
15 | | - public static Process Start(Action action, Action<RemoteHostOptions> configure = null) |
16 | | - => Start(GetMethodInfo(action), Array.Empty<string>(), configure).process; |
17 | | - |
18 | | - public static Process Start(Action<string[]> action, string[] args, Action<RemoteHostOptions> configure = null) |
19 | | - => Start(GetMethodInfo(action), args ?? throw new ArgumentNullException(nameof(args)), configure).process; |
20 | | - |
21 | | - public static Process Start(Func<int> action, Action<RemoteHostOptions> configure = null) |
22 | | - => Start(GetMethodInfo(action), Array.Empty<string>(), configure).process; |
23 | | - |
24 | | - public static Process Start(Func<string[], int> action, string[] args, Action<RemoteHostOptions> configure = null) |
25 | | - => Start(GetMethodInfo(action), args ?? throw new ArgumentNullException(nameof(args)), configure).process; |
26 | | - |
27 | | - public static Process Start(Func<Task> action, Action<RemoteHostOptions> configure = null) |
28 | | - => Start(GetMethodInfo(action), Array.Empty<string>(), configure).process; |
29 | | - |
30 | | - public static Process Start(Func<string[], Task> action, string[] args, Action<RemoteHostOptions> configure = null) |
31 | | - => Start(GetMethodInfo(action), args ?? throw new ArgumentNullException(nameof(args)), configure).process; |
32 | | - |
33 | | - public static Process Start(Func<Task<int>> action, Action<RemoteHostOptions> configure = null) |
34 | | - => Start(GetMethodInfo(action), Array.Empty<string>(), configure).process; |
35 | | - |
36 | | - public static Process Start(Func<string[], Task<int>> action, string[] args, Action<RemoteHostOptions> configure = null) |
37 | | - => Start(GetMethodInfo(action), args ?? throw new ArgumentNullException(nameof(args)), configure).process; |
38 | | - |
39 | | - public static void Run(Action action, Action<RemoteHostOptions> configure = null) |
40 | | - => Start(GetMethodInfo(action), Array.Empty<string>(), configure, waitForExit: true); |
41 | | - |
42 | | - public static void Run(Action<string[]> action, string[] args, Action<RemoteHostOptions> configure = null) |
43 | | - => Start(GetMethodInfo(action), args ?? throw new ArgumentNullException(nameof(args)), configure, waitForExit: true); |
44 | | - |
45 | | - public static void Run(Func<int> action, Action<RemoteHostOptions> configure = null) |
46 | | - => Start(GetMethodInfo(action), Array.Empty<string>(), configure, waitForExit: true); |
47 | | - |
48 | | - public static void Run(Func<string[], int> action, string[] args, Action<RemoteHostOptions> configure = null) |
49 | | - => Start(GetMethodInfo(action), args ?? throw new ArgumentNullException(nameof(args)), configure, waitForExit: true); |
50 | | - |
51 | | - public static void Run(Func<Task> action, Action<RemoteHostOptions> configure = null) |
52 | | - => Start(GetMethodInfo(action), Array.Empty<string>(), configure, waitForExit: true); |
53 | | - |
54 | | - public static void Run(Func<string[], Task> action, string[] args, Action<RemoteHostOptions> configure = null) |
55 | | - => Start(GetMethodInfo(action), args ?? throw new ArgumentNullException(nameof(args)), configure, waitForExit: true); |
56 | | - |
57 | | - public static void Run(Func<Task<int>> action, Action<RemoteHostOptions> configure = null) |
58 | | - => Start(GetMethodInfo(action), Array.Empty<string>(), configure, waitForExit: true); |
59 | | - |
60 | | - public static void Run(Func<string[], Task<int>> action, string[] args, Action<RemoteHostOptions> configure = null) |
61 | | - => Start(GetMethodInfo(action), args ?? throw new ArgumentNullException(nameof(args)), configure, waitForExit: true); |
62 | | - |
63 | | - public static Task RunAsync(Action action, Action<RemoteHostOptions> configure = null) |
64 | | - => Start(GetMethodInfo(action), Array.Empty<string>(), configure, returnTask: true).exitedTask; |
65 | | - |
66 | | - public static Task RunAsync(Action<string[]> action, string[] args, Action<RemoteHostOptions> configure = null) |
67 | | - => Start(GetMethodInfo(action), args ?? throw new ArgumentNullException(nameof(args)), configure, returnTask: true).exitedTask; |
68 | | - |
69 | | - public static Task RunAsync(Func<int> action, Action<RemoteHostOptions> configure = null) |
70 | | - => Start(GetMethodInfo(action), Array.Empty<string>(), configure, returnTask: true).exitedTask; |
71 | | - |
72 | | - public static Task RunAsync(Func<string[], int> action, string[] args, Action<RemoteHostOptions> configure = null) |
73 | | - => Start(GetMethodInfo(action), args ?? throw new ArgumentNullException(nameof(args)), configure, returnTask: true).exitedTask; |
74 | | - |
75 | | - public static Task RunAsync(Func<Task> action, Action<RemoteHostOptions> configure = null) |
76 | | - => Start(GetMethodInfo(action), Array.Empty<string>(), configure, returnTask: true).exitedTask; |
77 | | - |
78 | | - public static Task RunAsync(Func<string[], Task> action, string[] args, Action<RemoteHostOptions> configure = null) |
79 | | - => Start(GetMethodInfo(action), args ?? throw new ArgumentNullException(nameof(args)), configure, returnTask: true).exitedTask; |
80 | | - |
81 | | - public static Task RunAsync(Func<Task<int>> action, Action<RemoteHostOptions> configure = null) |
82 | | - => Start(GetMethodInfo(action), Array.Empty<string>(), configure, returnTask: true).exitedTask; |
83 | | - |
84 | | - public static Task RunAsync(Func<string[], Task<int>> action, string[] args, Action<RemoteHostOptions> configure = null) |
85 | | - => Start(GetMethodInfo(action), args ?? throw new ArgumentNullException(nameof(args)), configure, returnTask: true).exitedTask; |
| 15 | + public static (Process, Task) RunProcessAsync( |
| 16 | + Func<string[], Task<int>> action, |
| 17 | + string[] args, |
| 18 | + Action<RemoteHostOptions> configure = null, |
| 19 | + CancellationToken token = default) |
| 20 | + => Start(GetMethodInfo(action), args ?? throw new ArgumentNullException(nameof(args)), configure, token); |
86 | 21 |
|
87 | 22 | private static (Process process, Task exitedTask) Start( |
88 | 23 | MethodInfo method, |
89 | 24 | string[] args, |
90 | 25 | Action<RemoteHostOptions> configure, |
91 | | - bool waitForExit = false, |
92 | | - bool returnTask = false) |
| 26 | + CancellationToken token = default) |
93 | 27 | { |
94 | | - Process process = null; |
| 28 | + var process = new Process(); |
| 29 | + RemoteHostOptions options; |
95 | 30 | try |
96 | 31 | { |
97 | | - process = new Process(); |
98 | | - |
99 | | - var options = new RemoteHostOptions(process.StartInfo); |
| 32 | + options = new RemoteHostOptions(process.StartInfo); |
100 | 33 | ConfigureProcessStartInfoForMethodInvocation(method, args, options.StartInfo); |
101 | 34 | configure?.Invoke(options); |
102 | | - |
103 | | - TaskCompletionSource<bool> tcs = null; |
104 | | - if (returnTask) |
105 | | - { |
106 | | - tcs = new TaskCompletionSource<bool>(); |
107 | | - } |
108 | | - |
109 | | - if (options.OnExit != null || tcs != null) |
| 35 | + } |
| 36 | + catch |
| 37 | + { |
| 38 | + process.Dispose(); |
| 39 | + throw; |
| 40 | + } |
| 41 | + |
| 42 | + var tcs = new TaskCompletionSource<bool>(); |
| 43 | + if (token != default) |
| 44 | + { |
| 45 | + token.Register(() => |
110 | 46 | { |
111 | | - process.EnableRaisingEvents = true; |
112 | | - process.Exited += (_1, _2) => |
| 47 | + try |
113 | 48 | { |
114 | | - options.OnExit(process); |
115 | | - |
116 | | - if (tcs != null) |
117 | | - { |
118 | | - tcs?.SetResult(true); |
119 | | - process.Dispose(); |
120 | | - } |
121 | | - }; |
122 | | - } |
123 | | - |
124 | | - if (options.OutputDataReceived != null) |
125 | | - { |
126 | | - process.OutputDataReceived += options.OutputDataReceived; |
127 | | - options.StartInfo.RedirectStandardOutput = true; |
128 | | - } |
129 | | - |
130 | | - if (options.ErrorDataReceived != null) |
131 | | - { |
132 | | - process.ErrorDataReceived += options.ErrorDataReceived; |
133 | | - options.StartInfo.RedirectStandardError = true; |
134 | | - } |
| 49 | + process.Kill(); |
| 50 | + } catch{} |
| 51 | + }); |
| 52 | + } |
135 | 53 |
|
136 | | - process.Start(); |
137 | | - |
138 | | - if (options.OutputDataReceived != null) |
139 | | - { |
140 | | - process.BeginOutputReadLine(); |
141 | | - } |
| 54 | + process.EnableRaisingEvents = true; |
| 55 | + process.Exited += (_1, _2) => |
| 56 | + { |
| 57 | + options.OnExit(process); |
142 | 58 |
|
143 | | - if (options.ErrorDataReceived != null) |
144 | | - { |
145 | | - process.BeginErrorReadLine(); |
146 | | - } |
| 59 | + tcs.SetResult(true); |
| 60 | + process.Dispose(); |
| 61 | + }; |
147 | 62 |
|
148 | | - if (waitForExit) |
149 | | - { |
150 | | - process.WaitForExit(); |
151 | | - } |
| 63 | + if (options.OutputDataReceived != null) |
| 64 | + { |
| 65 | + process.OutputDataReceived += options.OutputDataReceived; |
| 66 | + options.StartInfo.RedirectStandardOutput = true; |
| 67 | + } |
152 | 68 |
|
153 | | - return (process, tcs?.Task); |
| 69 | + if (options.ErrorDataReceived != null) |
| 70 | + { |
| 71 | + process.ErrorDataReceived += options.ErrorDataReceived; |
| 72 | + options.StartInfo.RedirectStandardError = true; |
154 | 73 | } |
155 | | - catch |
| 74 | + |
| 75 | + process.Start(); |
| 76 | + |
| 77 | + if (options.OutputDataReceived != null) |
156 | 78 | { |
157 | | - process?.Dispose(); |
158 | | - throw; |
| 79 | + process.BeginOutputReadLine(); |
159 | 80 | } |
160 | | - finally |
| 81 | + |
| 82 | + if (options.ErrorDataReceived != null) |
161 | 83 | { |
162 | | - if (waitForExit) |
163 | | - { |
164 | | - process?.Dispose(); |
165 | | - } |
| 84 | + process.BeginErrorReadLine(); |
166 | 85 | } |
| 86 | + |
| 87 | + return (process, tcs.Task); |
167 | 88 | } |
168 | 89 |
|
169 | 90 | private static void ConfigureProcessStartInfoForMethodInvocation( |
|
0 commit comments