Create response to send notification email on job failure

Languages: C#, VB.NET

View on GitHub to download or comment.

See the Client API Examples Introduction for additional information and prerequisites.

Samples index

This example uses the adTempus API to add a Notification Action to a job to send an email message when the job fails.

It also demonstrates how to create a Notification Recipient for an email recipient.

sample.cs
public void Main()
    {

        // if the adTempus server is on a different computer, replace "." with the server name
        using (var session = Scheduler.Connect(".", LoginAuthenticationType.Windows, "", ""))
        {
            using (var context = session.NewDataContext())
            {

                // In this example we fetch a sample job.
                var job = context.GetJob("Sample Job");
                if (job == null)
                    throw new Exception("Job not found");

                // create the Response
                var response = (Response)context.CreateObject(ClassID.Response);

                // Create the Event to trigger on job failed
                var responseEvent = (ResponseEvent)context.CreateObject(ClassID.ResponseEvent);
                responseEvent.Event = JobEvent.JobFailed;
                response.Events.Add(responseEvent);


                // find or create the recipient
                var recipient = GetOrCreateNotificationRecipient(context, "[email protected]", "Someone");

                // create a Notification Action
                var action = (NotificationAction)context.CreateObject(ClassID.NotificationAction);

                action.Recipients.Add(recipient);

                // If you don't set a subject or message, the defaults are used.
                // If you set them explicitly, use Job Variables to insert job name, computer name, etc., rather than
                // hard-coding them. See the list of predefined variables
                // action.NotificationSubject="Job %ADTJobName% failed on %ServerName%"
                // action.NotificationMessage="The job failed"

                // include Captured Console output
                action.SendConsoleOutput = true;

                response.Actions.Add(action);

                job.Responses.Add(response);

                job.Save();
            }
        }
    }

    // returns the existing NotificationRecipient for an email address, or creates a new one if none is found
    public void GetOrCreateNotificationRecipient(ArcanaDevelopment.adTempus.Client.DataContext context, string emailAddress, string name)
    {
        var recipient = context.GetNotificationRecipientsForAddress(NotificationAddressType.SMTP, emailAddress).FirstOrDefault();

        if (recipient != null)
            // there's already a NotificationRecipient that uses this address
            return recipient;

        // otherwise create it
        // recipient not found; create it
        recipient = (NotificationIndividual)context.CreateObject(ClassID.NotificationIndividual);
        recipient.Name = name;

        var address = (NotificationAddress)context.CreateObject(ClassID.NotificationAddress);
        address.AddressType = NotificationAddressType.SMTP;
        address.RecipientAddress = emailAddress;

        recipient.Addresses.Add(address);
        recipient.Save();
        return recipient;
    }
sample.vb
Sub Main

	'if the adTempus server is on a different computer, replace "." with the server name
	Using session = Scheduler.Connect(".", LoginAuthenticationType.Windows, "", "")
		Using context = session.NewDataContext()

			'In this example we fetch a sample job.
			Dim job = context.GetJob("Sample Job")
			If job Is Nothing Then
				Throw New Exception("Job not found")
			End If

			'create the Response
			Dim response = Ctype(context.CreateObject(ClassID.Response), Response)

			'Create the Event to trigger on job failed
			Dim responseEvent = CType(context.CreateObject(ClassID.ResponseEvent), ResponseEvent)
			responseEvent.Event = JobEvent.JobFailed
			response.Events.Add(responseEvent)


			'find or create the recipient
			Dim recipient = GetOrCreateNotificationRecipient(context,"[email protected]", "Someone")

			'create a Notification Action
			Dim action = CType(context.CreateObject(ClassID.NotificationAction), NotificationAction)

			action.Recipients.Add(recipient)

			'If you don't set a subject or message, the defaults are used.
			'If you set them explicitly, use Job Variables to insert job name, computer name, etc., rather than
			'hard-coding them. See the list of predefined variables
			'action.NotificationSubject="Job %ADTJobName% failed on %ServerName%"
			'action.NotificationMessage="The job failed"

			'include Captured Console output
			action.SendConsoleOutput = True

			response.Actions.Add(action)

			job.Responses.Add(response)

			job.Save()
		End Using
	End Using

End Sub

'returns the existing NotificationRecipient for an email address, or creates a new one if none is found
Function GetOrCreateNotificationRecipient(context As ArcanaDevelopment.adTempus.Client.DataContext, emailAddress As String, name As String)
	Dim recipient = context.GetNotificationRecipientsForAddress(NotificationAddressType.SMTP, emailAddress).FirstOrDefault()

	If recipient IsNot Nothing Then
		'there's already a NotificationRecipient that uses this address
		Return recipient
	End If

	'otherwise create it
	'recipient not found; create it
	recipient = CType(context.CreateObject(ClassID.NotificationIndividual), NotificationIndividual)
	recipient.Name = name

	Dim address = CType(context.CreateObject(ClassID.NotificationAddress), NotificationAddress)
	address.AddressType = NotificationAddressType.SMTP
	address.RecipientAddress = emailAddress
	
	recipient.Addresses.Add(address)
	recipient.Save()
	Return recipient
End Function

Comments

View on GitHub to comment.