Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel queue keep processing multiple times for a job

Below is what's happening when i run php artisan queue:listen and at my job table only have one job

enter image description here

and this is my code :

public function handle(Xero $xero)
{
        $this->getAndCreateXeroSnapshotID();
        $this->importInvoices($xero);
        $this->importBankTransaction($xero);
        $this->importBankStatement($xero); 
        $this->importBalanceSheet($xero);
        $this->importProfitAndLoss($xero);

}
like image 670
Bathulah Mahir Avatar asked Feb 22 '18 09:02

Bathulah Mahir


Video Answer


1 Answers

In order for a job to leave the queue, it must reach the end of the handle function -- without errors and exceptions.

There must be something breaking inside one or more of your functions.

If an exception is thrown while the job is being processed, the job will automatically be released back onto the queue so it may be attempted again. https://laravel.com/docs/5.8/queues

The same behavior can be achieved with

$this->release()

If you can't figure out what is breaking, you can set your job to run only once. If an error is thrown, the job will be considered failed and will be put in the failed jobs queue.

The maximum number of attempts is defined by the --tries switch used on the queue:work Artisan command. https://laravel.com/docs/5.8/queues

php artisan queue:work --tries=1

If you are using the database queue, (awesome for debugging) run this command to create the failed queue table

php artisan queue:failed

Finally, to find out what is wrong with your code. You can catch and log the error.

public function handle(Xero $xero)
{
    try{
        $this->getAndCreateXeroSnapshotID();
        $this->importInvoices($xero);
        $this->importBankTransaction($xero);
        $this->importBankStatement($xero); 
        $this->importBalanceSheet($xero);
        $this->importProfitAndLoss($xero);
    }catch(\Exception $e){
        Log::error($e->getMessage());
    }
}

You could also set your error log channel to be slack, bugsnag or whatever. Just be sure to check it. Please don't be offended, it's normal to screw up when dealing with laravel queues. How do you think I got here?

like image 137
Magus Avatar answered Sep 22 '22 02:09

Magus