adTempus API
PreviousUpNext
Creating a Job with Responses

This example demonstrates how to create a more complex job, including Responses to respond to error conditions.

Description

This example illustrates:

  • Creating a job step to run a program
  • Setting the exit code success criteria for the step
  • Using a Response to retry the step if it fails
  • Using a Response to send e-mail notification if the step fails more than 5 times
 

This example shows the minimum amount of code needed to create a simple job. Most properties are left set to their default values. No error handling is provided. 

The method requires an existing client connection. See Creating a Client Session for an example of creating a client connection. 

 

    Public Sub CreateJobWithResponse(ByVal theScheduler As Scheduler)
        Dim job1 As Job
        Dim errors As New ADErrors
        Dim errorSeverity As ADErrorSeverity
        Dim ipv As IProvideValidation
        Dim step1 As JobStep
        Dim progex As ProgramExecutionTask
        Dim recipient As NotificationIndividual
        Dim response As Response
        Dim eventFilter As JobEventFilter
        Dim notificationaction As NotificationAction
        Dim restartAction As JobControlAction

        'Data objects (objects derived from IADTObject) cannot be directly created.
        'They must be created through a call to Scheduler.CreateObject()
        job1 = CType(theScheduler.CreateObject(ClassIDEnum.CID_Job), Job)

        job1.Name = "API Sample 2"

        'See the Credential Profile Retrieval example for the implementation of CredentialProfileGetter.GetCredentialProfile
        'replace with the appropriate credentials
        job1.Credentials = CredentialProfileGetter.GetCredentialProfile(theScheduler, "mydomain\myuserID", "mypassword")
        'we'll use the defaults for all other Job properties

        '********************* Begin Step 1 *********************
        'Add step 1
        'This step will execute "someprogram.exe"
        'The program returns an exit code > 4 if it fails
        'if it fails, we will retry it up to 5 times at 2-minute intervals,
        'then fail the step and send an e-mail notification message

        'first create a step
        step1 = CType(theScheduler.CreateObject(ClassIDEnum.CID_JobStep), JobStep)

        'and a task. Use a ProgramExecutionTask to run a program
        progex = CType(theScheduler.CreateObject(ClassIDEnum.CID_ProgramExecutionTask), ProgramExecutionTask)

        'we'll run "someprogram.exe"
        progex.ExecutionTarget = "someprogram.exe"
        progex.WindowMode = WindowModeEnum.wmNormal 'we want it to be visible


        'we want to look at the exit code to determine success
        progex.SuccessCriterion = SuccessCriterionEnum.scExitCode

        'success is indicated by an exit code <= 4
        progex.ExitCodeCriterion = ExitCodeTestTypeEnum.exitCodeLE
        progex.SuccessMinCode = 4

        'if the task fails, we want to retry it.
        'create the response
        response = CType(theScheduler.CreateObject(ClassIDEnum.CID_Response), ArcanaDevelopment.adTempus.Client.Response)

        'create an event filter to trigger the response when the step fails
        eventFilter = CType(theScheduler.CreateObject(ClassIDEnum.CID_JobEventFilter), JobEventFilter)
        eventFilter.Event = JobEventEnum.jeStepFailed
        response.Events.Add(eventFilter)

        'now the action to restart
        restartAction = CType(theScheduler.CreateObject(ClassIDEnum.CID_JobControlAction), JobControlAction)
        restartAction.ControlType = JobControlActionsEnum.jcaRestartStep
        restartAction.RestartDelay = 120 'wait 120 seconds (=2 minutes) before trying again()
        restartAction.RetryLimit = 4 'retry up to 4 times

        response.actions.Add(restartAction)
        step1.Responses.Add(response)



        'we want to send e-mail notification if the task still doesn't succeed after the 4 retries.
        'first create the recipient. This method will retrieve the existing
        'recipient object if one exists, or create a new one if necessary
        'The GetRecipient method is defined here.
        recipient = NotificationRecipientGetter.GetRecipient(theScheduler, "joe user", "[email protected]")

        'create another response
        response = CType(theScheduler.CreateObject(ClassIDEnum.CID_Response), ArcanaDevelopment.adTempus.Client.Response)

        'create an event filter to trigger the response when the restart limit Is exceeded
        eventFilter = CType(theScheduler.CreateObject(ClassIDEnum.CID_JobEventFilter), JobEventFilter)
        eventFilter.Event = JobEventEnum.jeRestartLimitExceeded
        response.Events.Add(eventFilter)

        'create the action to send the notification
        notificationaction = CType(theScheduler.CreateObject(ClassIDEnum.CID_NotificationAction), ArcanaDevelopment.adTempus.Client.NotificationAction)
        notificationaction.Recipients.Add(recipient)

        'we'll use the default subject and message, so we don't need to do anything else with the action
        response.actions.Add(notificationaction)
        step1.Responses.Add(response)

        'assign the task to the step
        step1.Task = progex

        'add the step to the job
        job1.Steps.Add(step1)

        '********************* End Step 1 *********************



        'before we save the job we'll validate it
        'Saving automatically validates first, but if Save returns validation errors,
        'we'd have to call Validate
        'anyway to get details of the error
        'first cast the job to an IProvideValidation interface
        ipv = CType(job1, IProvideValidation)
        errorSeverity = ipv.Validate(errors)
        If errorSeverity > ADErrorSeverity.adetWarning Then
            'one or more errors were reported.
            'examine the contents of the errors collection for details
        Else
            'validation was successful. Save the job.
            job1.Save()
        End If

    End Sub
adTempus API Reference version 3.0.0.0, revised 10/30/2008