mate/queue-bundle

基于 Pheanstalk 的 Symfony 框架的队列(生产者 + 消费者)系统

安装: 19

依赖者: 0

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

dev-master 2018-03-12 11:25 UTC

This package is not auto-updated.

Last update: 2024-09-18 09:09:30 UTC


README

基于 Pheanstalk 的 Symfony 框架的队列(生产者 + 消费者)系统

需求

安装

步骤 1: 下载 Bundle

打开命令行,进入您的项目目录并执行以下命令以下载此 Bundle 的最新版本

$ composer require mate/queue-bundle dev-master

步骤 2: 启用 Bundle

然后,通过将其添加到项目中 app/AppKernel.php 文件中注册的 Bundle 列表来启用 Bundle

<?php
// app/AppKernel.php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Mate\QueueBundle\MateQueueBundle(),
        );
    }
}

步骤 3: 添加覆盖参数

将您的 pheanstalk 服务器主机参数添加到 app/config/parameters.yml 文件中

# app/config/parameters.yml
mate_worker_host: 127.0.0.1

入门

此 Bundle 使得创建执行时间较长的作业/任务变得简单。让我们想象有一个应用程序,它会向用户发送确认邮件。

创建作业 (ConfirmationMailJob.php)

让我们在 AppBundle\Job 命名空间中创建一个名为 ConfirmationMailJob.php 的类

namespace AppBundle\Job;

use Mate\QueueBundle\Worker\Job;
use Symfony\Component\Security\Core\User\User;

class ConfirmationMailJob extends Job
{
    /** @var User */
    protected $user;

    /** @var \Swift_Mailer */
    protected $mailerService;

    /**
     * ConfirmationMailJob constructor.
     *
     * Dependencies:
     * 
     * @param User $user
     * @param \Swift_Mailer $mailerService
     */
    public function __construct( User $user, \Swift_Mailer $mailerService )
    {
       $this->user          = $user;
       $this->mailerService = $mailerService;
    }


    /**
     * Method used to execute this job
     * by our Queue worker later
     */
    public function handle()
    {
        //
    }
}

我们的 ConfirmationMailJob 类应该扩展此 Bundle 的 Job(抽象)类,并需要实现 handle() 方法。

现在让我们在 handle() 方法中编写一些基本代码来向认证用户发送邮件。

/**
 * Method used to execute this job
 * by our Queue worker later
 */
public function handle()
{
    $message = (new \Swift_Message('Hello Email'))
        ->setFrom('send@example.com')
        ->setTo($this->user->getEmail())
        ->setBody('Confirmation mail here', 'text/plain')
    ;

    $this->mailerService->send($message);
}

这就完成了 :), 最后我们在这里要做的就是调用控制器中的 Producer 服务并传递这个作业给它的 produce() 方法

/**
 * @param Producer $producer
 * @param \Swift_Mailer $mailerService
 *
 * @return Response
 *
 * @Route("/", name="homepage")
 * @Security("has_role('ROLE_USER')")
 *
 * @throws \Exception
 */
public function indexAction( Producer $producer, \Swift_Mailer $mailerService ): Response
{
    /** @var User $user */
    $user = $this->getUser();

    // Produce the job to our Pheanstalk server
    // And injecting the desired services
    $producer->produce(new ConfirmationMailJob($user, $mailerService), 3);

    return $this->render('default/index.html.twig');
}

这就完成了 :), 你已经完成了。请注意,produce(Job $job, $delay = 0, $timeToRun = 60) 方法接受以下参数

  1. 作业类
  2. 延迟(秒):默认为 0 - 无延迟
  3. 运行时间(秒):默认为 60

运行队列工作进程

最后,我们需要监听应用程序中执行的任务,为此您需要运行以下命令

$ php bin/console mate:queue:work

现在尝试运行您的应用程序服务器,并导航到 https://:8000/,查看您的控制台

[2017-10-08 23:31:30] Processing: AppBundle\Job\ConfirmationMailJob
[2017-10-08 23:31:30] Processed : AppBundle\Job\ConfirmationMailJob

确保找到一些工具,如 Supervisor,以便在 mate:queue:work 进程失败时自动重启它。

高级主题

队列事件

此包提供一些事件来帮助您管理作业(Mate\QueueBundle\Event\JobEvent

  • MATE_QUEUE_JOB_INITIALIZED(onInitialized)
  • MATE_QUEUE_JOB_EXECUTED(onExecuted)
  • MATE_QUEUE_JOB_FAILED(onFailed)
  • MATE_QUEUE_JOB_DELETED(onDeleted)

// TODO