-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProcessHostService.cs
155 lines (125 loc) · 4.35 KB
/
ProcessHostService.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
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Threading.Tasks;
namespace ServiceManager2
{
partial class ProcessHostService : ServiceBase
{
private readonly ServiceConfiguration config;
public ProcessHostService(ServiceConfiguration config)
{
InitializeComponent();
this.config = config;
}
private Process process;
protected override void OnStart(string[] args)
{
if (string.IsNullOrWhiteSpace(config.Executable))
{
throw new ArgumentException("Executable is not defined");
}
var commandLine = config.Executable.Split(new[] { ' ' }, 2);
var exe = commandLine[0];
var arguments = "";
if (commandLine.Length > 1)
{
arguments = commandLine[1];
}
process = ServiceManager.RunProcess(exe, arguments);
if (process != null)
{
StartLogging();
}
var exited = process?.WaitForExit(10 * 1000) ?? true;
if (exited)
{
base.Stop();
}
}
private void StartLogging()
{
if (!string.IsNullOrWhiteSpace(config.LogPath))
{
if ("console".Equals(config.LogPath, StringComparison.InvariantCultureIgnoreCase))
{
StartReaderThread(new Tuple<StreamReader, TextWriter>(process.StandardOutput, Console.Out));
}
else
{
RedirectOutputToFile(process, config.LogPath);
}
}
}
protected override void OnStop()
{
}
public void Start()
{
OnStart(new string[0]);
process?.WaitForExit();
}
private static void StartReaderThread(Tuple<StreamReader, TextWriter> args)
{
new Task(
obj => ReadPipe((Tuple<StreamReader, TextWriter>)obj),
args
).Start();
}
private void RedirectOutputToFile(Process process, string logPath)
{
var fileStream = new FileStream(logPath, FileMode.OpenOrCreate, FileAccess.Write);
var writer = new StreamWriter(fileStream) { AutoFlush = true };
StartReaderThread(new Tuple<StreamReader, TextWriter>(process.StandardOutput, writer));
}
private static void ReadPipe(Tuple<StreamReader, TextWriter> tuple)
{
while (true)
{
if (tuple.Item1.EndOfStream)
{
return;
}
var str = tuple.Item1.Read();
tuple.Item2.Write((char)str);
}
}
}
internal static class ServiceManager
{
public static void Install(ServiceConfiguration config)
{
RunProcess("sc", $"create {config.ServiceName} displayname= \"{config.DisplayName}\" binpath= \"{config.GenerateServiceBinPath()}\" start= auto").WaitForExit();
var service = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == config.ServiceName);
if (service != null && service.Status != ServiceControllerStatus.Running)
{
service.Start();
}
}
public static void Uninstall(ServiceConfiguration config)
{
var service = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == config.ServiceName);
if (service != null &&
service.CanStop &&
service.Status != ServiceControllerStatus.Stopped &&
service.Status != ServiceControllerStatus.StopPending)
{
service.Stop();
}
RunProcess("sc", $"delete {config.ServiceName}").WaitForExit();
}
internal static Process RunProcess(string exe, string command)
{
return Process.Start(new ProcessStartInfo
{
FileName = exe,
Arguments = command,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true
});
}
}
}