Backport diagnosability updates (#736)

pull/737/head
Next Turn 2020-12-09 01:14:27 +08:00 committed by GitHub
parent 1bd0c56a09
commit d1ae60975e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 43 deletions

View File

@ -187,7 +187,14 @@ namespace WinSW.Util
if (priority != null) if (priority != null)
{ {
processToStart.PriorityClass = priority.Value; try
{
processToStart.PriorityClass = priority.Value;
}
catch (InvalidOperationException)
{
// exited
}
} }
// Redirect logs if required // Redirect logs if required

View File

@ -21,7 +21,6 @@ using log4net.Layout;
using WinSW.Configuration; using WinSW.Configuration;
using WinSW.Logging; using WinSW.Logging;
using WinSW.Native; using WinSW.Native;
using WinSW.Util;
using WMI; using WMI;
using ServiceType = WMI.ServiceType; using ServiceType = WMI.ServiceType;
@ -74,7 +73,16 @@ namespace WinSW
if (!inConsoleMode) if (!inConsoleMode)
{ {
Log.Debug("Starting WinSW in service mode"); Log.Debug("Starting WinSW in service mode");
ServiceBase.Run(new WrapperService(descriptor)); using var service = new WrapperService(descriptor);
try
{
ServiceBase.Run(service);
}
catch
{
// handled in OnStart
}
return; return;
} }

View File

@ -43,7 +43,7 @@ namespace WinSW
/// so don't try to kill us when the child exits. /// so don't try to kill us when the child exits.
/// </summary> /// </summary>
private bool orderlyShutdown; private bool orderlyShutdown;
private bool systemShuttingdown; private bool shuttingdown;
/// <summary> /// <summary>
/// Version of Windows service wrapper /// Version of Windows service wrapper
@ -56,7 +56,7 @@ namespace WinSW
/// <summary> /// <summary>
/// Indicates that the system is shutting down. /// Indicates that the system is shutting down.
/// </summary> /// </summary>
public bool IsShuttingDown => this.systemShuttingdown; public bool IsShuttingDown => this.shuttingdown;
public WrapperService(IWinSWConfiguration descriptor) public WrapperService(IWinSWConfiguration descriptor)
{ {
@ -67,7 +67,7 @@ namespace WinSW
this.CanStop = true; this.CanStop = true;
this.CanPauseAndContinue = false; this.CanPauseAndContinue = false;
this.AutoLog = true; this.AutoLog = true;
this.systemShuttingdown = false; this.shuttingdown = false;
// Register the event log provider // Register the event log provider
eventLogProvider.Service = this; eventLogProvider.Service = this;
@ -144,7 +144,7 @@ namespace WinSW
public void LogEvent(string message) public void LogEvent(string message)
{ {
if (this.systemShuttingdown) if (this.shuttingdown)
{ {
/* NOP - cannot call EventLog because of shutdown. */ /* NOP - cannot call EventLog because of shutdown. */
} }
@ -163,7 +163,7 @@ namespace WinSW
public void LogEvent(string message, EventLogEntryType type) public void LogEvent(string message, EventLogEntryType type)
{ {
if (this.systemShuttingdown) if (this.shuttingdown)
{ {
/* NOP - cannot call EventLog because of shutdown. */ /* NOP - cannot call EventLog because of shutdown. */
} }
@ -179,8 +179,51 @@ namespace WinSW
} }
} }
} }
internal void RaiseOnStart(string[] args) => this.OnStart(args);
internal void RaiseOnStop() => this.OnStop();
protected override void OnStart(string[] args) protected override void OnStart(string[] args)
{
try
{
this.DoStart();
}
catch (Exception e)
{
Log.Error("Failed to start service.", e);
throw;
}
}
protected override void OnStop()
{
try
{
this.DoStop();
}
catch (Exception e)
{
Log.Error("Failed to stop service.", e);
throw;
}
}
protected override void OnShutdown()
{
try
{
this.shuttingdown = true;
this.DoStop();
}
catch (Exception e)
{
Log.Error("Failed to shut down service.", e);
throw;
}
}
private void DoStart()
{ {
bool succeeded = ConsoleApis.SetConsoleCtrlHandler(null, true); bool succeeded = ConsoleApis.SetConsoleCtrlHandler(null, true);
Debug.Assert(succeeded); Debug.Assert(succeeded);
@ -293,43 +336,10 @@ namespace WinSW
this.process.StandardInput.Close(); // nothing for you to read! this.process.StandardInput.Close(); // nothing for you to read!
} }
protected override void OnShutdown()
{
// WriteEvent("OnShutdown");
try
{
this.systemShuttingdown = true;
this.StopIt();
}
catch (Exception ex)
{
Log.Error("Shutdown exception", ex);
}
}
protected override void OnStop()
{
// WriteEvent("OnStop");
try
{
this.StopIt();
}
catch (Exception ex)
{
Log.Error("Cannot stop exception", ex);
}
}
internal void RaiseOnStart(string[] args) => this.OnStart(args);
internal void RaiseOnStop() => this.OnStop();
/// <summary> /// <summary>
/// Called when we are told by Windows SCM to exit. /// Called when we are told by Windows SCM to exit.
/// </summary> /// </summary>
private void StopIt() private void DoStop()
{ {
string? stopArguments = this.descriptor.StopArguments; string? stopArguments = this.descriptor.StopArguments;
this.LogEvent("Stopping " + this.descriptor.Id); this.LogEvent("Stopping " + this.descriptor.Id);
@ -371,7 +381,7 @@ namespace WinSW
// Stop extensions // Stop extensions
this.ExtensionManager.FireBeforeWrapperStopped(); this.ExtensionManager.FireBeforeWrapperStopped();
if (this.systemShuttingdown && this.descriptor.BeepOnShutdown) if (this.shuttingdown && this.descriptor.BeepOnShutdown)
{ {
Console.Beep(); Console.Beep();
} }