Skip to content

Commit 0d55649

Browse files
committed
Fixed all remaining issues with the service
Added EventViewer logging of Exceptions from the Service Removed the need for a batch file Fixed logging not working Fixed graceful shutdown Fixed folder path needing trim on installer
1 parent 9ca3974 commit 0d55649

7 files changed

Lines changed: 93 additions & 79 deletions

File tree

PhantomBotService.sln

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PhantomBotService", "PhantomBotService\PhantomBotService.csproj", "{AAE23004-3FF9-401B-987C-E925B153BF32}"
77
EndProject
88
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{60E30BA8-C0AB-438B-A495-08F257B69015}"
9-
ProjectSection(SolutionItems) = preProject
10-
launch-service.bat = launch-service.bat
11-
EndProjectSection
129
EndProject
1310
Project("{54435603-DBB4-11D2-8724-00A0C9A8B90C}") = "PhantomBotServiceSetup", "PhantomBotServiceSetup\PhantomBotServiceSetup.vdproj", "{C6AAB224-8AF7-4664-B02B-C44BEB893FAB}"
1411
EndProject

PhantomBotService/GlobalSuppressions.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
// a specific target and scoped to a namespace, type, member, etc.
66

77
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "SG0018:Path traversal", Justification = "Safe usage for log file names", Scope = "member", Target = "~M:PhantomBotService.PhantomBotService.OnStart(System.String[])")]
8-
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "RCS1075:Avoid empty catch clause that catches System.Exception.", Justification = "Catches log file Exceptions", Scope = "member", Target = "~M:PhantomBotService.PhantomBotService.OnStart(System.String[])")]
9-
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "RCS1075:Avoid empty catch clause that catches System.Exception.", Justification = "Catches log file Exceptions", Scope = "member", Target = "~M:PhantomBotService.PhantomBotService.OnStop")]
108
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "SG0018:Path traversal", Justification = "Path provided by installer", Scope = "member", Target = "~M:PhantomBotService.PhantomBotServiceInstaller.CreateConfig")]
119
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "SG0018:Path traversal", Justification = "Path provided by installer", Scope = "member", Target = "~M:PhantomBotService.PhantomBotServiceInstaller.DeleteConfig")]
1210

PhantomBotService/PhantomBotService.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PhantomBotService/PhantomBotService.cs

Lines changed: 86 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.ComponentModel;
23
using System.Diagnostics;
34
using System.IO;
45
using System.ServiceProcess;
@@ -19,6 +20,17 @@ public partial class PhantomBotService : ServiceBase
1920
public PhantomBotService()
2021
{
2122
this.InitializeComponent();
23+
24+
((ISupportInitialize)(this.EventLog)).BeginInit();
25+
if (!EventLog.SourceExists(this.EventLog.Source))
26+
{
27+
EventLog.CreateEventSource(this.EventLog.Source, this.EventLog.Log);
28+
}
29+
((ISupportInitialize)(this.EventLog)).EndInit();
30+
31+
this.EventLog.Source = this.ServiceName;
32+
this.EventLog.Log = "Application";
33+
2234
string cmdLine = Environment.CommandLine.Remove(Environment.CommandLine.Length - 2, 2).Remove(0, 1);
2335
string appFolder = Path.GetDirectoryName(cmdLine);
2436
string path = appFolder + "\\PhantomBotService.config";
@@ -28,8 +40,9 @@ public PhantomBotService()
2840
{
2941
this.f = File.Open(path, FileMode.Open, FileAccess.Read);
3042
}
31-
catch (IOException)
43+
catch (IOException e)
3244
{
45+
this.EventLog.WriteEntry("Failed to open config file: " + path + Environment.NewLine + e.GetType().FullName + ": " + e.Message + Environment.NewLine + e.StackTrace, EventLogEntryType.Error);
3346
}
3447

3548
byte[] b = new byte[1024];
@@ -55,20 +68,23 @@ public PhantomBotService()
5568
string[] sdata = configData.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
5669

5770
string workingDir = sdata[1];
58-
string exec = sdata[4];
59-
this.log = sdata[7].Equals("true", StringComparison.OrdinalIgnoreCase);
71+
this.log = sdata[4].Equals("true", StringComparison.OrdinalIgnoreCase);
6072

6173
this.phantomBotProcess = new Process();
6274
this.phantomBotProcess.StartInfo.WorkingDirectory = workingDir;
63-
this.phantomBotProcess.StartInfo.FileName = exec;
75+
this.phantomBotProcess.StartInfo.FileName = "java-runtime\\bin\\java.exe";
76+
this.phantomBotProcess.StartInfo.Arguments = "--add-opens java.base/java.lang=ALL-UNNAMED -Djava.security.policy=config/security -Dinteractive -Xms1m -Dfile.encoding=UTF-8 -jar \"PhantomBot.jar\"";
6477
this.phantomBotProcess.StartInfo.CreateNoWindow = true;
65-
this.phantomBotProcess.StartInfo.UseShellExecute = true;
78+
this.phantomBotProcess.StartInfo.UseShellExecute = false;
6679
this.phantomBotProcess.EnableRaisingEvents = true;
6780

6881
this.phantomBotProcess.Exited += this.PhantomBotProcess_Exited;
6982

7083
if (this.log)
7184
{
85+
this.phantomBotProcess.StartInfo.RedirectStandardOutput = true;
86+
this.phantomBotProcess.StartInfo.RedirectStandardError = true;
87+
this.phantomBotProcess.StartInfo.RedirectStandardInput = true;
7288
this.phantomBotProcess.OutputDataReceived += this.PhantomBotProcess_OutputDataReceived;
7389
this.phantomBotProcess.ErrorDataReceived += this.PhantomBotProcess_ErrorDataReceived;
7490
}
@@ -115,6 +131,8 @@ private void PhantomBotProcess_Exited(object sender, EventArgs e)
115131

116132
protected override void OnStart(string[] args)
117133
{
134+
string path = this.phantomBotProcess.StartInfo.WorkingDirectory + "\\PhantomBotService." + DateTime.Now.ToFileTime() + ".log";
135+
118136
try
119137
{
120138
if (this.log)
@@ -123,47 +141,94 @@ protected override void OnStart(string[] args)
123141
{
124142
this.f.Close();
125143
}
126-
127-
string path = this.phantomBotProcess.StartInfo.WorkingDirectory + "\\PhantomBotService." + DateTime.Now.ToFileTime() + ".log";
128-
this.f = File.Open(path, FileMode.OpenOrCreate, FileAccess.Write);
144+
this.f = File.Open(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
129145
this.logLine(DateTime.Now.ToShortDateString() + " @ " + DateTime.Now.ToShortTimeString() + " >> Starting PhantomBot" + Environment.NewLine);
130146
}
131147

148+
}
149+
catch (Exception e)
150+
{
151+
this.EventLog.WriteEntry("Failed to open log file: " + path + Environment.NewLine + e.GetType().FullName + ": " + e.Message + Environment.NewLine + e.StackTrace, EventLogEntryType.Error);
152+
}
153+
154+
try
155+
{
132156
this.phantomBotProcess.Start();
157+
this.phantomBotProcess.BeginErrorReadLine();
158+
this.phantomBotProcess.BeginOutputReadLine();
133159
}
134-
catch (Exception)
160+
catch (Exception e)
135161
{
162+
this.EventLog.WriteEntry("Failed to start process: " + this.phantomBotProcess.StartInfo.WorkingDirectory + "\\" + this.phantomBotProcess.StartInfo.FileName + Environment.NewLine + e.GetType().FullName + ": " + e.Message + Environment.NewLine + e.StackTrace, EventLogEntryType.Error);
136163
}
164+
137165
}
138166

139167
protected override void OnStop()
140168
{
141169
try
142170
{
143-
if (this.log)
171+
this.allowExit = true;
172+
173+
using (StreamWriter writer = this.phantomBotProcess.StandardInput)
144174
{
145-
this.logLine(DateTime.Now.ToShortDateString() + " @ " + DateTime.Now.ToShortTimeString() + " >> Stopping PhantomBot" + Environment.NewLine);
146-
this.f.Close();
175+
writer.WriteLine("exit");
147176
}
148177

149-
this.allowExit = true;
150-
this.phantomBotProcess.CloseMainWindow();
151-
Thread.Sleep(15000);
152-
153-
if (this.phantomBotProcess.HasExited)
178+
for (int i = 0; i < 30; i++)
154179
{
155-
this.phantomBotProcess.Close();
180+
if (this.phantomBotProcess.HasExited)
181+
{
182+
this.phantomBotProcess.Close();
183+
break;
184+
}
185+
186+
Thread.Sleep(500);
156187
}
157-
else
188+
}
189+
catch (Exception e)
190+
{
191+
this.EventLog.WriteEntry("Failed to end process" + Environment.NewLine + e.GetType().FullName + ": " + e.Message + Environment.NewLine + e.StackTrace, EventLogEntryType.Error);
192+
}
193+
194+
try
195+
{
196+
if (!this.phantomBotProcess.HasExited)
158197
{
159198
this.phantomBotProcess.Kill();
160199
}
161200
}
162-
catch (Exception)
201+
catch (Exception e)
163202
{
203+
this.EventLog.WriteEntry("Failed to kill process" + Environment.NewLine + e.GetType().FullName + ": " + e.Message + Environment.NewLine + e.StackTrace, EventLogEntryType.Error);
204+
}
205+
206+
try
207+
{
208+
if (this.log)
209+
{
210+
this.logLine(DateTime.Now.ToShortDateString() + " @ " + DateTime.Now.ToShortTimeString() + " >> Stopping PhantomBot" + Environment.NewLine);
211+
this.f.Close();
212+
}
213+
}
214+
catch (Exception e)
215+
{
216+
this.EventLog.WriteEntry("Failed to close log file" + Environment.NewLine + e.GetType().FullName + ": " + e.Message + Environment.NewLine + e.StackTrace, EventLogEntryType.Error);
164217
}
165218
}
166219

167-
private void logLine(string logline) => this.f.Write(Encoding.UTF8.GetBytes(logline), 0, logline.Length);
220+
private void logLine(string logline)
221+
{
222+
try
223+
{
224+
this.f.Write(Encoding.UTF8.GetBytes(logline), 0, logline.Length);
225+
this.f.Flush();
226+
}
227+
catch (Exception e)
228+
{
229+
this.EventLog.WriteEntry("Failed to write to log file: " + logline + Environment.NewLine + Environment.NewLine + e.GetType().FullName + ": " + e.Message + Environment.NewLine + e.StackTrace, EventLogEntryType.Error);
230+
}
231+
232+
}
168233
}
169234
}

PhantomBotService/PhantomBotServiceInstaller.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ private void DeleteConfig()
6161
return;
6262
}
6363

64+
this.appFolder = this.appFolder.Trim();
65+
6466
File.Delete(this.appFolder + "\\PhantomBotService.config");
6567
}
6668

@@ -89,11 +91,12 @@ private void CreateConfig()
8991
return;
9092
}
9193

94+
this.appFolder = this.appFolder.Trim();
95+
9296
FileStream f = File.Open(this.appFolder + "\\PhantomBotService.config", FileMode.OpenOrCreate, FileAccess.Write);
9397

9498
string data = "[Bot Install Directory]" + Environment.NewLine +
9599
this.appFolder.Replace("\\\\", "") + Environment.NewLine + Environment.NewLine +
96-
"[Bot launch Script]" + Environment.NewLine + "launch-service.bat" + Environment.NewLine + Environment.NewLine +
97100
"[Logging Enabled]" + Environment.NewLine + "false";
98101

99102
f.Write(Encoding.UTF8.GetBytes(data), 0, data.Length);

PhantomBotServiceSetup/PhantomBotServiceSetup.vdproj

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@
2727
}
2828
"Entry"
2929
{
30-
"MsmKey" = "8:_D4A2AF5A73B24E8B80B76E2E8F51CE08"
31-
"OwnerKey" = "8:_UNDEFINED"
32-
"MsmSig" = "8:_UNDEFINED"
33-
}
34-
"Entry"
35-
{
3630
"MsmKey" = "8:_UNDEFINED"
3731
"OwnerKey" = "8:_0D8D97A34CE6C0307BFC9FBD54C248AA"
3832
"MsmSig" = "8:_UNDEFINED"
@@ -213,26 +207,6 @@
213207
"IsDependency" = "11:TRUE"
214208
"IsolateTo" = "8:"
215209
}
216-
"{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_D4A2AF5A73B24E8B80B76E2E8F51CE08"
217-
{
218-
"SourcePath" = "8:..\\launch-service.bat"
219-
"TargetName" = "8:launch-service.bat"
220-
"Tag" = "8:"
221-
"Folder" = "8:_D521E163504E497AA7F0C100D211890F"
222-
"Condition" = "8:"
223-
"Transitive" = "11:FALSE"
224-
"Vital" = "11:TRUE"
225-
"ReadOnly" = "11:FALSE"
226-
"Hidden" = "11:FALSE"
227-
"System" = "11:FALSE"
228-
"Permanent" = "11:FALSE"
229-
"SharedLegacy" = "11:FALSE"
230-
"PackageAs" = "3:1"
231-
"Register" = "3:1"
232-
"Exclude" = "11:FALSE"
233-
"IsDependency" = "11:FALSE"
234-
"IsolateTo" = "8:"
235-
}
236210
}
237211
"FileType"
238212
{
@@ -288,9 +262,9 @@
288262
"Product"
289263
{
290264
"Name" = "8:Microsoft Visual Studio"
291-
"ProductName" = "8:PhantomBotServiceSetup"
265+
"ProductName" = "8:PhantomBotService"
292266
"ProductCode" = "8:{F498F94E-A2B9-4930-9FB9-E6A281915231}"
293-
"PackageCode" = "8:{C3DB0E01-F6C5-4BA8-A706-9F682342EFCC}"
267+
"PackageCode" = "8:{D6E9F90B-DABA-480C-B71A-62A10517F73B}"
294268
"UpgradeCode" = "8:{D81B927D-224E-4578-A6D8-D0A4C02D3130}"
295269
"AspNetVersion" = "8:4.0.30319.0"
296270
"RestartWWWService" = "11:FALSE"

launch-service.bat

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)