如何避免等待Laravel队列并继续使用功能 [英] How to avoid waiting for the Laravel queue and continue with functionality

查看:227
本文介绍了如何避免等待Laravel队列并继续使用功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个非常琐碎/新手的问题.我已经开始在其中一个控制器功能中使用Laravel Queue;但是,当我分派作业时,我必须等待队列完成才能让我继续执行该功能.我认为队列背后的整个概念是,它使您可以在后台处理时间密集的任务,以便继续使用其他功能.

This is probably a very trivial/novice question. I have started using Laravel Queue in one of my controller functions; however, when I dispatch the job, I have to wait for the queue to finish before it lets me continue with the function. I thought the whole concept behind queues is that it lets you process time-intensive tasks in the background so you can continue with rest of functionality.

这是我的控制器的外观:

This is what my controller looks like:

public function postCreate(Request $request)
{
    // Validate Request
    $this->validate($request, $this->rules());

    // I have to wait for this to finish before it lets me continue
    // I want this to be processed in the background instead
    $this->dispatchFrom('App\Jobs\GenerateReport', $request);

    return json_encode([
        'html' => '<p>some html</p>'
    ]);
}

推荐答案

如果脚本确实是在线处理作业,而不是离线处理,那么我最好的猜测是您正在使用同步驱动程序处理队列,以处理作业立即并被阻止.

If indeed the script is processing the job online, instead of offline, my best guess is that you're using the synchronous driver for your queue, which processes jobs immediately and is blocking.

检查config/queue.php文件以查看将默认"配置值设置为什么,默认情况下应读取它:env('QUEUE_DRIVER', 'sync').如果是这样,那是正确的,因此请查看您的.env文件,并确保为QUEUE_DRIVER设置了环境变量.

Check the config/queue.php file to see what the "default" config value is set to, it should read by default: env('QUEUE_DRIVER', 'sync'). If that's the case then that is correct, so then take a look at your .env file and make sure you have an environmental variable set for QUEUE_DRIVER.

如果在.env文件中将QUEUE_DRIVER变量设置为"sync",则说明您正在使用同步驱动程序.如果根本没有设置,那么queue.php文件中的该行表示它将回退到同步驱动程序.

If the QUEUE_DRIVER variable is set to "sync" in your .env file, then you're using the synchronous driver. If it's not set at all, then that line in the queue.php file says it will fall back to the synchronous driver.

如果所有这些都是正确的,只需使用您要使用的驱动程序更新环境变量,并确保您在config/queue.php文件中更新了该驱动程序的配置选项.

Should all of this be true, just update the environmental variable with the driver you wish to use, and make sure you update the configuration options for that driver in the config/queue.php file.

希望有帮助!

这篇关于如何避免等待Laravel队列并继续使用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆