8
votes

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);

}
4
Are you using supervisor to monitor? If yes, how many workers have you setup?Tahir Raza
Check your laravel.log for errors, as this is the sort of behaviour when there is an error thrown.Jono20201
@Jono20201 thank you for that , can post your comment as answer?Bathulah Mahir

4 Answers

20
votes

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?

7
votes

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

1
votes

The solution that worked for me to delete the job after pushing them into the queue.

Consider the e.g.

class SomeController extends Controller{
  public function uploadProductCsv(){
   //process file here and push the code inot Queue
   Queue::push('SomeController@processFile', $someDataArray); 
  }

  public function processFile($job, $data){
    //logic to process the data
    $job->delete(); //after complete the process delete the job
  }

}

Note: This is implemented for laravel 4.2

0
votes

In my case the problem was the payload, I've created the variable private, but it needs to by protected.

class EventJob implements ShouldQueue
{       
    use InteractsWithQueue, Queueable, SerializesModels;

    // payload
    protected $command;
    // Maximum tries of this event
    public $tries = 5;

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

    public function handle()
    {
        $event = I_Event::create([
            'event_type_id' => $command->getEventTypeId(),
            'sender_url' => $command->getSenderUrl(),
            'sender_ip' => $command->getSenderIp()
        ]);

        return $event;
    }
}