Skip to content

Instantly share code, notes, and snippets.

@drusellers
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drusellers/df55ed64e375c5524666 to your computer and use it in GitHub Desktop.
Save drusellers/df55ed64e375c5524666 to your computer and use it in GitHub Desktop.
Topshelf Service With Recovery Options
static int Main()
{
return (int)HostFactory.Run(x =>
{
bool throwOnStart = false;
bool throwOnStop = false;
bool throwUnhandled = false;
x.Service(settings => new SampleService(throwOnStart, throwOnStop, throwUnhandled), s =>
{
s.BeforeStartingService(_ => Console.WriteLine("BeforeStart"));
s.BeforeStoppingService(_ => Console.WriteLine("BeforeStop"));
});
x.SetStartTimeout(TimeSpan.FromSeconds(10));
x.SetStopTimeout(TimeSpan.FromSeconds(10));
x.EnableServiceRecovery(r =>
{
//you can have up to three of these
r.RestartComputer(5, "message");
r.RestartService(0);
//the last one will act for all subsequent failures
r.RunProgram(7, "some command");
r.OnCrashOnly();
r.SetResetPeriod(1);
});
x.AddCommandLineSwitch("throwonstart", v => throwOnStart = v);
x.AddCommandLineSwitch("throwonstop", v => throwOnStop = v);
x.AddCommandLineSwitch("throwunhandled", v => throwUnhandled = v);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment