Question
· Dec 5, 2019

Task schedule - once a month but different day each month

I need  to ran a task on a specified days (once a month but a different day each month).

How can I do that in Task Manager?

Discussion (4)0
Log in or sign up to continue

Looks like rescheduling running task does not work. But it is possible to create a new one, which runs once.

Class User.Test Extends %SYS.Task.Definition
{
Parameter TaskName = "SomeTask";
Method OnTask() As %Status
{

  Set task = ##class(%SYS.Task).%New()
  Set task.Name = "Test task"
  Set task.NameSpace = $Namespace
  Set task.TaskClass = ..%ClassName(1)
  Set task.TimePeriod = 5 // RunOnce
  Set task.RescheduleOnStart = 0
  Set tSC = task.%Save()
  If $$$ISERR(tSC) {
    Quit tSC
  }
  
  Set nextDate = +$Horolog + $Random(10) + 1
  Set nextTime = $ZTimeh("10:00")
  Quit ##class(%SYS.Task).RunOnce(task.%Id(), nextDate, nextTime)
}
}

I have found a workaround.

1. Create task class as usual.

2. Create this subclass extending (1)

/// Run it daily but it would actually run only on Dates
Class util.CustomDatesTask Extends util.BaseTask
{

Parameter TaskName = "BaseTask (random dates)";

/// Comma separated Dates in YYYY-MM-DD format
/// Example: 2019-12-11,2020-01-17,2020-02-11,2020-03-10,2020-04-09,2020-05-12
Property Dates As %VarString;

/// Check that Dates is valid
Method %OnValidateObject() As %Status
{
    #dim sc As %Status = $$$OK
    set sc = ##super()
    quit:$$$ISERR(sc) sc
    try {
        set dates = $lfs(..Dates)
    } catch ex {
        set sc = ex.AsStatus()
        set sc = $$$ADDSC($$$ERROR($$$GeneralError, "Incorrect Dates value: " _ ..Dates), sc)
    }
    quit:$$$ISERR(sc) sc
    for i=1:1:$ll(dates) {
        try {
            set date = $lg(dates, i)
            set temp = $zdh(date, 3)
        } catch ex {
            set sc = ex.AsStatus()
            set sc = $$$ADDSC($$$ERROR($$$GeneralError, "Incorrect Date value: " _ date), sc)
        }
        quit:$$$ISERR(sc)
    }
    quit sc
}

Method OnTask() As %Status
{
    #dim sc As %Status = $$$OK
    set dates = $lfs(..Dates)
    set curDate = $zd($h, 3)
    if $lf(dates, curDate) {
        // Execute the task
        set sc = ##super()
    }
    quit sc
}
}

The advantage is that schedule is easy to set as a task config property. Drawback is that logs would be created for each day.