-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExecutableHandler.cs
175 lines (152 loc) · 5.71 KB
/
ExecutableHandler.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using System.Text.RegularExpressions;
namespace iisbridge
{
public class ExecutableHandler
{
private string processFilename;
private string args;
private int port;
private Dictionary<string,string> envVariables;
private Task processObserverTask;
private Process process;
private CancellationTokenSource tokenSource;
private DateTime? startedTime;
private bool startFailed = false;
public DateTime? StartedTime { get => startedTime; }
public bool Started { get => StartedTime != null; }
public bool StartFailed { get => startFailed; }
public ExecutableHandler(string processFilename, string args, int port, Dictionary<string,string> envVariables)
{
this.processFilename = processFilename;
this.args = args;
this.port = port;
this.envVariables = envVariables;
}
private void StartProcess() {
// TODO: Find a better way to prevent the web app from running twice
// Kill applications aleready running on this port
this.KillByPort(port);
// Create Process StartInfo
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = processFilename;
startInfo.Arguments = args;
foreach (KeyValuePair<string, string> variable in envVariables)
{
startInfo.EnvironmentVariables.Add(variable.Key,variable.Value);
}
startInfo.WorkingDirectory = Environment.CurrentDirectory;
// Start web app process
using (process = Process.Start(startInfo))
{
startFailed = false;
Console.WriteLine($"Starting webapp process (process: {processFilename}, args: {args})");
do
{
if (!process.HasExited)
{
// Refresh the current process property values.
process.Refresh();
if (!process.Responding)
{
Console.WriteLine("Application not responding. It is getting killed and restarted.");
// Kill an restart process
process.Kill();
StartProcess();
}
}
}
while (!process.WaitForExit(1000) && !tokenSource.Token.IsCancellationRequested);
startedTime = null;
// If no cancellation was requeted -> throw error
if (!tokenSource.Token.IsCancellationRequested)
{
startFailed = true;
tokenSource.Cancel();
throw new Exception($"Web app exited with code {process.ExitCode} (process: {processFilename}, args: {args})");
}
}
}
public void Start()
{
startedTime = DateTime.Now;
tokenSource = new CancellationTokenSource();
this.processObserverTask = Task.Run(() => StartProcess(), tokenSource.Token);
}
public void Stop()
{
try
{
tokenSource.Cancel();
process.Kill();
} catch (Exception ex)
{
Console.WriteLine($"Error on stopping process: {ex.Message}");
}
}
private void KillByPort(int port)
{
var processes = GetAllProcesses();
if (processes.Any(p => p.Port == port))
{
try
{
Process.GetProcessById(processes.First(p => p.Port == port).PID).Kill();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
} else
{
Console.WriteLine("No process to kill!");
}
}
public List<PRC> GetAllProcesses()
{
var pStartInfo = new ProcessStartInfo();
pStartInfo.FileName = "netstat.exe";
pStartInfo.Arguments = "-a -n -o";
pStartInfo.WindowStyle = ProcessWindowStyle.Maximized;
pStartInfo.UseShellExecute = false;
pStartInfo.RedirectStandardInput = true;
pStartInfo.RedirectStandardOutput = true;
pStartInfo.RedirectStandardError = true;
var process = new Process()
{
StartInfo = pStartInfo
};
process.Start();
var soStream = process.StandardOutput;
var output = soStream.ReadToEnd();
var result = new List<PRC>();
var lines = Regex.Split(output, "\r\n");
foreach (var line in lines)
{
if (line.Trim().StartsWith("Proto"))
continue;
var parts = line.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
var len = parts.Length;
if (len > 2)
result.Add(new PRC
{
Protocol = parts[0],
Port = int.Parse(parts[1].Split(':').Last()),
PID = int.Parse(parts[len - 1])
});
}
return result;
}
}
public class PRC
{
public int PID { get; set; }
public int Port { get; set; }
public string Protocol { get; set; }
}
}