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

时间:2014-09-21 21:07:44

标签: php laravel laravel-4

我有一些耗时的代码来处理我想在后台运行的一系列HTTP请求的结果。我使用Redis商店来管理队列。以下是我尝试过的内容:

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 */ },
]);

Redis队列存储中显示的是JSON参数的$data序列化:

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

如您所见,回调只是在队列存储中显示为空数组。我之前从未使用过Queue类,所以我可能会以错误的方式处理这个问题。我正在寻找一个解决此问题的最佳方法的建议。谢谢!

2 个答案:

答案 0 :(得分:3)

为安全起见,您应该只推送数组(因为序列化问题) 要回答您的问题 - 没有解决方法,您应该重新考虑逻辑。

答案 1 :(得分:2)

您可以传递函数名称并使用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)
    {
        // ...
    }
}

这种方法有一个基于非类的版本,但这个相对干净。

call_user_func()作为第一个参数的

['ResponseHandler', 'fetchComplete']将调用ResponseHandler::fetchComplete()