Last active
August 29, 2015 14:17
-
-
Save drusellers/df55ed64e375c5524666 to your computer and use it in GitHub Desktop.
Topshelf Service With Recovery Options
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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