Summary

This article demonstrates how to send a notification message from an adTempus job that lists the status of all the steps in the job.

Background

It's easy in adTempus to send a notification message when a job step succeeds or fails, and the default notification message gives the status of the step that triggers the Response.

However, in some cases you might want to send a notification message that gives the status of all steps. For example, you might have a job that is configured to continue running even if some steps fail. At the end of the jobor if a particular step failsyou might want to send a notification that gives information on all the steps that have run.

This can be done using a script that reads the job history and constructs the message.

Implementation

The implementation consists of a Shared Script to pull together the status information, and a Response to run the script and send an e-mail message using a Notification Action.

Script

We have created a Shared Script to get the step status information. The script uses the adTempus API to retrieve the history record for the active job instance. It iterates through the steps for the instance and constructs an HTML table listing basic status information for each step (step number, name, start time, end time, status, and result). The resulting HTML code is placed in a Job Variable, which can be used to insert the table in an e-mail message.

Imports System
Imports System.Text
Imports System.Collections.Generic
Imports ArcanaDevelopment.adTempus.Shared
Imports ArcanaDevelopment.adTempus.ApplicationIntegration
Imports ArcanaDevelopment.adTempus.Client

Public Class UserScript
    Inherits ArcanaDevelopment.adTempus.ApplicationIntegration.ResponseActionScriptBase

    Public Overrides Function Run() As Object

		'connect to the local adTempus server under the identity being used for the job
		'We get the server/instance name from the adTempus.ServerAndInstanceName variable so the
		'script will work on any adTempus instance, including named (non-default) instances
		Dim session=ArcanaDevelopment.adTempus.Client.Scheduler.Connect(adTempus.JobVariables("adTempus.ServerAndInstanceName"), LoginAuthenticationType.Windows,"","")
		Dim context=session.NewDataContext

		'get the identity of the current job instance. This is set by adTempus in a variable
		Dim instanceID=adTempus.JobVariables("ADJobInstance")
		
		'convert the identity to an OID, which the API expects
		Dim instanceOID=New OID(ClassID.ExecutionHistoryItem,Guid.Parse(instanceID))
		
		'fetch the history record for the current instance
		Dim instance = CType(context.GetObject(instanceOID), ExecutionHistoryItem)
		If instance Is Nothing Then
			adTempus.LogMessage(MessageTypeEnum.Error,0,"Cannot find history record for current job instance")
			Return False
		End If

		'go through the steps and build a table showing status information for each
		Dim sb As New StringBuilder()
		sb.Append("<table border=""1"" style=""border-collapse:collapse;"">")
		sb.Append("<thead><tr><td>Step</td><td>Status</td><td>Result</td><td>Start</td><td>Finish</td></tr></thead>")
	
		For Each jobStep As ExecutionHistoryStep In instance.Steps
			sb.AppendFormat("<tr><td>{0}. {1}</td><td>{2}</td><td>{5}</td><td>{3}</td><td>{4}</td></tr>", 
			jobStep.StepNumber, 
			jobStep.GetDescription(), 
			jobStep.StatusDescription ,
			If(jobStep.ExecutionStart.HasValue,jobStep.ExecutionStart.Value.ToString(),""),
			If(jobStep.ExecutionFinish.HasValue,jobStep.ExecutionFinish.Value.ToString(),""),
			jobStep.Result
			)
		Next
	
		sb.Append("</table>")

		'now put the table in a variable so it can be used later
		adTempus.JobVariables.Add("StepStatusSummaryTable",sb.ToString(),False)
		Return True
    End Function
End Class

Response

The Response can be configured at the Job or Step level and triggered by the appropriate, such as Job Failed. The response runs two actions:

  1. Run the Shared Script. This collects the status information and puts it in a Job Variable.
  2. Run a Notification Action. The Notification Action uses the variable created by the script to insert the step status list.

Because we have formatted the step status information as an HTML table, the e-mail message must also be constructed with HTML formatting:

<html><body>
<p>Job %ADTFullyQualifiedJobName% has failed. Status of steps that have run:</p>
%StepStatusSummaryTable%
</body></html>

The e-mail message editor in adTempus does not support WYSIWIG editing for HTML, so you must write the HTML markup yourself.

The "%StepStatusSummaryTable%" token gets replaced at runtime with the table created by the script.

When adTempus sends the message, it sees that the body starts with an "<html>" tag and sends the message in HTML format.

Download a Sample Job

A sample job is available that demonstrates this technique. Download the adTempus export file here and import it using the Import tool in the adTempus Console. This will create a Shared Script named "Create step status summary" and a Job in the "Samples" group named "Response with step status email."

The job has a job-level Response (triggered on job failure) that runs the script and sends the e-mail message.

Caveats

The script uses Windows authentication to connect to the adTempus server. The Windows user account that the job is running under must have an associated adTempus login, and must have permission to view the job.