承接 jc-it/yii2-job-queue 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jc-it/yii2-job-queue

Composer 安装命令:

composer require jc-it/yii2-job-queue

包简介

Job Queue implementation.

README 文档

README

Scrutinizer Code Quality Code Coverage Build Status

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 the ContainerMapLocator (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.

Credits

统计信息

  • 总下载量: 2.76k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-04-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固