Mo D
Mo D

Reputation: 559

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.

Upvotes: 5

Views: 8757

Answers (3)

Mo D
Mo D

Reputation: 559

The solution that worked for this project was to use a heartbeat like implementation to allow the service to notify me of it's availability.

Because our application uses WebAPI I was able to set up an endpoint which the service "pings" every [x] seconds.

A separate process has been added which checks the date and time of the last notification from the service and if that doesn't fall within a set threshold I notify the user that the service is unavailable.

I looked at using the ServiceController but that was not going to be an ideal solution because of the possibility that the functionality added to the service could be run as a Windows console application instead of a Windows Service.

Upvotes: -1

Bharath
Bharath

Reputation: 195

you can use ServiceController class in .net to monitor services.

I faced the same problem in one of my projects.

I used the below approach to monitor the my service.

  • First thing, i logged all the informations,errors from my service to event viewer in a standard format like this

custom-eventid|datetime|message

  • Then i created one more notification service which will listen to the event viewer for the particular events,reads the message from the event entry
  • if the event entry falls in the event viewer it will send mail notification through smtp
  • if you are not provided with the smpt then go for windows application which listen to the events and shows message using baloon or message box

Upvotes: 0

KingCronus
KingCronus

Reputation: 4519

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?

Upvotes: 5

Related Questions