https://github.com/patchlevel/worker
Gives the opportunity to build a stable worker that terminates properly when limits are exceeded.
https://github.com/patchlevel/worker
Last synced: 8 months ago
JSON representation
Gives the opportunity to build a stable worker that terminates properly when limits are exceeded.
- Host: GitHub
- URL: https://github.com/patchlevel/worker
- Owner: patchlevel
- License: mit
- Created: 2023-02-28T11:51:35.000Z (almost 3 years ago)
- Default Branch: 1.5.x
- Last Pushed: 2025-04-15T17:06:44.000Z (10 months ago)
- Last Synced: 2025-05-22T02:49:46.136Z (9 months ago)
- Language: PHP
- Homepage:
- Size: 528 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://dashboard.stryker-mutator.io/reports/github.com/patchlevel/hydrator/2.0.x)
[](https://shepherd.dev/github/patchlevel/hydrator)
[](//packagist.org/packages/patchlevel/hydrator)
[](//packagist.org/packages/patchlevel/hydrator)
# 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](https://github.com/patchlevel/event-sourcing) library as a separate library.
## Installation
```bash
composer require patchlevel/worker
```
## Example
```php
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;
}
}
```