Written by

Senior Cloud Architect at InterSystems
MOD
Article Eduard Lebedyuk · 1 hr ago 2m read

Catch long-running messages

If you have long-running messages here's a simple code to catch them. You can also use Queue Wait Alert to get the same results, although this code will catch even a single active message with an empty queue. It also scans all namespaces with no production modification, so it can be used for debugging.

Include Ensemble

Class User.LongMessagesTask Extends %SYS.Task.Definition
{

Parameter TaskName = "Long running messages";

Property LongRunningMessageThreshold As %Integer(MINVAL = 1) [ InitialExpression = 60 ];

Method OnTask() As %Status
{
	set ts = $zdatetime($NOW(0),3,1)
	set ns = ""
	while 1 {
		// Iterate namespaces
		set ns = $o($$$EnsJobMonitorRoot(ns))
		quit:ns=""
		
		set job =""
		while 1 {
			// Iterate jobs
			set job = $o($$$EnsJobMonitorRoot(ns, job))
			quit:job=""
			
			// Get business host name
			set bh = $o($$$EnsJobMonitorRoot(ns,job,""))
			
			// Get the time of the last state change
			set lastActivity = $$$EnsJobMonitorRoot(ns,job,bh,"%LastActivity")
			
			// Get active message, if any - bh can be just idling
			set active = $d(^[ns]Ens.ActiveMessage(job),messageid) // From $$$EnsActiveMessage
			if active {
				set time = $system.SQL.Functions.DATEDIFF("s", lastActivity,  ts)
				if time > ..LongRunningMessageThreshold {
					set text = $$$FormatText("Long running message %1, in ns: %2, BH: %3, processing since %4, for %5 seconds", messageid, ns, bh, lastActivity, time)
					// For debug
					// write text, !
					do ##class(%SYS.System).WriteToConsoleLog(text)
				}
			}
		}
	}
	quit $$$OK
}

}