我想知道如何处理这件事。我有一个web钩子端点,它响应来自Github的web钩子调用。
它开始了一个长时间运行的过程,在其中它克隆了进行web钩子调用的存储库。
/**
* The webhook endpoint.
*
* @param Request $request
* @return mixed
* @throws \Exception
*/
public function webhook(Request $request)
{
// The type of GitHub event that we receive.
$event = $request->header('X-GitHub-Event');
$url = $this->createCloneUrl();
$this->cloneGitRepo($url);
return new Response('Webhook received succesfully', 200);
}这方面的问题是,当响应提供得不够快时,Github会给出一个错误。
我们无法交付这个有效载荷:服务超时
这是正确的,因为我的cloneGitRepo方法简单地阻止了响应的执行,而且花费的时间太长了。
我如何才能仍然提供一个回复,以确认已经成功地进行了web钩子调用,并开始了我漫长的运行过程?
我和雷迪斯一起用拉拉维尔做这一切,也许可以在那里取得一些成就?我愿意接受所有的建议。
发布于 2017-10-26 17:03:15
你要找的是一份排队的工作。Laravel使Laravel队列变得非常容易。
使用队列,您可以设置一个队列驱动程序(数据库、redis、Amazon等),然后就有一个到多个队列工作人员在连续运行。当您从webhook方法将作业放到队列中时,它将由您的一个队列工作人员获取,并在一个单独的进程中运行。但是,将排队作业分派到队列的动作非常快,因此在队列工作人员完成实际工作时,webhook方法将快速返回。
链接文档包含所有细节,但一般流程如下:
php artisan make:job CloneGitRepo创建CloneGitRepo作业类。- It should implement the `Illuminate\Contracts\Queue\ShouldQueue` interface so that Laravel knows to send this job to a queue when it is dispatched.
- Make sure to define properties on the class for any data you pass into the constructor. This is necessary so the worker can rebuild the job correctly when it is pulled off the queue.
- The queue worker will call the `handle()` method to process the job. Any dependencies can be type hinted here and they will be injected from the IoC container.
dispatch()助手函数,也可以对作业本身调用静态dispatch()方法。- `dispatch(new CloneGitRepo($url));`
- `CloneGitRepo::dispatch($url);`
所以,你的网钩看起来应该是:
public function webhook(Request $request)
{
// The type of GitHub event that we receive.
$event = $request->header('X-GitHub-Event');
$url = $this->createCloneUrl();
CloneGitRepo::dispatch($url);
return new Response('Webhook received succesfully', 200);
}https://stackoverflow.com/questions/46943006
复制相似问题