douyi9447 2016-08-22 12:56
浏览 105

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

I have a legacy API written in Laravel, and I'm attempting to post data to it from a RabbitMQ queue. I'm using a third party library to integrate RabbitMQ into the native Laravel Queue architecture. Which works well, and is handling messages correctly when the listener console command is running.

However, when I pass the following event, including the job and data keys, which are required by the queue handler...

{"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}}

I notice the class which handles queued events, splits the class path and the method name (SixtyFiveContrib\Jobs\CreateSubmissionMetaDataJob and handle), and attempts to call that class from the container.

However jobs aren't automatically part of the container. When I attempt to add the job into the container myself, I run into a situation where I can't dynamically pass the 'data' from the queue into the job, for example...

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

If I omit this entry from the container, the queue handler can no longer resolve the job, I get the following error...

[Illuminate\Contracts\Container\BindingResolutionException]
Unresolvable dependency resolving [Parameter #0 [ $data ]] in class SixtyFiveContrib\Jobs\CreateSubmissionMetaDataJob

Here's my job class (sensitive business logic omitted).

<?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条回答 默认 最新

  • 普通网友 2016-08-22 14:37
    关注

    Okay, I figured it out! You have to pass your data as a second param to your handle or fire method, first argument is the application instance.

    <?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(); 
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮