Summary

This article demonstrates how to use a Script Condition to configure a job to run only if no other jobs are running.

Overview

A Job Condition can be used to prevent a Job from running if one or more specified other jobs are already running. In some cases you may want to run a job only if no other jobs are running, and it would be impractical to configure the Job Condition to exclude all other jobs on the system.

Instead, you can use a Script Condition to query adTempus (using the adTempus API) to determine whether any other jobs are already running.

Configuration

Security Configuration

The script will connect to adTempus using the API, which uses the same security framework as the adTempus Console. The script will connect using the identity of the account that the job is running under, so this account must be configured with a Security Login in adTempus. This login should be granted at least View permission for the root Job Group (the "Jobs" folder).

For example, if your job is running using the credential profile for "corpnet\fred", there must be a Security Login configured to allow "corpnet\fred" permission to log in to adTempus and view all jobs.

Job Configuration

In the Job Properties, go to the Conditions page and click Add to add a new condition. Select "Depend on the result of a script" for the condition type.

This will open the Script Condition Properties window. Go to the Script Condition page and click New to add a new script.

In the Script Properties window, leave the language set to "VB.NET."

In the Referenced Assemblies section, click Add, then Browse. Locate the "ArcanaDevelopment.adTempus.Client.dll" in the adTempus program folder and select it, then click OK to add the reference.

In the Script Code section of the window, replace the template script with the following script code:


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

Public Class UserScript
    Inherits ArcanaDevelopment.adTempus.ApplicationIntegration.ConditionScriptBase

    Public Overrides Function Run() As Object

        'connect to the local adTempus server using Windows authentication
        'Note: The account that the job is running under must be configured with an adTempus login
        Using scheduler=ArcanaDevelopment.adTempus.Client.Scheduler.Connect()
            Using context=scheduler.NewDataContext()
            
                'look at instances with an Active (Running, etc.) or Waiting (Waiting for Condition, etc.)
                Dim parms=New InstanceQueryParameters()
                parms.Statuses.AddRange(JobStatusHelpers.ActiveStatuses)
                parms.Statuses.AddRange(JobStatusHelpers.WaitingStatuses)
                
                'get a count of
                Dim jobCount=context.CountJobHistory(parms)
                
                'Report the job count so it shows up in the Conditions tab for the instance properties
                SetConditionStatus("Active job count: " & jobCount)

                'The current job will be reported as active, so the count should always be at least 1.
                If jobCount > 1
                    'There's at least one job running other than this one. Don't allow job to continue
                    Return False
                Else
                    'No other jobs are running. Allow job to continue
                    Return True
                End If
            End Using
        End Using
        
    End Function
End Class

Click OK to save the script and return to the Script Condition Properties window.

Go to the Condition Wait page and set the wait options as appropriate.

Click OK to save the Condition.

Configure the rest of the job as necessary.

Explanation

When the job is triggered, the script will be executed. It will connect to the adTempus server and get a count of the number of active job instances. There will always be at least 1 instance, because the job that the condition is attached to will be counted. If the count is greater than 1, this indicates that other jobs are running. In this case the script returns False, which tells adTempus not to execute the job. If you have configured the job to wait for the condition to be met, adTempus will continue executing the script periodically until the wait time limit is exceeded or the script reports that no other jobs are running. 

Troubleshooting

Common error scenarios:

Script compilation error: 'Client' is not a member of 'adTempus'. (BC30456) (Line 14, Column 0) [ADT005066E]

This indicates that you did not add a reference to the ArcanaDevelopment.adTempus.Client.dll file as described above.

Script execution failed because the script threw an exception: You do not have permission to connect to the adTempus server. [ADT000001E][ADT005070E]

This indicates that the user account that the job runs under does not have a Security Login configured in adTempus.