Wednesday, 2 December 2015

Sitecore Heartbeat


What is Heartbeat ?

Heartbeat is vital Sitecore CMS timer, that enables subscribed code execution to be executed periodically.


Whenever it rings, a list of due subscribers is built & invoked one-by-one.

If any of subscribers takes ages to be executed, all other has nothing to do but wait.


By whom is it used ?

Usages via iLSpy
Core CMS features rely on the mechanism:

  • Indexing
  • Publishing
  • Sitecore Jobs
  • Event Queue processing

However custom code is welcomed to use it as well. You can either subscribe on static 'Sitecore.Services.Heartbeat.Beat' event, or build a new instance of
'Sitecore.Services.AlarmClock' class, which internally uses Beat Event as well.

How is it implemented?


A thread named "Heartbeat" is created during CMS start. You might have noticed that some Sitecore log entries start with 'Heartbeat' - those were raised by background timer.


It would do following in forever loop:

  1. Sleep a few seconds ( specified by 'HeartbeatInterval' setting )
  2. Get all subscribers for 'Heartbeat.Beat' event
  3. Invoke them one-by-one

Possible pitfalls

If execution of a subscriber takes a while, all other subscribers must wait.

As a result CMS vital mechanisms ( Event Queues, Indexing ) would need to wait for invocation as well =\

You can observe the situation by filtering Sitecore Logs via 'Heartbeat' text filter -> gaps might appear in time panel.

Thus if you tend to initiate a new 'AlarmClock' that would execute heavy logic, please don`t do it.

Consider creating a Sitecore Job instead.

1 comment: