xactsystems / command-scheduler
一个 Symfony 命令调度器。
v5.0.6
2024-09-20 16:28 UTC
Requires
- php: >=8.1
- doctrine/doctrine-bundle: ^2.12
- doctrine/orm: ^3.2
- dragonmantank/cron-expression: ^3.3
- symfony/asset: ^6.4||^7.1
- symfony/form: ^6.4||^7.1
- symfony/process: ^6.4||^7.1
- symfony/twig-bundle: ^6.4||^7.1
- symfony/yaml: *
- williarin/cook: ^1.3
- xactsystems/cast: ^1.0
Requires (Dev)
- phpstan/extension-installer: ^1.4
- phpstan/phpdoc-parser: ^1.29
- phpstan/phpstan: ^1.11
- phpstan/phpstan-deprecation-rules: ^1.2
- phpstan/phpstan-doctrine: ^1.5
- phpstan/phpstan-symfony: ^1.4
- slevomat/coding-standard: ^8.15
- squizlabs/php_codesniffer: ^3.10
- symfony/browser-kit: ^6.4||^7.1
- symfony/css-selector: ^6.4||^7.1
- symfony/debug-bundle: ^7.1
- symfony/monolog-bundle: ^3.0
- symfony/phpunit-bridge: ^7.1
- symfony/stopwatch: ^7.1
- symfony/web-profiler-bundle: ^7.1
- dev-master
- 5.x-dev
- v5.0.6
- v5.0.5
- v5.0.3
- v5.0.2
- v5.0.1
- v5.0.0
- 4.5.x-dev
- v4.5.3
- 4.5.2
- 4.5.0
- 4.4.x-dev
- 4.4.7
- 4.4.6
- 4.4.5
- v4.4.3
- v4.4.2
- 4.4.1
- v4.4.0
- 3.x-dev
- 3.6.5
- 3.6.3
- v3.6.2
- 3.6.1
- v3.5.1
- 3.4.x-dev
- 3.4.22
- 3.4.20
- 3.4.19
- 3.4.18
- 3.4.17
- v3.4.14
- 3.4.13
- v3.4.12
- v3.4.11
- v3.4.10
- v3.4.9
- v3.4.8
- v3.4.7
- v3.4.6
- v3.4.5
- 3.4.4
- v3.4.3
- v3.4.2
- 3.4.1.x-dev
- 3.4.1
- v3.4.0
This package is auto-updated.
Last update: 2024-09-20 16:35:30 UTC
README
此组件允许您通过配置在 DBAL 存储中的作业列表来提供作业调度。
可以创建一次性的作业以及基于 cron 表达式的重复作业。组件具有管理预定命令的管理员界面,以及通过 CommandScheduler 类。
文档
1) 将 XactCommandScheduler 添加到您的项目中
composer require xactsystems/command-scheduler
2) 创建调度器表
php bin/console doctrine:schema:create
3) 添加调度器管理员视图的路由
config/routes.yaml
command_scheduler: resource: "@XactCommandSchedulerBundle/Resources/config/routing.yaml"
4) 使用管理员视图
浏览到 http://my-project/command-scheduler/list
5) 运行命令调度器
php bin/console xact:command-scheduler
该命令接受以下选项
--max-runtime=nnn
设置调度器运行的秒数。0(默认)运行无限期。--idle-time=nnn
设置当命令队列空时调度器休眠的秒数。默认为5。
通过代码管理调度器
添加预定命令
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 ); }
禁用命令
use Xact\CommandScheduler\Scheduler\CommandScheduler; use Xact\CommandScheduler\Entity\ScheduledCommand; ... public function myControllerAction(int $commandId, CommandScheduler $scheduler) { $scheduler->disable($commandId); }
立即运行命令
use Xact\CommandScheduler\Scheduler\CommandScheduler; use Xact\CommandScheduler\Entity\ScheduledCommand; ... public function myControllerAction(int $commandId, CommandScheduler $scheduler) { $scheduler->runImmediately($commandId); }
删除命令
use Xact\CommandScheduler\Scheduler\CommandScheduler; use Xact\CommandScheduler\Entity\ScheduledCommand; ... public function myControllerAction(int $commandId, CommandScheduler $scheduler) { $scheduler->delete($commandId); }
获取活动命令列表
use Xact\CommandScheduler\Scheduler\CommandScheduler; use Xact\CommandScheduler\Entity\ScheduledCommand; ... public function myControllerAction(CommandScheduler $scheduler) { $commands = $scheduler->getActive(); foreach ($commands as $command) { ... } }
通过 CommandSchedulerFactory 创建命令
当使用工厂时,您可以设置以下命令设置的配置值
#config/bundles/xact_command_scheduler.yml xact_command_scheduler: clear_data: ~ # Defaults to true retry_on_fail: ~ # Defaults to false retry_delay: ~ # Defaults to 60 retry_max_attempts: ~ # Defaults to 60
然后您可以使用工厂方法创建您的预定命令。上述配置参数将应用于通过工厂方法创建的命令,除非在方法调用中覆盖。
use Xact\CommandScheduler\CommandSchedulerFactory; class MyController extends AbstractController { private CommandSchedulerFactory $commandFactory; public function __controller(CommandSchedulerFactory $commandFactory) { $this->commandFactory = $commandFactory; } public function myEmailAction(EntityManagerInterface $em) { $myLargeDataObject = 'Some enormous serialized value you want to store in the command and probably delete once the command is complete.'; $command = $this->commandFactory->createImmediateCommand( 'My test command', 'app:test-command', null, $myLargeDataObject, ); $em->persist($command); $em->flush(); } }
工厂方法
/** * Create a command to run immediately * * @param string[]|null $arguments * @param bool|null $clearData If null, the configuration value 'clear_data' is used. Default true. * @param bool|null $retryOnFail If null, the configuration value 'retry_on_fail' is used. Default false. * @param int|null $retryDelay If null, the configuration value 'retry_delay' is used. Default 60. * @param int|null $retryMaxAttempts If null, the configuration value 'retry_max_attempts' is used. Default 60. */ public function createImmediateCommand( string $description, string $command, ?array $arguments = null, ?string $data = null, ?bool $clearData = null, ?bool $retryOnFail = null, ?int $retryDelay = null, ?int $retryMaxAttempts = null ): ScheduledCommand {} /** * Create a command scheduled by a CRON expression * * @param string[]|null $arguments * @param bool|null $clearData If null, the configuration value 'clear_data' is used. Default true. * @param bool|null $retryOnFail If null, the configuration value 'retry_on_fail' is used. Default false. * @param int|null $retryDelay If null, the configuration value 'retry_delay' is used. Default 60. * @param int|null $retryMaxAttempts If null, the configuration value 'retry_max_attempts' is used. Default 60. */ public function createCronCommand( string $cronExpression, string $description, string $command, ?array $arguments = null, ?string $data = null, ?bool $clearData = null, ?bool $retryOnFail = null, ?int $retryDelay = null, ?int $retryMaxAttempts = null ): ScheduledCommand {}
cron 备注
该组件使用 dragonmantank/cron-expression CronExpression 类来确定运行时间,并且您可以使用非标准的预定义调度定义。有关更多详细信息,请参阅 Cron 格式。
致谢
- Ian Foulds 作为此包的创建者。
- Piotr Nowak (https://github.com/noofaq) 在寻找 jms/job-queue-bundle 的替代品时受到启发 - schmittjoh/JMSJobQueueBundle#208 (评论)。
- Julien Guyon (https://github.com/j-guyon) 在他的命令调度器组件中受到启发。
许可证
此组件在 MIT 许可下发布。请参阅组件中的完整许可证。