Date: Tue, 19 Mar 2024 06:48:54 -0400 (EDT) Message-ID: <893438741.4179.1710845334595@docs.blesta.com> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_4178_2129024839.1710845334594" ------=_Part_4178_2129024839.1710845334594 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
In some cases plugins need to execute tasks on a regular basis. = For example, if your plugin needs to send an email to a fulfillment center = after an invoice is paid you'll want to create a cron task for that.
To add cron tasks have your plugin invoke the CronTasks::add() and CronT= asks::addTaskRun() methods during its install or upgrade process.
Don't forget
= span>You need to remove any cron tasks you create when your plugin is complet= ely uninstalled. First, invoke CronTasks::getTaskRunByKey(), and pass the cron task run id to the CronTasks::deleteTas= kRun method. Then invoke CronTasks::getByKey(), and pass the cron ta= sk ID to the CronTasks::deleteTask() method.
<?php class MyPluginPlugin extends Plugin { =20 ... =20 public function install($plugin_id) { =09=09Loader::loadModels($this, array("CronTasks")); =09=09$task =3D array( =09=09=09'key' =3D> "my_task", // a string used to identify this cron ta= sk (see MyPluginPlugin::cron()) =09=09=09'task_type' =3D> "plugin", // the cron task type, which must be= 'plugin' =09=09=09'dir' =3D> "my_plugin", // the plugin directory of this plugin =09=09=09'name' =3D> "My Plugin Task", // the name of this cron task =09=09=09'description' =3D> "This cron tasks does stuff", // the descrip= tion of this task =09=09=09'type' =3D> "time" // "time" =3D once per day at a defined time= , "interval" =3D every few minutes or hours =09=09); =09=09$task_id =3D $this->CronTasks->add($task); =09=09$task_run =3D array( =09=09=09'time' =3D> "14:25", // the daily 24-hour time that this task s= hould run (optional, required if interval is not given) =09=09=09// 'interval' =3D> "15", // the interval, in minutes, that this= cron task should run (optional, required if time is not given) =09=09=09'enabled' =3D> 1, // 1 if this cron task is enabled, 0 otherwis= e (optional, default 1) =09=09); =09=09$this->CronTasks->addTaskRun($task_id, $task_run); =09=09// Add additional cron tasks as needed... } =20 public function cron($key) { // This method is invoked once for each different cron task configu= red by the plugin and identified by $key } } ?>
Cron tasks are automatically executed at the appropriate time as configu= red by Company > Automa= tion settings.