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.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace heartbeat { using System.Threading; public class pulse { private int _seconds=30; public bool keep_beating = true; public int seconds { get { return _seconds; } set { //allow from 9 to 60 seconds if (value>8 && value <= 60) { _seconds = value; } else { _seconds = 30; } } } public pulse(int seconds_between_pulses) { seconds = seconds_between_pulses; pulse_inverval(); } void heartbeat() { //do some work here Console.WriteLine("heartbeat, time: {0}", DateTime.Now.ToLongTimeString()); } void pulse_inverval() { DateTime when_to_beat = DateTime.Now.Add(System.TimeSpan.FromSeconds(Convert.ToDouble(seconds))); while (keep_beating) { //wait 15 seconds System.Threading.Thread.Sleep(seconds * 1000); if (when_to_beat < DateTime.Now) { //heartbeat //done waiting, now do work heartbeat(); //reset variable to wait more seconds when_to_beat = DateTime.Now.Add(System.TimeSpan.FromSeconds(Convert.ToDouble(seconds))); } } } } }
Next in order to test the routine, a console program is required.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace heartbeatTester { class Program { static void Main(string[] args) { Console.WriteLine("start heartbeatTester"); heartbeat.pulse hbp = new heartbeat.pulse(3); Console.WriteLine("done heartbeatTester"); } } }
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:
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.