flexic / scheduler
Provides an scheduler extension.
Requires
- php: ^8.1
- dragonmantank/cron-expression: ^3.3.2
- flexic/cron-builder: ^1.0.0
- psr/log: ^3.0.0
- symfony/console: ^6.1.0
- symfony/event-dispatcher: ^6.1.0
- symfony/options-resolver: ^6.1.0
Requires (Dev)
- ergebnis/php-cs-fixer-config: ^4.9.0
- phpstan/phpstan: ^1.8.8
- phpstan/phpstan-deprecation-rules: ^1.0.0
- phpstan/phpstan-strict-rules: ^1.4.4
README
PHP Schedule is a simple library for scheduling tasks in PHP.
It is inspired by the Laravel Scheduler and Symfony Messenger.
Installation
Run
composer require flexic/scheduler
to install flexic/scheduler
.
Setup Events to schedule
class MyScheduleEvent implements \Flexic\Scheduler\Interfaces\AbstractScheduleEventInterface { public function __invoke(): void { // ... do something } public function configure(Flexic\Scheduler\Interfaces\ScheduleInterface $schedule): void { $schedule->cron('* * * * *'); } }
Schedule events are classes that implement the ScheduleEventInterface
.
Inside the configure
method, you can use the Schedule
object to define when the event should be scheduled to run.
Setup Schedule Worker (Console Command)
Run
php bin/schedule ./path/to/event_config.php ./path/to/event_config_1.php
to start the schedule worker. Worker will automatically load all events from the given config files and run them.
Options
Setup Schedule Worker (own script)
# Options for worker $options = []; $events = [ new MyScheduleEvent(), ]; $worker = new \Flexic\Scheduler\Worker( new Flexic\Scheduler\Configuration\WorkerConfiguration($options), $events, new \Symfony\Component\EventDispatcher\EventDispatcher(), ); $worker->start();
ScheduleEvent Factory
The ScheduleEventInterface
is implemented to allow the usage of a factory to create the event. This is useful if you want to use a dependency injection container to create the event.
class MyScheduleEventFactory implements \Flexic\Scheduler\Interfaces\ScheduleEventFactoryInterface { public function create(): array { return [ new MyScheduleEvent('foo'), new MyScheduleEvent('bar'), ]; } }
Schedule API
cron()
method accepts string, or objects of type CronBuilder
& Cron
of flexic/cron-builder
.
Methods for tokens allows usage of Expressions from flexic/cron-builder
.
Worker API
Worker Lifecycle Events
License
This package is licensed using the GNU License.
Please have a look at LICENSE.md.