jc-it / yii2-job-queue
Job Queue implementation.
Installs: 2 547
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 1
Open Issues: 1
Requires
- php: >=8.0
- league/tactician: ^1.1.0
- pda/pheanstalk: ^4.0.3
- yiisoft/yii2: ^2.0
Requires (Dev)
README
This extension provides a package that implements a queue, workers and jobs.
$ composer require jc-it/yii2-job-queue
or add
"jc-it/yii2-job-queue": "<latest version>"
to the require
section of your composer.json
file.
Configuration
- You need to have Beanstalk installed (
sudo apt install beanstalkd
) - Apply configuration:
'container' => [ 'definitions' => [ \League\Tactician\CommandBus::class => function(\yii\di\Container $container) { return new \League\Tactician\CommandBus([ new \League\Tactician\Handler\CommandHandlerMiddleware ( $container->get(\League\Tactician\Handler\CommandNameExtractor\CommandNameExtractor::class), $container->get(\League\Tactician\Handler\Locator\HandlerLocator::class), $container->get(\League\Tactician\Handler\MethodNameInflector\MethodNameInflector::class) ) ]); }, \JCIT\jobqueue\components\ContainerMapLocator::class => function(\yii\di\Container $container) { $result = new \JCIT\jobqueue\components\ContainerMapLocator($container); // Register handlers here // i.e. $result->setHandlerForCommand(\JCIT\jobqueue\jobs\HelloJob::class, \JCIT\jobqueue\jobHandlers\HelloHandler::class); return $result; }, \League\Tactician\Handler\CommandNameExtractor\CommandNameExtractor::class => \League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor::class, \League\Tactician\Handler\Locator\HandlerLocator::class => \JCIT\jobqueue\components\ContainerMapLocator::class, \JCIT\jobqueue\interfaces\JobFactoryInterface::class => \JCIT\jobqueue\factories\JobFactory::class, \JCIT\jobqueue\interfaces\JobQueueInterface::class => \JCIT\jobqueue\components\Beanstalk::class, \League\Tactician\Handler\MethodNameInflector\MethodNameInflector::class => \League\Tactician\Handler\MethodNameInflector\HandleInflector::class, \Pheanstalk\Contract\PheanstalkInterface::class => \JCIT\jobqueue\components\Beanstalk::class, \Pheanstalk\Contract\SocketFactoryInterface::class => function() { return new \Pheanstalk\SocketFactory('localhost', 11300, 10); }, ] ],
- Register Daemon action in controller:
public function actions(): array { return [ 'daemon' => \JCIT\jobqueue\actions\DaemonAction::class, ]; }
- Run action, i.e.
./yii job-queue/daemon
Optional: install daemon as service
Look here to see an example how a console action can be installed as a linux service.
Quick test
- Execute steps in Configuration
- Register
\JCIT\jobqueue\jobs\HelloJob::class
and\JCIT\jobqueue\jobHandlers\HelloHandler::class
in theContainerMapLocator
(as shown in the configuration) - Create
JobQueueController
console controller
class JobQueueController extends \yii\console\Controller { public $defaultAction = 'daemon'; public function actionTest( \JCIT\jobqueue\interfaces\JobQueueInterface $jobQueue ) { $task = \Yii::createObject(\JCIT\jobqueue\jobs\HelloJob::class, ['world']); $jobQueue->putJob($task); } /** * @return array */ public function actions(): array { return [ 'daemon' => [ 'class' => \JCIT\jobqueue\actions\DaemonAction::class, ] ]; } }
- Run in one console
src/yii job-queue
- Run in a second console
src/yii job-queue/test
- The console that runs the daemon will show
Hello world
- NOTE the daemon must be restarted when handlers have changed
Own implementations
- Create Job (that implements
\JCIT\jobqueue\interfaces\JobInterface::class
) which should not do more than carry data - Create JobHandler (that implements
\JCIT\jobqueue\interfaces\JobHandlerInterface::class
) which handles the handling of the job- Injection should be done on construction of the handler
Logging
To extend with an easy ActiveRecord logging of the jobs, look at https://packagist.org/packages/jc-it/yii2-job-queue-logging.
Recurring jobs
To extend with easy ActiveRecord based recurring jobs, look at https://packagist.org/packages/jc-it/yii2-job-queue-recurring.