Summary

This article demonstrates how to create a condition to run a Job or Job Step only on certain days, as defined by a Shared Schedule.

Background

It is easy to schedule a job to run only on certain days, using a Schedule Trigger with a Date Rule. However, adTempus does not support date-based conditions for jobs or steps. Such a condition is useful in situations such as these:

  • You want some steps within a job to only run on certain dates.
  • Your job is included within a job chain, and you only want the job to be run on certain dates. Because the job is triggered by another job as part of the chain, you cannot use a schedule to determine when the job runs.

A future version of adTempus will include a date-based condition. In current versions, the same effect can be accomplished using a Script Condition that checks the current date against a Shared Schedule. The Shared Schedule is used so that the dates can easily be updated without needing to modify the script that the job runs.

Download the Sample

The script code and a sample job and schedule are available in an adTempus export file. Download date condition.adtexport4 and import it using the adTempus Console. Then review the job "Conditional Schedule Test."

You can also create the Shared Script yourself using the code shown below.

Implementation Details

The code that performs the check is implemented as a Shared Script so that it can be referenced from any job or step. You use a Job Variable to tell the script which Shared Schedule to use for your condition.

Using the Script

Once you have the "Date-Based Conditional Execution" imported, it is available to use in any job.

1. When you have a job or step that needs to run only on certain days or dates, create a Shared Schedule to define the appropriate rules.

2. Edit your job and add a Job Variable named "Conditional Schedule Name" and set it to the name of your Shared Schedule. For example, if you want your job to only run on Monday, you would create a Shared Schedule named "Run on Monday Only" and configure it appropriately. Set the "Conditional Schedule Name" variable to "Run on Monday Only".

3. On the Job or Job Step that you want to run conditionally, go to the Conditions page. Make sure the settings are as follows:

  • Condition Criteria: Execute only if all conditions are met
  • If condition(s) are not satisfied: Skip this step and continue to the next step.

4. Click Add... to add a new condition, and select "Depend on the result of a script" for the condition type.

5. In the Script Condition Properties window, go to the Condition Wait page and make sure the setting is "Do not wait for condition to be met."

6. On the Script Condition page, click Select... and select the "Date-Based Conditional Execution" script.

7. Click OK to save the Condition.

Your job or step will now run only on dates that meet the criteria of the Shared Schedule.

Additional Notes

The user account that the job runs under must have permission to connect to the adTempus server and view the Shared Schedule. Otherwise the condition will fail.

If you disable the Shared Schedule (edit the Schedule and uncheck the Enabled box), the job or step will be skipped.

The Script Code

Following is the code for the "Date-Based Conditional Execution" shared script;


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

		Dim scheduleName As String 
	Dim variable=adTempus.JobVariables.GetVariable("Conditional Schedule Name")
	If variable IsNot Nothing Then
		scheduleName=variable.Value 
	End If

	If String.IsNullOrEmpty(scheduleName)
		'throw exception so job reports condition evaluation failure
		adTempus.LogMessage(MessageTypeEnum.Error,0,"Variable ""Conditional Schedule Name"" is not set. Set this variable to the name of the Shared Schedule to use for this condition")
		Throw New Exception("Variable ""Conditional Schedule Name"" is not set.")
	End If
	

	Dim session As Scheduler
	
	Try
		'connect to the local adTempus server under the identity of the job's user.
		session = Scheduler.Connect(".", LoginAuthenticationType.Windows, "", "")
	Catch ex As Exception
		adTempus.LogMessage(MessageTypeEnum.Error,0,"Could not connect to the adTempus server. Be sure the account the job is running under has permission to connect to adTempus and view the Shared Schedule")				
		'throw exception so job reports condition evaluation failure
		Throw
	End Try

	Using session
		Using context = session.NewDataContext()
			Dim schedule = context.GetSharedSchedule(scheduleName)
			If schedule Is Nothing Then
				adTempus.LogMessage(MessageTypeEnum.Error,0,"Could not find Shared Schedule """ & scheduleName & """")				
				'throw exception so job reports condition evaluation failure
				Throw New Exception("Shared Schedule not found")
			End If
			
			If Not schedule.Enabled Then
				adTempus.LogMessage(MessageTypeEnum.Informational,0,"Will not run because Shared Schedule """ & scheduleName & """ is disabled")				
				Return False
			End If
			
			Dim today=DateTime.Today
			
			'SharedSchedule doesn't have a method to tell us if a date matches the schedule,
			'but it does has a method to get all matching dates for a date range.
			'We call that method for today's date, and if the method returns any values
			'we know today's date matches the schedule.
			Dim matchingDates=schedule.GetMatchingDates(today,today)
			If matchingDates.Count>0 Then
				Return True
			Else
				adTempus.LogMessage(MessageTypeEnum.Informational,0,"Will not run because the current date does not meet the rules for Shared Schedule """ & scheduleName & """")				
				Return False
			End If
		End Using
	End Using
    End Function
End Class