doumi1852 2014-09-21 21:07
浏览 65
已采纳

有没有办法将回调作为数据参数传递给Laravel 4.2 Queue :: push()

I've got some time-consuming code that processes the results of a series of HTTP requests that I'd like to run in the background. I'm using a Redis store for managing the queue. Here's what I've tried:

Queue::push( 'FetchUrls', [
    'urls'     => [ 'http://one.com', 'http://two.com', 'http://three.com' ],
    'complete' => function( $response ) { /* process data returned by URL here */ },
    'error'    => function( $error    ) { /* process HTTP errors here */ },
]);

What shows up in the Redis queue store is a JSON serialization of the $data parameter:

{
    "job": "FetchUrls",
    "data": { 
        "urls": [
            "http:\/\/one.com",
            "http:\/\/two.com",
            "http:\/\/three.com"
        ],
        "complete": [],
        "error": []
    },
    "id": "aAlkNM0ySLXcczlLYho19TlWYs9hStzl",
    "attempts": 1
}

As you can see, the callbacks just show up as empty arrays in the queue store. I've never used the Queue class before and so I may be approaching this problem in the wrong way. I'm looking for a suggestion for the best way to work around this. Thanks!

  • 写回答

2条回答 默认 最新

  • douxuanpa8298 2014-09-22 04:31
    关注

    You could pass function names and invoke them with something like call_user_func().

    Queue::push('FetchUrls', [
        'urls'     => ['http://one.com', 'http://two.com', 'http://three.com'],
        'complete' => ['ResponseHandler', 'fetchComplete'],
        'error'    => ['ResponseHandler', 'fetchError'],
    ]);
    
    class FetchUrls
    {
        public function fire($job, $data)
        {
            list($urls, $complete, $error) = $data;
    
            foreach ($urls as $url) {
                if ($response = $this->fetch($url)) {
                    $job->delete();
                    call_user_func($complete, $response);
                } else {
                    $job->release();
                    call_user_func($error, $this->getError());
                }
            }
        }
    
        private function fetch($url)
        {
            // ...
        }
    
        private function getError()
        {
            // ...
        }
    }
    
    class ResponseHandler
    {
        public static function fetchComplete($response)
        {
            // ...
        }
    
        public static function fetchError($error)
        {
            // ...
        }
    }
    

    There's a not-class-based version of this approach, but this is relatively clean.

    call_user_func() with ['ResponseHandler', 'fetchComplete'] as the first parameter will invoke ResponseHandler::fetchComplete().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效