补丁级别 / 工作者
提供构建稳定工作者的机会,当超过限制时能够正确终止。
1.2.0
2024-03-12 11:15 UTC
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.0
- symfony/event-dispatcher: ^5.4.26|^6.4.0|^7.0.0
Requires (Dev)
- cspray/phinal: ^2.0.0
- infection/infection: ^0.27.8
- patchlevel/coding-standard: ^1.3.0
- phpbench/phpbench: ^1.2.15
- phpspec/prophecy-phpunit: ^2.1.0
- phpstan/phpstan: ^1.10.49
- phpunit/phpunit: ^10.5.2
- psalm/plugin-phpunit: ^0.18.4
- roave/infection-static-analysis-plugin: ^1.34.0
- symfony/var-dumper: ^5.4.29|^6.4.0|^7.0.0
- vimeo/psalm: ^5.17.0
This package is auto-updated.
Last update: 2024-09-23 04:24:54 UTC
README
工作者
提供构建稳定工作者的机会,当超过限制时能够正确终止。现在它已经被事件源库作为单独的库外包。
安装
composer require patchlevel/worker
示例
<?php declare(strict_types=1); namespace App\Console\Command; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Logger\ConsoleLogger; use Symfony\Component\Console\Output\OutputInterface; #[AsCommand( 'app:worker', 'do stuff' )] final class WorkerCommand extends Command { protected function configure(): void { $this ->addOption( 'run-limit', null, InputOption::VALUE_OPTIONAL, 'The maximum number of runs this command should execute', 1 ) ->addOption( 'memory-limit', null, InputOption::VALUE_REQUIRED, 'How much memory consumption should the worker be terminated (500MB, 1GB, etc.)' ) ->addOption( 'time-limit', null, InputOption::VALUE_REQUIRED, 'What is the maximum time the worker can run in seconds' ) ->addOption( 'sleep', null, InputOption::VALUE_REQUIRED, 'How much time should elapse before the next job is executed in milliseconds', 1000 ); } protected function execute(InputInterface $input, OutputInterface $output): int { $logger = new ConsoleLogger($output); $worker = DefaultWorker::create( function ($stop): void { // do something if (/* some condition */) { $stop(); } }, [ 'runLimit' => $input->getOption('run-limit'), 'memoryLimit' => $input->getOption('memory-limit'), 'timeLimit' => $input->getOption('time-limit'), ], $logger ); $worker->run($input->getOption('sleep') ?: null); return 0; } }