Scheduled Tasks
Creating a scheduled task
You can add your own tasks to your implementation by following the steps below:
- Create a single class for the task, which implements
\Jadu\ScheduleTask\ScheduleTaskInterface
- Fill out the
boot
method with any pre-task steps required, for example initialising any other objects - Fill out the
run
method with the logic the task needs to execute each time it is triggered - Generate a Doctrine migration
- Complete the
up
anddown
methods in the migration to add and remove your new task from theJaduScheduledTasks
table respectively. - 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.
Column | Description |
---|---|
startDate | The first date on which to run this task. It is recommended to set this to a day in the future for daily tasks. |
endDate | The last date on which to run this task. Set to '1970-01-01' to allow the task to run indefinitely. |
nextRunDate | When creating the task, set this to the same value as startDate . |
scheduleTime | The time of day to run the task. |
scheduleInterval | How frequently the task should run. Accepted values are: tenminutes , daily , weekly , fortnightly (2 weeks), monthly . |
script | The 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.