web stats
Mirth Community - View Single Post - Mirth Tools: User defined functions
View Single Post
  #7  
Old 04-17-2012, 02:26 PM
glenn71 glenn71 is offline
OBX.3 Kenobi
 
Join Date: Nov 2010
Location: Sydney Australia
Posts: 129
glenn71 is on a distinguished road
Default

Function to Stop a channel if it reaches a particular error count. Also sends an alert message via another channel (this sends a sms via our monitoring system).

Code:
function stopOnErrorCount(cid, errorCount) {
	var channelStatisticsController = Packages.com.mirth.connect.server.controllers.ChannelStatisticsController.getInstance();
	var channelStatusController = Packages.com.mirth.connect.server.controllers.ChannelStatusController.getInstance();
	var stats = channelStatisticsController.getStatistics(cid);
	// var numOfQueued = stats.queued;
	var numOfErrors = stats.error;

	if(numOfErrors > errorCount )
	{
		//Stop Me
		channelStatusController.stopChannel(cid);
		//Alert msg sent to _SVHALERT channel
		var channelController = Packages.com.mirth.connect.server.controllers.ChannelController.getInstance();
		var channelName = channelController.getDeployedChannelById(cid).getName();
		var alertmsg = 'mirth01 mirth_channel_status codeTemplate_stopOnErrorCount 0 ' +
					   'channel ' + channelName + ' stopped';
		router.routeMessage('_SVHALERT', alertmsg);
	}
}

To use this in the Postprocessor:
Code:
// will stop the current channel when errorcount greater than 10.
stopOnErrorCount(channelId, 10);
return;
Reply With Quote