patchlevel / worker
Gives the opportunity to build a stable worker that terminates properly when limits are exceeded.
Installs: 110 641
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 0
Open Issues: 2
Requires
- php: ~8.2.0 || ~8.3.0 || ~8.4.0
- symfony/event-dispatcher: ^5.4.26|^6.4.0|^7.0.0
Requires (Dev)
- infection/infection: ^0.29.10
- patchlevel/coding-standard: ^1.3.0
- phpbench/phpbench: ^1.2.15
- phpspec/prophecy-phpunit: ^2.1.0
- phpstan/phpstan: ^2.1.2
- phpunit/phpunit: ^11.5.3
- psalm/plugin-phpunit: ^0.19.0
- roave/infection-static-analysis-plugin: ^1.34.0
- symfony/var-dumper: ^5.4.29|^6.4.0|^7.0.0
- vimeo/psalm: ^6.0.0
- 1.4.x-dev
- 1.3.x-dev
- 1.3.0
- 1.2.x-dev
- 1.2.0
- 1.1.x-dev
- 1.1.1
- 1.1.0
- 1.0.x-dev
- 1.0.0
- 1.0.0-beta2
- 1.0.0-beta1
- dev-renovate/php-8.x
- dev-renovate/lock-file-maintenance
- dev-php84
- dev-renovate/vimeo-psalm-6.x
- dev-renovate/phpunit-phpunit-11.x
- dev-renovate/phpstan-phpstan-2.x
- dev-renovate/infection-infection-0.x
This package is auto-updated.
Last update: 2025-01-31 05:14:26 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; } }