I have a windows service that polls a database and will do things depending on the records it finds.
I want to set up Azure Devops to automatically deploy an upgrade to this service.
In order to deploy the upgrade I need to stop the service.
Is there a way that I can tell whether this would interrupt it processing?
In my release pipeline, with a command task, I use
sc stop MyService
[Update]
Here is my simplified code
static class Program
{
static void Main()
{
ServiceBase.Run(new ServiceBase[] { new Hoster() });
}
}
public sealed class Hoster : ServiceBase
{
private IMyEngine _engine;
private readonly EventHandler<EngineProgressEventArgs> _progressHandler;
public Hoster()
{
_progressHandler = TrapEngineProgress;
}
protected override void OnStart(string[] args)
{
try
{
_engine = MyFactory.Create();
_engine.Progress += _progressHandler;
_engine.StartupEngine();
}
catch (Exception ex)
{
_logger.Error("OnStart failed", ex);
}
}
protected override void OnStop()
{
if (_engine == null) return;
_engine.Dispose();
_engine.Progress -= _progressHandler;
_engine = null;
}
private void TrapEngineProgress(object sender, EngineProgressEventArgs e)
{
switch (e.Type)
{
case ProgressType.Changed:
Trace("Changed: " + e.Filename);
break;
case ProgressType.Created:
Trace("Created: " + e.Filename);
break;
case ProgressType.Trace:
Trace(e.Message);
break;
case ProgressType.Error:
Error(e.Error, e.Message);
break;
}
}
}
When you issue the SC STOP command, you are merely making a request for the service to stop itself. When the service receives the request, it can stop immediately or wait until it has finished important tasks — whatever it wants to do. Your service is in control.
To avoid inappropriate interruption, your service's "stop workflow" should look like this:
With that approach, your service will never be interrupted in a "bad" place.