Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

To add cron tasks have your plugin invoke the CronTasks::add() method and CronTasks::addTaskRun() methods during its install or upgrade process.

Note
titleDon't forget

You need to remove any cron tasks you create when your plugin is completely uninstalled. Do this by invoking First, invoke CronTasks::getTaskRunByKey(), and pass the cron task run id to the CronTasks::deleteTaskRun method. Then invoke CronTasks::getByKey(), and pass the cron task ID to the CronTasks::deletedeleteTask() method.


Code Block
languagephp
title/plugins/my_plugin/my_plugin_plugin.php
<?php
class MyPluginPlugin extends Plugin {
 
    ...
 
    public function install($plugin_id) {
		Loader::loadModels($this, array("CronTasks"));

		$task = array(
			'key' => "my_task", // a string used to identify this cron task (see MyPluginPlugin::cron())
			'task_type'plugin_ => "plugin", // the cron task type, which must be 'plugin'
			'dir' => "my_plugin", // the plugin directory of this plugin
			'name' => "My Plugin Task", // the name of this cron task
			'description' => "This cron tasks does stuff", // the description of this task
			'type' => "time" // "time" = once per day at a defined time, "interval" = every few minutes or hours
		);
		$task_id = $this->CronTasks->add($task);

		$task_run = array(
			'time' => "14:25", // the daily 24-hour time that this task should run (optional, required if interval is not given)
			// 'interval' => "15", // the interval, in minutes, that this cron task should run (optional, required if time is not given)
			'enabled' => 1, // 1 if this cron task is enabled, 0 otherwise (optional, default 1)
		);
		$this->CronTasks->addTaskRun($task_id, $task_run);

		// Add additional cron tasks as needed...
    }
 
    public function cron($key) {
        // This method is invoked once for each different cron task configured by the plugin and identified by $key
    }
}
?>

...