Category Archives: Development

periodic job execution via a ‘heartbeat’ routine

A heartbeat is something that occurs at intervals and performs some work. In the body, it will pump blood etc. In some information systems, heartbeat could refer to a failure detection method, in the absence of a heartbeat a system is considered to be offline causing a failover situation.  However, that is not the meaning here. Instead, I mean heartbeat to refer to regular occurring timespans (in seconds) at the end of which, the system will perform some work or status checking.

Many applications have to do work over time and at regular intervals. I’m thinking of systems that need to check for work to do, (checking if there is a need for processing data, preparing files, sending email or other communications for example). These are periodic jobs that have to be done but not all the time. Frequently the system needs to just check every once in a while if there is work to do and then to do it. It is a little different than task scheduler which performs work at a certain time or job scheduling. Rather this heartbeat function performs regular essential work at each heart beat or pulse. During the time between heartbeats, the system is asleep.

In the approach I used, I created a class file and a console application under the same solution in Visual studio 2013 using the C# language. To start I build the class first shown below.  The main things I wanted to do is to allow the setting of number of seconds between heartbeats, the pulse rate.  For my situation I wanted a pulse between 8 beats per minute up to 1 beat per minute. 1 beat means I am performing work 1 time every 60 seconds, so I would initialize it with a value of 60.  In my own situation I wanted to set the interval a value between 9 and 60.

Next in order to test the routine, a console program is required.

note: when building the console checker, I had to add the project ‘under’ the same solution as the class. In the console program I also had to set a reference to the class project so I could reference the object.  The other thing that was required was to set the console program to be the first project to run in the solution.

After compiling and running the project the output looks something like this below:

heartbeat2

The ‘work’ is just to write out the time to show the pulse.

It is interesting to note the routine will continue to run on and on. In this version, there is no way to set a limit on the number of heartbeats. Perhaps this is something to fix in a later version.