An extension can run some of its routines periodically, for example, every 10 minutes, every hour, or daily. This scheduler interface manages the
crontab utility on Linux and the Task Scheduler application on Windows. The interface is presented by two classes - pm_Scheduler and pm_Scheduler_Task. The first class represents a task manager and the second class represents a task.
To successfully schedule a task, your code must meet the following requirements:
- Put a scheduled task under
/plib/scripts/relatively to the extension's root directory.
- Initialize the extension context (pm_Context::init) before scheduling a task.
- Schedule the task using pm_Scheduler and pm_Scheduler_Task.
A sample code that adds a periodic task to the scheduler can look as follows:
See an example of using scheduled tasks in Exercise 2.
Designing a Run Schedule
When designing a run schedule, you should pay attention to the following operating-system differences:
- On Linux, the schedule may be specified in the cron format. Learn more about the format at: http://www.nncron.ru/help/EN/working/cron-format.htm.
- On Windows, the schedule may be set by using commas (,) and asterisks (*).
- We recommend that you use the following cross-platform constants when defining a period between two task runs:
- On Windows, a task is limited to 48 different start conditions (triggers). Learn more about triggers and the limitation at:
If you need to loop through scheduled tasks, do it in the following way:
Removing Scheduled Tasks
We highly recommend that you save IDs of your scheduled tasks to the key-value storage and then remove the tasks during the removal of your extension. The following code removes a task:
Passing Arguments to Tasks
If you need to pass certain arguments to a task, use the setArguments method. The following code creates a task with arguments:
To retrieve a task's arguments, use the getArguments method, for example, in the following way:
Note that a task ID is assigned by the putTask method.