Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2924_DotNetCoreMigration/HeuristicLab.PluginInfrastructure/3.3/Isolation/Runner.cs

Last change on this file was 17013, checked in by dpiringe, 5 years ago

#2924:

  • some changes in CLIApplication.cs to reduce unnecessary allocation of string objects
  • renamed AppTest to ConsoleOptimizer and fixed race condition
  • replaced enum RunnerJob with class RunnerMessage for more control of saved data
  • changed usage of BinaryFormatter with HEAL.Attic, following types are now Storable:
    • ConsoleOptimizer
    • InspectApplication
    • ApplicationBase
    • ApplicationRunner
    • AssemblyInfo
    • Runner
    • UniPath
    • RunnerMessage
  • switched QuietMode from ApplicationRunner to IRunner
  • DockerRunnerHost can now automatically build docker images for linux and windows containers (also identifies which container type is active) -> removes the condition to have the image preinstalled
    • to achieve this, there are two new folders DockerLinuxBuild and DockerWindowsBuild included in build output, which include Dockerfiles to build images for linux and windows container
  • added NuGet package System.CodeDom to project HeuristicLab.Scripting-3.3
  • added method Send(RunnerMessage) to IRunnerHost and transferred methods Pause and Resume to IRunner
  • added internal reference of RunnerHost in Runner
  • added abstract method SendMessage(RunnerMessage) in RunnerHost which gets called from method Send(RunnerMessage)
  • because of a Google.Protobuf "bug", RunnerMessages cannot get serialized/deserialized directly on stream -> workaround with a byte array, which gets written and read
    • additionally, the length of the array gets sent first (length as integer -> 4 bytes)
    • static method in RunnerMessage to read a message from a stream
    • the method SendMessage(RunnerMessage) in RunnerHost implements this functionality
File size: 2.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Threading;
5using System.Threading.Tasks;
6using HEAL.Attic;
7
8namespace HeuristicLab.PluginInfrastructure {
9  [StorableType(StorableMemberSelection.MarkedOnly, "D93CBE04-9847-417A-AAB5-7FBCA6A32247")]
10  public abstract class Runner : IRunner {
11
12    #region Vars
13    private Thread listener;
14    #endregion
15
16    #region Properties
17    [Storable]
18    public IEnumerable<AssemblyInfo> AssembliesToLoad { get; set; }
19
20    internal RunnerHost Host { get; set; }
21    #endregion
22
23    public void Pause() {
24      var message = new PauseRunnerMessage();
25      if (Host != null) Host.Send(message);
26      else OnRunnerMessage(message);
27    }
28
29    public void Resume() {
30      var message = new ResumeRunnerMessage();
31      if (Host != null) Host.Send(message);
32      else OnRunnerMessage(message);
33    }
34
35    public void Cancel() {
36      var message = new CancelRunnerMessage();
37      if (Host != null) Host.Send(message);
38      else OnRunnerMessage(message);
39    }
40
41    public void Run() {
42      IPluginLoader loader = PluginLoaderFactory.Create();
43      loader.LoadPlugins(AssembliesToLoad);
44      Task t = Task.Run(Execute);
45      StartListener();
46      t.Wait();
47    }
48
49    protected abstract void Execute();
50    protected abstract void OnRunnerMessage(RunnerMessage message);
51
52    #region Helper
53
54    private void StartListener() {
55      listener = new Thread(() => {
56        Stream stdin = Console.OpenStandardInput();
57        DateTime lastMessageRecieved = DateTime.MinValue;
58        while (true) {
59          RunnerMessage message = RunnerMessage.ReadMessageFromStream(stdin);
60          if (DateTime.Compare(lastMessageRecieved, message.SendTime) < 0) {
61            OnRunnerMessage(message);
62            lastMessageRecieved = message.SendTime;
63          }
64        }
65      });
66      listener.IsBackground = true;
67      listener.Start();
68    }
69    #endregion
70  }
71}
Note: See TracBrowser for help on using the repository browser.