[16984] | 1 | using System;
|
---|
| 2 | using System.Diagnostics;
|
---|
| 3 | using System.IO;
|
---|
| 4 | using System.Threading;
|
---|
| 5 | using System.Threading.Tasks;
|
---|
[17013] | 6 | using HEAL.Attic;
|
---|
[16984] | 7 |
|
---|
| 8 | namespace HeuristicLab.PluginInfrastructure {
|
---|
| 9 | /// <summary>
|
---|
| 10 | /// Enum for the different RunnerStates.
|
---|
| 11 | /// </summary>
|
---|
| 12 | public enum RunnerState {
|
---|
| 13 | Created,
|
---|
| 14 | Cancelled,
|
---|
| 15 | Starting,
|
---|
| 16 | Running,
|
---|
| 17 | Stopped
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | /// <summary>
|
---|
| 21 | /// Abstract <c>RunnerBase</c> class, which implements the IRunner interface.
|
---|
| 22 | /// Every specific runner implementation should inherit this class.
|
---|
| 23 | /// </summary>
|
---|
| 24 | public abstract class RunnerHost : IRunnerHost {
|
---|
| 25 |
|
---|
| 26 | #region Vars
|
---|
| 27 | protected Process process;
|
---|
[17013] | 28 | private ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
[16984] | 29 | #endregion
|
---|
| 30 |
|
---|
| 31 | #region Properties
|
---|
| 32 | /// <summary>
|
---|
[17013] | 33 | /// Set this to true, if console output should be disabled.
|
---|
| 34 | /// </summary>
|
---|
| 35 | public bool QuietMode { get; set; }
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 | /// <summary>
|
---|
[16984] | 39 | /// The programm, which should be used. For example 'docker'.
|
---|
| 40 | /// </summary>
|
---|
| 41 | protected string Program { get; private set; }
|
---|
| 42 | /// <summary>
|
---|
| 43 | /// The specific start arguments for the programm.
|
---|
| 44 | /// </summary>
|
---|
| 45 | protected string StartArgument { get; private set; }
|
---|
| 46 | protected string UserName { get; private set; }
|
---|
| 47 | protected string Password { get; private set; }
|
---|
| 48 | protected string Domain { get; private set; }
|
---|
[17013] | 49 | protected IRunner Runner { get; set; }
|
---|
[16984] | 50 |
|
---|
| 51 | public RunnerState State { get; protected set; } = RunnerState.Created;
|
---|
| 52 | #endregion
|
---|
| 53 |
|
---|
| 54 | #region Constructors
|
---|
[17013] | 55 |
|
---|
[16984] | 56 | protected RunnerHost(string program, string startArgument, string userName, string password, string domain) {
|
---|
| 57 | Program = program;
|
---|
| 58 | StartArgument = startArgument + " --StartAsRunnerHost";
|
---|
| 59 | UserName = userName;
|
---|
| 60 | Password = password;
|
---|
| 61 | Domain = domain;
|
---|
| 62 | }
|
---|
| 63 | #endregion
|
---|
| 64 |
|
---|
| 65 | public virtual void Run(IRunner runner) => RunAsync(runner, null).Wait();
|
---|
| 66 | public virtual async Task RunAsync(IRunner runner, CancellationToken? token = null) {
|
---|
| 67 | if (State != RunnerState.Created)
|
---|
| 68 | throw new InvalidOperationException("Runner must be in state 'Created'.");
|
---|
| 69 | State = RunnerState.Starting;
|
---|
[17013] | 70 | Runner = runner;
|
---|
[16984] | 71 | process = new Process {
|
---|
| 72 | StartInfo = new ProcessStartInfo {
|
---|
| 73 | FileName = Program,
|
---|
| 74 | Arguments = StartArgument,
|
---|
| 75 | UseShellExecute = false,
|
---|
| 76 | RedirectStandardOutput = true,
|
---|
| 77 | RedirectStandardInput = true,
|
---|
| 78 | RedirectStandardError = true,
|
---|
| 79 | CreateNoWindow = false,
|
---|
[17013] | 80 | UserName = string.IsNullOrEmpty(UserName) ? null : UserName, // TODO: accounts testen: https://docs.microsoft.com/en-us/windows/security/identity-protection/access-control/local-accounts#sec-localsystem
|
---|
[16984] | 81 | PasswordInClearText = string.IsNullOrEmpty(Password) ? null : Password,
|
---|
[16993] | 82 | Domain = string.IsNullOrEmpty(Domain) ? null : Domain,
|
---|
[16984] | 83 | WorkingDirectory = Directory.GetCurrentDirectory()
|
---|
| 84 | },
|
---|
| 85 | EnableRaisingEvents = true
|
---|
| 86 | };
|
---|
| 87 | process.Start();
|
---|
[17013] | 88 |
|
---|
[16984] | 89 | // registers a task for cancellation, prevents the use of polling (while-loop)
|
---|
| 90 | var task = RegisterCancellation(token.HasValue ? token.Value : CancellationToken.None);
|
---|
| 91 |
|
---|
[17013] | 92 | // set runnerhost in runner
|
---|
| 93 | Runner r = Runner as Runner;
|
---|
| 94 | if (r != null) r.Host = this;
|
---|
[16984] | 95 |
|
---|
[16993] | 96 | process.BeginOutputReadLine();
|
---|
| 97 | process.BeginErrorReadLine();
|
---|
| 98 |
|
---|
[17013] | 99 | if (!QuietMode) {
|
---|
[16984] | 100 | process.OutputDataReceived += (s, e) => Console.WriteLine(e.Data);
|
---|
| 101 | process.ErrorDataReceived += (s, e) => Console.WriteLine(e.Data);
|
---|
| 102 | }
|
---|
[17013] | 103 |
|
---|
| 104 | // write config to standardinput, runner listens on this and deserializes the config
|
---|
| 105 | SendMessage(new TransportRunnerMessage(Runner));
|
---|
| 106 |
|
---|
[16984] | 107 | State = RunnerState.Running;
|
---|
| 108 | if (await task) State = RunnerState.Cancelled;
|
---|
| 109 | else State = RunnerState.Stopped;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[17013] | 112 |
|
---|
| 113 | public void Send(RunnerMessage runnerMessage) {
|
---|
[16984] | 114 | if (State != RunnerState.Running) throw new InvalidOperationException("Runner must be in state 'Running'!");
|
---|
[17013] | 115 | SendMessage(runnerMessage);
|
---|
[16984] | 116 | }
|
---|
| 117 |
|
---|
[17013] | 118 | // because we need to transfer the runner with a TransportRunnerMessage in the starting state and the
|
---|
| 119 | // original send method should not be available until running state
|
---|
| 120 | protected virtual void SendMessage(RunnerMessage runnerMessage) {
|
---|
| 121 | runnerMessage.SendTime = DateTime.Now;
|
---|
| 122 | byte[] bytes = serializer.Serialize(runnerMessage);
|
---|
| 123 | byte[] size = BitConverter.GetBytes(bytes.Length);
|
---|
| 124 | process.StandardInput.BaseStream.Write(size, 0, size.Length);
|
---|
| 125 | process.StandardInput.BaseStream.Flush();
|
---|
| 126 | process.StandardInput.BaseStream.Write(bytes, 0, bytes.Length);
|
---|
| 127 | process.StandardInput.BaseStream.Flush();
|
---|
[16984] | 128 | }
|
---|
| 129 |
|
---|
| 130 | /// <summary>
|
---|
| 131 | /// Creates a new LinkedTokenSource and a TaskCompletionSource.
|
---|
| 132 | /// When the specified token gets cancelled, a cancel requests gets send to the childprocess.
|
---|
| 133 | /// Afterwards the main process waits for the exit of the child process and sets a result of the TaskCompletionSource.
|
---|
| 134 | /// When the child process gets finished without requested cancellation, the linked token gets cancelled and a result set.
|
---|
| 135 | /// </summary>
|
---|
[17013] | 136 | protected Task<bool> RegisterCancellation(CancellationToken token) {
|
---|
[16984] | 137 | if (process != null && State == RunnerState.Starting) {
|
---|
| 138 | var cts = CancellationTokenSource.CreateLinkedTokenSource(token);
|
---|
| 139 | var tcs = new TaskCompletionSource<bool>();
|
---|
| 140 |
|
---|
| 141 | process.Exited += (s, e) => cts.Cancel();
|
---|
| 142 | cts.Token.Register(() => {
|
---|
| 143 | if (!process.HasExited) {
|
---|
[17013] | 144 | Runner.Cancel();
|
---|
[16984] | 145 | process.WaitForExit();
|
---|
| 146 | }
|
---|
| 147 | tcs.SetResult(token.IsCancellationRequested);
|
---|
| 148 | });
|
---|
| 149 | return tcs.Task;
|
---|
| 150 | }
|
---|
| 151 | return null;
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | }
|
---|