Setting up Task for User Defined Class File
I have created a class file that I want to execute daily to gather Metrics (Message Header, Space Available, etc..) and write the data into a Cache table. Now that I have written the class I want to add it to the Task Scheduler within Ensemble to run every morning. How do I go about getting the class file created as a Task within the Task Scheduler? The Documentation isn't as clear cut for creating custom tasks as one would expect.
Comments
Check this example.
In short:
- Create class extending %SYS.Task.Definition
- Add properties - that's task settings
- Implement OnTask method, which returns %Status
- Set TaskName parameter
After that you can add a task of TaskName type from the SMP.
Hi Eduard,
I have a scheduler task and associated class that works.
Class Sample.Util.CreateTask Extends (%SYS.Task.Definition, %Persistent)
{
Parameter PROPERTYVALIDATION = 1;
Parameter TaskName = "Sample.Util.CreateTask";
Method OnTask() As %Status
{
// Perform the logic
}
}
Is there a way to Enable or Disable the abbove task "CreateTask()" from an external function/Method/class ?
You can suspend the task via either ObjectScript or SQL.
Assuming you know the Task ID, and using 1001 as an example:
Set tsk = ##class(%SYS.Task).%OpenId(1001)
Set tsk.Suspended = 1Do tsk.%Save()or
Do##class(%SYS.Task).Suspend(1001,1)Resume the task with
Do##class(%SYS.Task).Resume(1001)Via SQL:
UPDATE %SYS.Task SET Suspended = 1WHEREID = 1001or
UPDATE %SYS.Task SET Suspended = 1WHERE TaskClass = 'Sample.Util.CreateTask'Set Suspended to 0 to re-enable the task.
Thank you , I will try these.
@Scott Roth - you can create a New Task instance via the "Task Schedule Wizard" in the System Management Portal, and in the "Task Type" field, select "RunLegacyTask". Then in the ExecuteCode field, simply type the command to run the desired method in your class. Then you can enter the rest of the details of the Task accordingly (when to run, how often, etc) and it will make the call to your class whenever the Task runs.
Please let me know if more clarification would be helpful.
In "ExecuteCode" do you have to specify the "do ##class....." or can you just enter "##class...."
It is the actual code as it will be executed, so "Do ##class...."
I have an example implemetation in the Ompare tool.
See: https://github.com/alexatwoodhead/ompare/blob/main/src/cls/ompare/Sched…
Extends: %SYS.Task.Definition
Implements: OnTask method
By the way, you can run Schedules programatically from IRIS Session outside of the Task Scheduler if you are investigating / debugging the behaviour. For example:
settBASE=##class(ompare.Schedule).%New() settBASE.Environment="BASE"settBASE.Namespaces="OMPARE-BASE,INST-*,-INST-TEMP"settBASE.RunSourceHandlers=1settBASE.ExportToFile=1settBASE.ExportDirectory="C:\TMP\ompare\"settBASE.ExportCompression=0settBASE.DiscardProfileData=1settBASE.RetainExportDays=0settBASE.IncludeSourceCode=1settSC1=tBASE.OnTask()
The reason I would subclass %SYS.Task.Definition is that the Management Portal knows how to display input configuration fields for your class properties.
This is more visably useful / supportable. Easier to see the task paramaters.
They can have validation also (Required, Pattern, MINLEN, MAXLEN, Setter methods, %OnSave methods).
Hope that helps :)