buzzingpixel / php-scheduler
Installs: 1 232
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Requires
- php: ^8.2
- lcobucci/clock: ^3.1
- psr/clock: ^1.0
- spatie/php-cloneable: ^1.0
- symfony/cache: ^6.2
Requires (Dev)
- doctrine/coding-standard: ^11.0.0
- mnapoli/silly: ^1.8
- phpstan/phpstan: ^1.10
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-strict-rules: ^1.5
- squizlabs/php_codesniffer: ^3.6
- symfony/console: ^6.2
- symfony/var-dumper: ^6.2
Suggests
- ext-redis: Required to use the Redis driver
- mnapoli/silly: If you're using the Silly Console app, a buzzingpixel-schedule:run command is supplied
- symfony/console: If you're using Symfony Console, a buzzingpixel-schedule:run command is supplied
README
A fairly simple php scheduling system you can add to nearly any PHP app that utilizes a PSR-11 container.
Drivers
At the moment, the only driver supplied with this package is the Redis driver. But if you want to supply your own driver, just implement the \BuzzingPixel\Scheduler\ScheduleHandler
interface and wire your container to supply that when the interface is requested.
Otherwise, wire \BuzzingPixel\Scheduler\ScheduleHandler
to supply the \BuzzingPixel\Scheduler\RedisDriver\RedisScheduleHandler
.
PHP's \Redis
and the Symfony \Symfony\Component\Cache\Adapter\RedisAdapter
will need to also be available through the constructor/container.
Usage
ScheduleFactory
You will need to implement the \BuzzingPixel\Scheduler\ScheduleFactory
and make it available through your container and configure to send whatever scheduled items you want.
Example:
<?php declare(strict_types=1); use App\SomeScheduledClass; use BuzzingPixel\Scheduler\Frequency; use BuzzingPixel\Scheduler\ScheduleItem; use BuzzingPixel\Scheduler\ScheduleItemCollection; class ScheduleFactory implements \BuzzingPixel\Scheduler\ScheduleFactory { public function createSchedule(): ScheduleItemCollection { return new ScheduleItemCollection([ new ScheduleItem( Frequency::FIVE_MINUTES, SomeScheduledClass::class, // Optionally provide a method, otherwise it will default to __invoke 'myMethod', // Optionally send a context array that will be passed as the first argument to your method [ 'foo' => 'bar', ], ), ]); } }
Midnight Strings
If you utilize the Frequency midnight strings, you might want to be able to control the timezone. Since system timezone can be unreliable and/or it's recommended practice to set that to UTC, this package does not rely on the system's timezone. What you can do instead is send your own newed up instance of \BuzzingPixel\Scheduler\SchedulerTimeZone
through the container.
Here's an example:
$containerBindings->addBinding( \BuzzingPixel\Scheduler\SchedulerTimeZone::class, static fn () => new \BuzzingPixel\Scheduler\SchedulerTimeZone( new \DateTimeZone('US/Central'), ), );
Running the schedule
Once your configuration is in place and you've set up some scheduled jobs to run, you need to set up something to call \BuzzingPixel\Scheduler\ScheduleHandler::runSchedule
every minute. You can use a cron, or set up a Docker image or whatever works best in your environment.
Command to run schedule
This package provides a Symfony console command which you can use if you're using Symfony Console (or Silly, which is my preference). Load up \BuzzingPixel\Scheduler\Framework\RunScheduleSymfonyCommand
through your container, and add it to your Symfony console app.
Then in your cron or Docker setup run the command buzzingpixel-schedule:run
(unless you've changed the command name) every minute through your CLI app.
Changing the command name
If you'd like to change the command name, you can do so through the name
constructor parameter of the RunScheduleSymfonyCommand
class. Configure your DI to provide the command name you would prefer.