Summary
This article demonstrates how to run a Job Step only on certain days of the week.
More Information
Note: A more flexible approach for meeting this need is presented in article K00000538: Making execution conditional on a Shared Schedule
It is easy to schedule a job to run only on certain days of the week, using a Schedule Trigger with a Date Rule. However, there are no separate scheduling options for steps within the job.
Suppose you want the job to run daily, but a particular step should only run on Monday. This can be accomplished using a Script Condition that checks the day of the week.
To do this:
1. Edit the script that should run selectively.
2. Go to the Conditions page of the step properties. 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.
3. Click Add... to add a new condition, and select "Depend on the result of a script" for the condition type.
4. 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."
5. On the Script Condition page, click New... to create a new script.
6. In the Script Properties window, make sure the language is set to "VB.NET".
7. Replace the sample script body with the following code:
Imports System
Imports System.Collections.Generic
Imports ArcanaDevelopment.adTempus.Shared
Imports ArcanaDevelopment.adTempus.ApplicationIntegration
Public Class UserScript
Inherits ArcanaDevelopment.adTempus.ApplicationIntegration.ConditionScriptBase
Public Overrides Function Run() As Object
Return DateTime.Now.DayOfWeek=DayOfWeek.Monday
End Function
End Class
Note: This code causes the step to run on Monday only. To run on a different day instead, change the day on line 10.
8. Click Validate to confirm that the code is correct.
9. Click OK to save the script, then OK to save the Condition.
Each time the job is run, adTempus will execute the script to check the date. On Mondays the script will return a True result, indicating that the step should be run. On all other days it will return False, causing adTempus to skip the step.
Note that the script is executed when job execution reaches that step, and looks at the current day when the script is executed. If your job began execution on Monday but runs past midnight and this step doesn't execute until Tuesday, the script will report the current date as Tuesday and the step will not run.
Additional Scenarios
The script can be adapted to handle other date-based scenarios.
Run on Last Sunday of Month
To run a step only on the last Sunday of the month, replace the Run function with the following functions:
Public Overrides Function Run() As Object
Return IsLastOccurrenceInMonth(DateTime.Today, DayOfWeek.Sunday)
End Function
Public Function IsLastOccurrenceInMonth(checkDate As Date, day As DayOfWeek) As Boolean
If checkDate.DayOfWeek <> day Then
Return False
End If
Dim daysLeftInMonth = DateTime.DaysInMonth(checkDate.Year, checkDate.Month) - checkDate.Day
Return daysLeftInMonth < 7
End Function
Notes:
- Move the IsLastOccurrenceInMonth function to a Script Library to allow it to be reused in other scripts.
- In the Run function, change the DayOfWeek value to use a different weekday instead.