来自外部服务的Laravel队列侦听器

时间:2016-08-22 12:56:09

标签: php laravel laravel-5 rabbitmq

我有一个用Laravel编写的遗留API,我正在尝试从RabbitMQ队列发布数据。我正在使用第三方库将RabbitMQ集成到本机Laravel Queue架构中。哪个工作正常,并且在侦听器控制台命令运行时正确处理消息。

但是,当我传递以下事件时,包括队列处理程序所需的作业和数据键......

{"job":"SixtyFiveContrib\\Jobs\\CreateSubmissionMetaDataJob@handle","data":{"title":"","story":"Testing 123","author":"Ewan Valentine","email":"","address":"","dob":"","phone":"","site":"The Lad Bible","legal":"The LAD bible - Facebook","agreed":false,"signature":"","files":[{"filename":"test.png","filesize":0,"filetype":""}],"agreement":{"id":"","facebook_id":""},"attempts":1036}}

我注意到处理排队事件的类,拆分类路径和方法名称(SixtyFiveContrib \ Jobs \ CreateSubmissionMetaDataJob和handle),并尝试从容器中调用该类。

但是,作业不会自动成为容器的一部分。当我尝试将作业自己添加到容器中时,我遇到了无法将队列中的“数据”动态传递到作业中的情况,例如......

$this->app->bind(\SixtyFiveContrib\Jobs\CreateSubmissionMetaDataJob::class, function() {
    return \SixtyFiveContrib\Jobs\CreateSubmissionMetaDataJob(????);
});

如果我从容器中省略此条目,则队列处理程序无法再解析作业,我收到以下错误...

  

[照亮\合同\容器\ BindingResolutionException]
  无法解析的依赖项解析[参数#0 [$ data]]   在类SixtyFiveContrib \ Jobs \ CreateSubmissionMetaDataJob

这是我的工作类(省略敏感业务逻辑)。

<?php

namespace SixtyFiveContrib\Jobs;

use App;

use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;

use SixtyFiveContrib\Jobs\Job;

/**
 * CreateSubmissionsMetaDataJob
 *
 * @author    Ewan Valentine <ewan@theladbible.com>
 * @copyright The LAD bible Group - 2016
 */
class CreateSubmissionMetaDataJob extends Job implements SelfHandling 
{
    use InteractsWithQueue, SerializesModels;

    /**
     * @var array $data
     */
    public $data;

    /**
     * __construct
     *
     * @param array $this->data
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    public function handle() 
    {
        try {

            // Omitted 
        } catch (\Exception $e) {
            \Log::error($e->getMessage() . " " . $e->getLine());
            return $e->getMessage(); 
        }
    }
}

1 个答案:

答案 0 :(得分:1)

好的,我明白了!您必须将数据作为第二个参数传递给handlefire方法,第一个参数是应用程序实例。

<?php

namespace SixtyFiveContrib\Jobs;

use App;

use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;

use SixtyFiveContrib\Jobs\Job;

/**
 * CreateSubmissionsMetaDataJob
 *
 * @author    Ewan Valentine <ewan@theladbible.com>
 * @copyright The LAD bible Group - 2016
 */
class CreateSubmissionMetaDataJob extends Job implements SelfHandling 
{
    use InteractsWithQueue, SerializesModels;

    /**
     * @var array $data
     */
    public $data;

    /**
     * __construct
     */
    public function __construct()
    {
    }

    public function handle($app, array $data) 
    {
        try {

            // Omitted 
        } catch (\Exception $e) {
            \Log::error($e->getMessage() . " " . $e->getLine());
            return $e->getMessage(); 
        }
    }
}