Laravel排队处理作业,即使有延迟也能立即处理 [英] Laravel queued jobs processed immediately even with a delay

查看:82
本文介绍了Laravel排队处理作业,即使有延迟也能立即处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发有关私人服务器的个人应用程序(例如, Minecraft 服务器),由于查询服务器需要一些时间,因此我决定实施排队的作业.但是,它们无法正常工作,即使被延迟,它们在被调用时也会立即运行,从而导致页面请求中的大量延迟.

I'm currently developing my personal application that is about private servers (for example, Minecraft servers) and since querying the server takes some time, I decided to implement queued jobs. However, they are not working properly, and they run immediately when called even though they are delayed, causing a massive latency in a page request.

这是我HomeController的index(),它调用作业以30秒的延迟更新每台服务器:

Here's my HomeController's index() which calls the job to update every server with a 30 seconds delay:

public function index()
{
    $servers = Server::all();

    foreach($servers as $server)
    {
        // Job Dispatch
        $job = (new UpdateServer($server->id))->delay(30);
        $this->dispatch($job);
    }
    return view('serverlist.index', compact('servers'));
}

更新服务器的作业类如下:

The job class that updates the servers is the following:

class UpdateServer extends Job implements SelfHandling, ShouldQueue
{
    use InteractsWithQueue, SerializesModels;
    protected $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

    public function handle(){
        $server = Server::findOrFail($this->id);

        // Preparing the packet
        $test = new RAGBuffer();
        $test->addChar('255');
        $test->addChar('1');
        $test->addShort(1 | 8);

        // Finding the server
        $serverGame = new RAGServer($server->server_ip);

        // Get server information
        $status = $serverGame->sendPacket($test);

        $server->onlinePlayers = $status->getOnline();
        $server->peakPlayers = $status->getPeak();
        $server->maxPlayers = $status->getMax();

        if (!$server->save()) {
            // Error occurred
        }
    }
}

无论何时运行HomeController的index(),页面请求都会有很大的延迟.我按照Laravel官方网页上的教程进行操作,试图找到答案,但没有找到任何答案.

Whenever the HomeController's index() is run, there's a massive delay in the page request. I followed the tutorial at Laravel's Official Webpage, and I tried to find answers, but I didn't find anything.

那么,我在做什么错?为什么不将工作延迟30秒,然后在后台在服务器中执行此操作?

So, what am I doing wrong? Why isn't the job getting delayed 30 seconds and then doing this in background in my server?

也:handle()正在执行应有的操作.它查询服务器,发送数据包,并使用正确的信息更新我的数据库.

Also: The handle() is doing what it is supposed to. It queries the server, sends packets, and updates my database with the correct information.

推荐答案

您必须在项目的根目录的 .env 文件中设置要使用的队列驱动程序.

You have to set up the queue driver you want to use in your project's root dir's .env file.

默认情况下,队列驱动程序是 sync ,它完全按照您的描述进行操作,并立即执行队列.

By default, the queue driver is sync which does exactly what you are describing, executing queues immediately.

您可以选择一些不同的队列驱动程序,例如beantalked或redis(这是我的选择).在laracasts.com上有关于设置的 优秀免费赠品 豆豆般的队列.

You can choose of some different queue drivers, such as beanstalked or redis (which would be my choice). There's an excellent freebie on laracasts.com about setting up a beanstalked queue.

要查看Laravel中所有可用的队列驱动程序选项,请在 此处 .

To view all available queue driver options in Laravel, have a look here.

这是一个.env示例

APP_ENV=local
APP_DEBUG=true
APP_KEY=SomeRandomString

DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync      // <-- Put the desired driver here

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

这篇关于Laravel排队处理作业,即使有延迟也能立即处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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