Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to check if a service has hung

I have a question about how best to check if a service is still running.

First a bit of clarification. The service I have is a C# application which can either be run from the command line or can be run as a Windows Service. The function of the service is to check for changes to a remote 3rd party data source and process those changes before adding them to our own local data store.

I want to be able to identify when the service has stopped functioning for whatever reason, and notify somebody when this happens as automatically as possible. This needs to happen regardless of whether the service is being run as a Windows Service or from the command line.

I have already considered monitoring the local data store for changes and notifying when changes haven't happened for a set amount of time, however this has proven to be a little too inconsistent, because the frequency of changes to the 3rd party data source is variable, which means that a prolonged lack of changes doesn't necessarily indicate that the service has stopped working, it could just be that there are no changes!

Are there any suggestions about how I might go about monitoring this? Anyone got any experience working with something similar?

Thanks, M

Edit 1 Just to give a rough idea of how the service works: The 3rd party service raises events when new/updated data is available so my service sits and waits for these events to be raised and processes the data returned in the raised event. Therefore this is why it's tricky to identify when there's "no changes" rather than "service crashed".

Edit 2 I think I need to be a little clearer: The main reason for this monitoring is to notify a user about a potential issue either with the service or with the connection to the 3rd party service. The service itself is single threaded and has proper exception handling and logging. Chances are this service is going to be run on a server somewhere so if there are any problems with the service and it stops updating our local data store for whatever reason the service needs to notify someone.

like image 810
Mo D Avatar asked Oct 03 '22 23:10

Mo D


1 Answers

you might want to consider something like a 'heartbeat':

Heartbeat activity for Windows Service

But your main consideration should be working out why your service should be able to stop/hang? All exceptions need to be caught, and at the very worst case, reset your service to its start state after a short wait to prevent CPU maxing.

Windows itself has a variety of methods to help also:

Start > Run > Services.msc > Right Click Service > Properties > Recovery Options

If you design your application to properly use exceptions and handle them appropriately, you shouldn't ever have a problem with your service 'hanging for some reason'.

Additional:

Is there no way for you do determine the difference between "no work required" and a hang?

like image 56
KingCronus Avatar answered Oct 12 '22 01:10

KingCronus