laravellaravel-jobs

Laravel jobs being processed but not doing anything


I have checked multiple answers regarding this issue but didn't seem to have a proper solution. I'm firing a job, and it's working well in sync mode, but not working at all in database driver.

queue.php

'default' => env('QUEUE_CONNECTION', 'sync'),

.env

QUEUE_DRIVER=database

How i'm firing the job. I tried removing onConnection('database') but it would run on sync driver instead

SendNotifications::dispatch(array("message" => "test"))->onConnection('database');

I'm executing the following command to listen

php artisan queue:work database

When I'm firing the job, it gets written in the jobs table, after a few seconds, it gets processed. The problem is: It's not firing. If I remove the onConnection('database') it works on sync mode without any issues.

class SendNotifications implements ShouldQueue {

 public function __construct(){
 }

public function handle(){
   // here i'm connecting to a notifications api
 }

}

Example of what's inside handle()

if($this->event_name === "example"){
            $room = $this->getRoomByID($example_id_variable); // database_call
            if(empty($room) === true) {
              return;
            }
            $notify_receivers = getBlabla(); //get the receivers from the database (complex query)
            if(empty($notify_receivers) === false){
              foreach($notify_receivers as $receiver){
                $this->sendMessageAPI($receiver, $this->payload);
              }
            }
          }

sentMessageAPI

public function sendMessageAPI($id, $payload) {
    $curl = curl_init();

    $post_fields = array(
        "key" => "key",
        "secret" => "secret",
        "channelId" => $id,
        "message" => $payload
    );
    curl_setopt_array($curl, array(
        CURLOPT_URL => "https://www.piesocket.com/api/publish",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => json_encode($post_fields),
        CURLOPT_HTTPHEADER => array(
            "Content-Type: application/json"
        ),
    ));

    $response = curl_exec($curl);
}

I'm running this on a localhost using xampp. Any ideas why it's not actually firing? I've checking the payload column in the database table jobs and it's correct.


Solution

  • First, you'll want to use QUEUE_CONNECTION not QUEUE_DRIVER in your .env file. That should remove the need for onConnection('database') and make the database your default. If you want to force a single job to run sync, you can use dispatch_now.

    Can you expand on "not firing"? You said it gets processed after a few seconds, so is the job not being picked up at all or just isn't functioning as intended? Does the job go into the failed_jobs table?

    Another important thing to note, you must restart your queue workers if you change the SendNotifications job class in your example. Workers do not necessarily re-read the class and may be using an older or incorrect class. For local testing I'd recommend either queue:listen or explore the --once argument. https://laravel.com/docs/8.x/queues#the-queue-work-command