adTempus API
PreviousUpNext
Starting a Job

This example illustrates how to start a job, given its name.

Description

Two functions are defined below:

  • RunJob finds a job based on its name and submits it for execution, using standard execution options.
  • RunJobAndWait demonstrates a new capability provided by Job.ExecuteJob: it calls RunJob to start the job, and then waits for the instance(s) started by RunJob to complete.
 

The methods requires an existing client connection. The Creating a Client Session contains code for creating a client connection. The following code shows how to obtain a session and run the job: 

 

dim theScheduler as Scheduler

    'connect to the adTempus server on the local computer, using Windows authentication
    theScheduler=ConnectAdTempusServer("","","")

    'Run the job named "test job" and wait for it to finish.
    RunJobAndWait(theScheduler,"test job")

 

 

Implementation of the Run methods: 

 

    Public Sub RunJobAndWait(ByVal theScheduler As Scheduler,jobName as string)
        Dim requestID As String


        requestID = RunJob(theScheduler, jobName)
        If requestID.Length = 0 Then
            'job couldn't be run
            Return
        End If

        'else the requestID can be used to get the instance(s) for the job
        Dim instances As ADTObjects
        Dim instance As ExecutionHistoryItem
        Dim activeInstances As Integer

        'GetInstancesForRequest gets all instances of the job
        'that were started as a result of our execution request.
        'we can examine them to determine the status of each instance
        'in this case we're going to wait until all instances are finished
        instances = theScheduler.GetInstancesForRequest(requestID)
        While True
            activeInstances = 0

            For Each instance In instances
                instance.Refresh()  'be sure to refresh to get latest status from server
                If instance.IsRunning Then
                    activeInstances += 1
                End If
            Next

            'if all instances have finished, return
            If activeInstances = 0 Then
                Return
            End If

            'otherwise wait one minute, then check again
            System.Threading.Thread.Sleep(60000)
        End While
    End Sub

    'RunJob starts the job with the given name, and returns the execution request ID
    'for the request.
    Public Function RunJob(ByVal theScheduler As Scheduler, ByVal jobname As String) As String
        Dim job As Job
        Dim options As New JobExecutionOptions

        'get the job. See the Getting a Job by Name example for the implementation of JobGetter.GetJobForName
        job = JobGetter.GetJobForName(theScheduler, jobname)
        If job Is Nothing Then
            'no job found
            Return ""
        End If

        'set whatever options are appropriate:
        'eoForceNewInstance: force a new instance even if another is running
        'eoIgnoreHeld: run the job even if held
        'eoIgnoreJobConditions: ignore job-level conditions
        'eoignorestepconditions: ignore conditions for steps
        options.options = ExecutionOptionsEnum.eoForceNewInstance _
         Or ExecutionOptionsEnum.eoIgnoreHeld _
         Or ExecutionOptionsEnum.eoIgnoreJobConditions

        '
        'This function succeeds unless the user doesn't have
        'permission or some unexpected error occurs
        'It doesn't fail if the job does--to determine whether the
        'job was started you
        'have to look at the status
        '
        job.ExecuteJob(options)
        Return options.ExecutionRequestID
    End Function
adTempus API Reference version 3.0.0.0, revised 10/30/2008