patchlevel / worker
Gives the opportunity to build a stable worker that terminates properly when limits are exceeded.
Installs: 79 982
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 0
Open Issues: 3
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-11-07 05:07:19 UTC
README
Worker
Gives the opportunity to build a stable worker that terminates properly when limits are exceeded. It has now been outsourced by the event-sourcing library as a separate library.
Installation
composer require patchlevel/worker
Example
<?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; } }