Introduction
For each task you needed to schedule on your server, you may have generated a Cron entry in the past. This can quickly become a headache, because your schedule of tasks is no longer in source control and you must SSH into your server to add Cron entries.
The command scheduler allows you to define your command schedule within the application itself fluently and expressively. Only one single Cron entry is required on your server when using the scheduler.
Note: See the installation guide for instructions on how to set up the task scheduler.
Task Scheduling is how scheduling time-based tasks are managed in TastyIgniter. Several core features of TastyIgniter, such as checking for updates, use the scheduler.
Defining Schedules
You may define all of your scheduled tasks by overriding the registerSchedule
method within the Extension registration class. The method takes a single argument for $schedule
and is used together with their frequency to define commands.
To get started, let's look at an example of how to schedule a task. In this example, we schedule to call a closure at midnight every day. We will execute a database query within the Closure
to clear a table:
class Extension extends \System\Classes\BaseExtension
{
[...]
public function registerSchedule($schedule)
{
$schedule->call(function () {
\Db::table('recent_users')->delete();
})->daily();
}
}
In addition to scheduling Closure
calls, you may also schedule console commands and operating system commands. For example, to schedule a console command, you can use the command
method:
$schedule->command('cache:clear')->daily();
The exec
method may be used to issue a command to the operating system:
$schedule->exec('node /home/acme/script.js')->daily();
More information on task scheduling can be found on the Laravel Task Scheduling docs.
Creating commands
TastyIgniter does not support generation of commands by php artisan make:command
, instead use php artisan create:command Vendor.Extension CommandName
. For example running php artisan create:command Igniter.Cart ClearSessions
will create a command stub in the extensions/igniter/cart/console
folder.
You should then register the command in your extension register()
method to make it available to artisan, e.g. $this->registerConsoleCommand('my.command', \Vendor\Extension\Console\MyCommand::class);