Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel queued jobs processed immediately even with a delay

Tags:

laravel-5

jobs

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.

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         }     } } 

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.

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

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

like image 847
Micael Sousa Avatar asked Jun 27 '15 17:06

Micael Sousa


People also ask

Is Laravel queue asynchronous?

php - Laravel Jobs are not asynchronous - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

What is the difference between job and queue in Laravel?

Jobs and QueuesThe line itself is the Queue, and each customer in the line is a Job. In order to process Jobs in the Queue you need command line processes or daemons. Think of launching a queue daemon on the command line as adding a new bank teller to the pool of available bank tellers.

How does queue work in Laravel?

Laravel queues provide a unified API across a variety of different queue backends, such as Beanstalk, Amazon SQS, Redis, or even a relational database. Queues allow you to defer the processing of a time consuming task, such as sending an email, until a later time.


1 Answers

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

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

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.

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

Here's a .env example

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 
like image 106
baao Avatar answered Sep 19 '22 06:09

baao