buzzingpixel / php-scheduler
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
这是一个相当简单的PHP调度系统,可以添加到几乎任何利用PSR-11容器的PHP应用中。
驱动程序
目前,此包提供的唯一驱动程序是Redis驱动程序。但如果您想提供自己的驱动程序,只需实现\BuzzingPixel\Scheduler\ScheduleHandler
接口,并将您的容器连接到在请求接口时提供该接口。
否则,将\BuzzingPixel\Scheduler\ScheduleHandler
连接到提供\BuzzingPixel\Scheduler\RedisDriver\RedisScheduleHandler
。
PHP的\Redis
和Symfony的\Symfony\Component\Cache\Adapter\RedisAdapter
也需要通过构造函数/容器可用。
用法
调度工厂
您需要实现\BuzzingPixel\Scheduler\ScheduleFactory
,并通过您的容器使其可用,并配置以发送您想要的任何计划项。
示例
<?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', ], ), ]); } }
午夜字符串
如果您使用频率午夜字符串,您可能希望能够控制时区。由于系统时区可能不可靠,并且建议将其设置为UTC,因此此包不依赖于系统的时区。您可以做的替代方法是,通过容器发送您自己的新实例\BuzzingPixel\Scheduler\SchedulerTimeZone
。
以下是一个示例
$containerBindings->addBinding( \BuzzingPixel\Scheduler\SchedulerTimeZone::class, static fn () => new \BuzzingPixel\Scheduler\SchedulerTimeZone( new \DateTimeZone('US/Central'), ), );
运行计划
一旦您的配置就绪,并且您已经设置了一些计划任务来运行,您需要设置每分钟调用\BuzzingPixel\Scheduler\ScheduleHandler::runSchedule
的东西。您可以使用cron、设置Docker镜像或适合您环境的任何方法。
运行计划的命令
此包提供了一个Symfony控制台命令,如果您使用Symfony Console(或Silly,这是我首选的),则可以通过容器加载\BuzzingPixel\Scheduler\Framework\RunScheduleSymfonyCommand
,并将其添加到您的Symfony控制台应用程序中。
然后在cron或Docker设置中,每分钟通过CLI应用程序运行命令buzzingpixel-schedule:run
(除非您已更改命令名称)。
更改命令名称
如果您想更改命令名称,可以通过RunScheduleSymfonyCommand
类的name
构造函数参数来这样做。配置您的DI以提供您首选的命令名称。