connectholland / tactician-scheduler-plugin
Tactician plugin that allows scheduling a command to be executed at a specific time in the future
Installs: 3 519
Dependents: 0
Suggesters: 0
Security: 0
Stars: 21
Watchers: 4
Forks: 3
Open Issues: 3
Requires
- league/tactician: ^0.6.0 | ^1.0
- ramsey/uuid: ^2.8
- symfony/console: ^2.7
Requires (Dev)
- phpunit/phpunit: ^4.7
- satooshi/php-coveralls: ^0.6.1
- squizlabs/php_codesniffer: ^2.3
This package is auto-updated.
Last update: 2023-05-29 00:58:29 UTC
README
Tactician plugin that allows scheduling a command to be executed at a specific time in the future
Concept
This plugin allows you to create ScheduledCommands that will be executed at a specific time in the future.
Usage
Make sure you put the SchedulerMiddleware in your CommandBus middleware chain:
// create your other middleware $middleware[] = new SchedulerMiddleware(new FileBasedScheduler($pathWhereTheSchedulerMayKeepItsFiles) ); // create your other middleware $commandbus = new CommandBus($middleware);
Let the command you want to schedule extend from AbstractScheduledCommand or implement the ScheduledCommandInterface. Create it and set a execution time:
class SayHappyNewYear extends AbstractScheduledCommand { private $message; public function __construct($message) { $this->message = $message; } public function getMessage() { return $this->message; } } $myScheduledCommand = new SayHappyNewYear('Happy New Year'); $myScheduledCommand->setTimestamp(strtotime('2016-01-01 0:00:00') ); $myCommandBus->handle($myScheduledCommand);
Create a bootstrap file that builds your Commandbus and cron the schedule execution command, for example bootstrap.php
// setup any environment you need // create your other middleware $middleware[] = new SchedulerMiddleware(new FileBasedScheduler($pathWhereTheSchedulerMayKeepItsFiles) ); // create your other middleware $commandbus = new CommandBus($middleware); return $commandbus;
Cron the scheduler at any interval you like (the more it runs, the better you can time your commands), example for once a minute
* * * * * www-data vendor/bin/scheduler scheduler:execute bootstrap.php
Or you can use the daemon command that ships with the package, to schedule an iteration every 10 seconds use:
vendor/bin/scheduler scheduler:daemon bootstrap.php 10
To make it stop after a minute use:
vendor/bin/scheduler scheduler:daemon bootstrap.php 10 6