Laravel queue keep processing multiple times for a job

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?


Laravel try to run the job again and again.

php artisan queue:work --tries=3

Upper command will only try to run the jobs 3 times.

Hope this helps