/ ProjectPlugins / CodexPlugin / BinaryProcessControl.cs
BinaryProcessControl.cs
 1  using System.Diagnostics;
 2  using CodexClient;
 3  using Logging;
 4  
 5  namespace CodexPlugin
 6  {
 7      public class BinaryProcessControl : IProcessControl
 8      {
 9          private readonly LogFile logFile;
10          private readonly Process process;
11          private readonly CodexProcessConfig config;
12          private List<string> logBuffer = new List<string>();
13          private readonly object bufferLock = new object();
14          private readonly List<Task> streamTasks = new List<Task>();
15          private bool running;
16  
17          public BinaryProcessControl(ILog log, Process process, CodexProcessConfig config)
18          {
19              logFile = log.CreateSubfile(config.Name);
20  
21              running = true;
22              this.process = process;
23              this.config = config;
24              streamTasks.Add(Task.Run(() => ReadProcessStream(process.StandardOutput)));
25              streamTasks.Add(Task.Run(() => ReadProcessStream(process.StandardError)));
26              streamTasks.Add(Task.Run(() => WriteLog()));
27          }
28  
29          private void ReadProcessStream(StreamReader reader)
30          {
31              while (running)
32              {
33                  var line = reader.ReadLine();
34                  if (!string.IsNullOrEmpty(line))
35                  {
36                      lock (bufferLock)
37                      {
38                          logBuffer.Add(line);
39                      }
40                  }
41              }
42          }
43  
44          private void WriteLog()
45          {
46              while (running || logBuffer.Count > 0)
47              {
48                  if (logBuffer.Count > 0)
49                  {
50                      List<string> lines = null!;
51                      lock (bufferLock)
52                      {
53                          lines = logBuffer;
54                          logBuffer = new List<string>();
55                      }
56  
57                      logFile.WriteRawMany(lines);
58                  }
59                  else Thread.Sleep(100);
60              }
61          }
62  
63          public void DeleteDataDirFolder()
64          {
65              if (!Directory.Exists(config.DataDir)) throw new Exception("datadir not found");
66              Directory.Delete(config.DataDir, true);
67          }
68  
69          public IDownloadedLog DownloadLog(LogFile file)
70          {
71              return new DownloadedLog(logFile, config.Name);
72          }
73  
74          public bool HasCrashed()
75          {
76              return process.HasExited;
77          }
78  
79          public void Stop(bool waitTillStopped)
80          {
81              running = false;
82              process.Kill();
83  
84              if (waitTillStopped)
85              {
86                  process.WaitForExit();
87                  foreach (var t in streamTasks) t.Wait();
88              }
89  
90              DeleteDataDirFolder();
91          }
92      }
93  }