happy-inc / worker
Happy Inc Worker
Installs: 11 845
Dependents: 0
Suggesters: 0
Security: 0
Stars: 25
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- ext-pcntl: *
- psr/log: ^1.0|^2.0|^3.0
Requires (Dev)
- ext-posix: *
- friendsofphp/php-cs-fixer: ^3.4.0
- icanhazstring/composer-unused: ^0.7.12
- maglnet/composer-require-checker: ^4.0.0
- phpunit/phpunit: ^9.5.11
- psalm/plugin-phpunit: ~0.16
- vimeo/psalm: ^4.18.1
README
use HappyInc\Worker\MemoryLimit\MemoryLimit; use HappyInc\Worker\MemoryLimit\MemoryLimitExtension; use HappyInc\Worker\ProcessSignal\ProcessSignalExtension; use HappyInc\Worker\Signaller\FileSignaller; use HappyInc\Worker\Signaller\SignallerExtension; use HappyInc\Worker\Sleep\SleepExtension; use HappyInc\Worker\Sleep\SleepInterval; use HappyInc\Worker\Worker; use Psr\Log\LogLevel; $signaller = new FileSignaller('/some/dir'); $worker = new Worker([ new MemoryLimitExtension( MemoryLimit::fromIniMemoryLimit(0.7), // stop when allocated memory reaches 70% of php.ini memory_limit $systemLogger, LogLevel::CRITICAL, // optionally log when memory limit is reached with the specified level ), new ProcessSignalExtension([SIGINT]), // gracefully stop the worker when Ctrl+C is pressed in the terminal new SignallerExtension('mailing_worker', $signaller), // allows to send a stop signal from a different process new SleepExtension(SleepInterval::fromSeconds(1)), // sleep 1 second after each job ]); $worker->workOn(function (): void { // some job }); $signaller->sendSignal('mailing_worker'); // stop all workers, listening to the "mailing_worker" channel via the SignallerExtension