Skip to main content

Scheduled Tasks

Creating a scheduled task

You can add your own tasks to your implementation by following the steps below:

  1. Create a single class for the task, which implements \Jadu\ScheduleTask\ScheduleTaskInterface
  2. Fill out the boot method with any pre-task steps required, for example initialising any other objects
  3. Fill out the run method with the logic the task needs to execute each time it is triggered
  4. Generate a Doctrine migration
  5. Complete the up and down methods in the migration to add and remove your new task from the JaduScheduledTasks table respectively.
  6. Package and deploy to your Jadu Central system

Scheduled task class

Below is an example scheduled task class skeleton.

    namespace Custom\ProjectBundle\ScheduleTask;

use Jadu\ScheduleTask\ScheduleTaskInterface;

class MyScheduledTask implements ScheduleTaskInterface
{
public function boot()
{
require_once 'JaduConstants.php';
}

public function run($params)
{
// do some important stuff here
}
}

Database migration

The database entry will detemine how frequently the task will be executed.

ColumnDescription
startDateThe first date on which to run this task. It is recommended to set this to a day in the future for daily tasks.
endDateThe last date on which to run this task. Set to '1970-01-01' to allow the task to run indefinitely.
nextRunDateWhen creating the task, set this to the same value as startDate.
scheduleTimeThe time of day to run the task.
scheduleIntervalHow frequently the task should run. Accepted values are: tenminutes, daily, weekly, fortnightly (2 weeks), monthly.
scriptThe fully qualified class name for your scheduled task class.

Example migration:

    namespace DoctrineMigrations;

use Doctrine\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;

class Version20200527123456 extends AbstractMigration
{
private $className = '\Custom\ProjectBundle\ScheduleTask\MyScheduledTask';

public function up(Schema $schema)
{
$tomorrow = date('Y-m-d', strtotime('tomorrow'));

$sql = <<<SQL
INSERT INTO JaduScheduledTasks (
startDate, endDate, nextRunDate, scheduleTime, scheduleInterval, script
) VALUES (
:tomorrow, '1970-01-01', :tomorrow, '01:00', 'daily', :class_name
)
SQL;

$this->addSql($sql, [$tomorrow, $tomorrow, $this->className]);
}

public function down(Schema $schema)
{
$this->addSql("DELETE FROM JaduScheduledTasks WHERE script = :class_name", [$this->className]);
}
}
note

The class name parameter may need to be escaped differently if running against SQL Server

Troubleshooting tasks

See our troubleshooting guide for more information on troubleshooting Scheduled tasks.