xactsystems/command-scheduler

A Symfony command scheduler.

v4.4.3 2021-08-24 11:36 UTC

This package is auto-updated.

Last update: 2021-09-24 11:58:52 UTC


README

This bundle allows you to provide job scheduling via a list of jobs configured within a DBAL store.

Jobs can be created for once-only jobs as well as repeating jobs based on a cron expression. The bundle features an admin interface for management of the scheduled commands as well as via the CommandScheduler class.

Documentation

1) Add XactCommandScheduler to your project

composer require xactsystems/command-scheduler

2) Create the scheduler table

php bin/console doctrine:schema:create

3) Add the routes for the scheduler admin views

config/routes.yaml

command_scheduler:
    resource: "@XactCommandSchedulerBundle/Resources/config/routing.yaml"

4) Use the admin views

Browse to http://my-project/command-scheduler/list

5) Run the command scheduler

php bin/console xact:command-scheduler

The command accepts the following options:

  • --max-runtime=nnn Sets the maximum length in seconds the scheduler will run for. 0 (default) runs forever.
  • --idle-time=nnn Sets the number of seconds the scheduler sleeps for when the command queue is empty. Defaults to 5.

Manage the scheduler via code

Add a scheduled command

use Xact\CommandScheduler\Scheduler\CommandScheduler;
use Xact\CommandScheduler\Entity\ScheduledCommand;
use Cron\CronExpression;
...

public function myControllerAction(CommandScheduler $scheduler)
{
    $scheduledCommand = new ScheduledCommand();
    $scheduledCommand->setDescription('My daily command');
    $scheduledCommand->setCommand( 'app:my-daily-command' );
    $scheduledCommand->setCronExpression( CronExpression::factory('@daily') );
    $scheduledCommand->setPriority(5);  // Defaults to 1
    $scheduler->add( $scheduledCommand );
}

Disable a command

use Xact\CommandScheduler\Scheduler\CommandScheduler;
use Xact\CommandScheduler\Entity\ScheduledCommand;
...

public function myControllerAction(int $commandId, CommandScheduler $scheduler)
{
    $scheduler->disable($commandId);
}

Run a command immediately

use Xact\CommandScheduler\Scheduler\CommandScheduler;
use Xact\CommandScheduler\Entity\ScheduledCommand;
...

public function myControllerAction(int $commandId, CommandScheduler $scheduler)
{
    $scheduler->runImmediately($commandId);
}

Delete a command

use Xact\CommandScheduler\Scheduler\CommandScheduler;
use Xact\CommandScheduler\Entity\ScheduledCommand;
...

public function myControllerAction(int $commandId, CommandScheduler $scheduler)
{
    $scheduler->delete($commandId);
}

Get a list of active commands

use Xact\CommandScheduler\Scheduler\CommandScheduler;
use Xact\CommandScheduler\Entity\ScheduledCommand;
...

public function myControllerAction(CommandScheduler $scheduler)
{
    $commands = $scheduler->getActive();
    foreach ($commands as $command) {
        ...
    }
}

Cron notes

The bundle uses dragonmantank/cron-expression CronExpression class to determine run times and you can use the non-standard pre-defined scheduling definitions. See Cron Format for more details.

Credits

License

This bundle is released under the MIT license. See the complete license in the bundle:

LICENSE