Scheduling a Job from Code

It is possible to add a Job to the scheduler’s  Job Plan (queue)  from code using the SchedulerService.

First you will need to know the name of the Job you want to schedule. You can find them it using the Scheduler Job Maintenance of in the SmartFramework Maintenance menu:

 

Figure 1 Scheduler Job Maintenance in the SmartFramework Maintenance menu

 

The Scheduler Job Maintenance shows a list of the available jobs as well as more detailed information about the selected jobs. This example will use the “[SmartFramework] Cleanup Scheduler Job status records” job.

 

Figure 2 The job in the list

 

What we need from this to run the job from code is the “Job Name” fields content. We should also look at the ISchedulerJobCommand implementation it uses, in this case CleanupSchedulerJobCommand, to know whether it expects a filled parameter object.

Looking at its Start method we can see that it does not use the poParameter argument.

 

METHOD PUBLIC VOID Start (poParameter AS Progress.Lang.Object): DEFINE VARIABLE oJobStatus AS SchedulerJobStatusDatasetModel NO-UNDO. oJobStatus = NEW SchedulerJobStatusDatasetModel () . oJobStatus:CleanupJobStatus (NEW CleanupJobStatusParameter (THIS-OBJECT:DeleteFailedFrom, THIS-OBJECT:DeleteFinishedFrom, THIS-OBJECT:DeleteCancelledFrom, THIS-OBJECT:DeleteRunningFrom)) . FINALLY: GarbageCollectorHelper:DeleteObject (oJobStatus). END FINALLY. END METHOD .

 

The SchedulerService can be used from code to add a job to the scheduler’s queue. Its method ScheduleJob can take four parameters:

  • pcSchedulerJobName [character] is a mandatory parameter and must contain the job name from the Scheduler Job Maintenance.

  • pcJobName [character] is an optional parameter that can be used to change the name of the job status in the queue. This can make it easier to find it later. The default value is pcSchedulerJobName.

  • pdtScheduleDateTime [datetime-tz] is an optional parameter to set the time at which the job is scheduled to start. The default value is now.

  • poJobParameter [ISerializable] is a mandatory parameter that can be used to pass information to the ISchedulerJobCommand instance. It will always need to be a valid object implementing ISerializable but which fields it needs and whether they need to be filled depends on the specific ISchedulerJobCommand. As we have seen earlier, the CleanupSchedulerJobCommand does not use the parameter. So, any valid instance of ISerializable would work.

 

If the optional parameters are not needed they can be left out as SchedulerService:ScheduleJob has overloaded variants for those cases.

As an example, to add the job under its default name and scheduled for now would look like this:

DEFINE VARIABLE cName AS CHARACTER NO-UNDO. DEFINE VARIABLE oResponse AS IScheduleJobResponse NO-UNDO. DEFINE VARIABLE oService AS ISchedulerService NO-UNDO. DEFINE VARIABLE oParam AS ISerializable NO-UNDO. cName = "[SmartFramework] Cleanup Scheduler Job status records":u. oService = {Consultingwerk/get-service.i ISchedulerService "new SchedulerService()"}. oParam = NEW CleanupJobStatusParameter (). oResponse = oService:ScheduleJob (cName, oParam). MESSAGE oResponse:SchedulerJobGuid SKIP oResponse:SchedulerJobStatusGuid VIEW-AS ALERT-BOX.

This example passes only the name of the job and an empty parameter to the service. The response object returned by it contains the SchedulerJobGuid of the SmartSchedulerJob with the given name as well as the SchedulerJobStatusGuid for the SmartSchedulerJobStatus record that the service created.

In the Scheduler Job Status Monitor we can see that this has added a new and non-recurring job scheduled for now with the same name as the SmartSchedulerJob.

It is also possible to set a different name and schedule the job for a different time. This example adds the same job but calls it “Lorem ipsum” and schedules it for 2111-11-11 at 11:11 o’clock.

DEFINE VARIABLE cName AS CHARACTER NO-UNDO. DEFINE VARIABLE oResponse AS IScheduleJobResponse NO-UNDO. DEFINE VARIABLE oService AS ISchedulerService NO-UNDO. DEFINE VARIABLE oParam AS ISerializable NO-UNDO. DEFINE VARIABLE cStatusName AS CHARACTER NO-UNDO. DEFINE VARIABLE dtScheduled AS DATETIME NO-UNDO. ASSIGN cName = "[SmartFramework] Cleanup Scheduler Job status records":u cStatusName = "Lorem ipsum":u dtScheduled = DATETIME(11, 11, 2111, 11, 11). . oService = {Consultingwerk/get-service.i ISchedulerService "new SchedulerService()"}. oParam = NEW CleanupJobStatusParameter (). oResponse = oService:ScheduleJob (cName, cStatusName, dtScheduled, oParam). MESSAGE oResponse:SchedulerJobGuid SKIP oResponse:SchedulerJobStatusGuid VIEW-AS ALERT-BOX.

The name helps finding the new job in the list of all current jobs.

The date can be in the past or future, but setting a date in the past has the same effect as setting it to the current date and time.

There are variations of the ScheduleJob method without either pcJobName or pdtScheduleDateTime.