Skip to content

Commit

Permalink
Windows error code compliant Topshelf#461
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcherednik authored and phatboyg committed Sep 24, 2018
1 parent 1732d3d commit d47f7a1
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/Topshelf.Tests/BeforeStart_Specs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void Should_not_start_the_service()
});

Assert.IsFalse(started);
Assert.AreEqual(TopshelfExitCode.StartServiceFailed, exitCode);
Assert.AreEqual(TopshelfExitCode.ServiceControlRequestFailed, exitCode);
}


Expand Down
8 changes: 4 additions & 4 deletions src/Topshelf.Tests/ExceptionHandler_Specs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void Should_be_called_when_exception_thrown_in_Start_method()

Assert.IsTrue(sawExceptionInStart);
Assert.IsFalse(sawExceptionInStop);
Assert.AreEqual(TopshelfExitCode.StartServiceFailed, exitCode);
Assert.AreEqual(TopshelfExitCode.ServiceControlRequestFailed, exitCode);
}

[Test]
Expand Down Expand Up @@ -81,7 +81,7 @@ public void Should_be_called_when_exception_thrown_in_Stop_method()

Assert.IsFalse(sawExceptionInStart);
Assert.IsTrue(sawExceptionInStop);
Assert.AreEqual(TopshelfExitCode.StopServiceFailed, exitCode);
Assert.AreEqual(TopshelfExitCode.ServiceControlRequestFailed, exitCode);
}

[Test]
Expand Down Expand Up @@ -115,7 +115,7 @@ public void Should_not_prevent_default_action_when_not_set()
x.Service(settings => new ExceptionThrowingService(true, false));
});

Assert.AreEqual(TopshelfExitCode.StartServiceFailed, exitCode);
Assert.AreEqual(TopshelfExitCode.ServiceControlRequestFailed, exitCode);

exitCode = HostFactory.Run(x =>
{
Expand All @@ -124,7 +124,7 @@ public void Should_not_prevent_default_action_when_not_set()
x.Service(settings => new ExceptionThrowingService(false, true));
});

Assert.AreEqual(TopshelfExitCode.StopServiceFailed, exitCode);
Assert.AreEqual(TopshelfExitCode.ServiceControlRequestFailed, exitCode);

}

Expand Down
2 changes: 1 addition & 1 deletion src/Topshelf/Hosts/CommandHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public TopshelfExitCode Run()
catch (Exception ex)
{
_log.Error("The command could not be sent to the service.", ex);
return TopshelfExitCode.SendCommandFailed;
return TopshelfExitCode.ServiceControlRequestFailed;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Topshelf/Hosts/ConsoleRunHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ void CatchUnhandledException(object sender, UnhandledExceptionEventArgs e)

if (e.IsTerminating)
{
_exitCode = TopshelfExitCode.UnhandledServiceException;
_exitCode = TopshelfExitCode.AbnormalExit;
_exit.Set();

// it isn't likely that a TPL thread should land here, but if it does let's no block it
Expand Down Expand Up @@ -195,7 +195,7 @@ void StopService()
_settings.ExceptionCallback?.Invoke(ex);

_log.Error("The service did not shut down gracefully", ex);
_exitCode = TopshelfExitCode.StopServiceFailed;
_exitCode = TopshelfExitCode.ServiceControlRequestFailed;
}
finally
{
Expand Down
2 changes: 1 addition & 1 deletion src/Topshelf/Hosts/StartHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public TopshelfExitCode Run()
catch (Exception ex)
{
_log.Error("The service failed to start.", ex);
return TopshelfExitCode.StartServiceFailed;
return TopshelfExitCode.ServiceControlRequestFailed;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Topshelf/Hosts/StopHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public TopshelfExitCode Run()
catch (Exception ex)
{
_log.Error("The service failed to stop.", ex);
return TopshelfExitCode.StopServiceFailed;
return TopshelfExitCode.ServiceControlRequestFailed;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Topshelf/Hosts/TestHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public TopshelfExitCode Run()
var exitCode = TopshelfExitCode.AbnormalExit;
try
{
exitCode = TopshelfExitCode.StartServiceFailed;
exitCode = TopshelfExitCode.ServiceControlRequestFailed;

_log.InfoFormat("The {0} service is being started.", _settings.ServiceName);
_serviceHandle.Start(this);
_log.InfoFormat("The {0} service was started.", _settings.ServiceName);

Thread.Sleep(100);

exitCode = TopshelfExitCode.StopServiceFailed;
exitCode = TopshelfExitCode.ServiceControlRequestFailed;

_log.InfoFormat("The {0} service is being stopped.", _settings.ServiceName);
_serviceHandle.Stop(this);
Expand Down
14 changes: 7 additions & 7 deletions src/Topshelf/Runtime/Windows/WindowsServiceHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected override void OnStart(string[] args)

_log.Fatal("The service did not start successfully", ex);

ExitCode = (int) TopshelfExitCode.StartServiceFailed;
ExitCode = (int) TopshelfExitCode.ServiceControlRequestFailed;
throw;
}
}
Expand All @@ -168,13 +168,13 @@ protected override void OnStop()
_settings.ExceptionCallback?.Invoke(ex);

_log.Fatal("The service did not shut down gracefully", ex);
ExitCode = (int) TopshelfExitCode.StopServiceFailed;
ExitCode = (int) TopshelfExitCode.ServiceControlRequestFailed;
throw;
}

if (_unhandledException != null)
{
ExitCode = (int) TopshelfExitCode.UnhandledServiceException;
ExitCode = (int) TopshelfExitCode.AbnormalExit;
_log.Info("[Topshelf] Unhandled exception detected, rethrowing to cause application to restart.");
throw new InvalidOperationException("An unhandled exception was detected", _unhandledException);
}
Expand Down Expand Up @@ -235,7 +235,7 @@ protected override void OnShutdown()
_settings.ExceptionCallback?.Invoke(ex);

_log.Fatal("The service did not shut down gracefully", ex);
ExitCode = (int) TopshelfExitCode.StopServiceFailed;
ExitCode = (int) TopshelfExitCode.ServiceControlRequestFailed;
throw;
}
}
Expand All @@ -257,7 +257,7 @@ protected override void OnSessionChange(SessionChangeDescription changeDescripti
_settings.ExceptionCallback?.Invoke(ex);

_log.Fatal("The did not handle Service session change correctly", ex);
ExitCode = (int) TopshelfExitCode.StopServiceFailed;
ExitCode = (int) TopshelfExitCode.ServiceControlRequestFailed;
throw;
}
}
Expand All @@ -281,7 +281,7 @@ protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
_settings.ExceptionCallback?.Invoke(ex);

_log.Fatal("The service did handle the Power event correctly", ex);
ExitCode = (int) TopshelfExitCode.StopServiceFailed;
ExitCode = (int) TopshelfExitCode.ServiceControlRequestFailed;
throw;
}
}
Expand Down Expand Up @@ -329,7 +329,7 @@ void CatchUnhandledException(object sender, UnhandledExceptionEventArgs e)
// return;
// This needs to be a configuration option to avoid breaking compatibility

ExitCode = (int) TopshelfExitCode.UnhandledServiceException;
ExitCode = (int) TopshelfExitCode.AbnormalExit;
_unhandledException = (Exception) e.ExceptionObject;

Stop();
Expand Down
2 changes: 1 addition & 1 deletion src/Topshelf/Topshelf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<ProjectReference Include="..\TopShelf.ServiceInstaller\TopShelf.ServiceInstaller.csproj" />
<PackageReference Include="Microsoft.Windows.Compatibility" Version="2.0.1"/>
<PackageReference Include="Microsoft.Windows.Compatibility" Version="2.0.1" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="HelpText.txt" />
Expand Down
44 changes: 31 additions & 13 deletions src/Topshelf/TopshelfExitCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,37 @@
// specific language governing permissions and limitations under the License.
namespace Topshelf
{
public enum TopshelfExitCode
public struct TopshelfExitCode
{
Ok = 0,
AbnormalExit = 1,
SudoRequired = 2,
ServiceAlreadyInstalled = 3,
ServiceNotInstalled = 4,
StartServiceFailed = 5,
StopServiceFailed = 6,
ServiceAlreadyRunning = 7,
UnhandledServiceException = 8,
ServiceNotRunning = 9,
SendCommandFailed = 10,
NotRunningOnWindows = 11,
private readonly int _exitCode;

public TopshelfExitCode(int exitCode)
{
_exitCode = exitCode;
}

public static explicit operator int(TopshelfExitCode topshelfExitCode)
{
return topshelfExitCode._exitCode;
}

// windows service exit code compliant
// https://docs.microsoft.com/en-us/windows/desktop/Debug/system-error-codes
public static TopshelfExitCode Ok { get; } = new TopshelfExitCode(0);
public static TopshelfExitCode ServiceAlreadyInstalled { get; } = new TopshelfExitCode(1242);
public static TopshelfExitCode ServiceNotInstalled { get; } = new TopshelfExitCode(1243);
public static TopshelfExitCode ServiceAlreadyRunning { get; } = new TopshelfExitCode(1056);
public static TopshelfExitCode ServiceNotRunning { get; } = new TopshelfExitCode(1062);
public static TopshelfExitCode ServiceControlRequestFailed { get; } = new TopshelfExitCode(1064);
public static TopshelfExitCode AbnormalExit { get; } = new TopshelfExitCode(1067);

// non-compliant
public static TopshelfExitCode SudoRequired { get; } = new TopshelfExitCode(2);
public static TopshelfExitCode NotRunningOnWindows { get; } = new TopshelfExitCode(11);

public override string ToString()
{
return $"Exit code: {_exitCode}";
}
}
}

0 comments on commit d47f7a1

Please sign in to comment.