8

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

}
3
  • Are you using supervisor to monitor? If yes, how many workers have you setup? Commented Feb 22, 2018 at 9:43
  • 1
    Check your laravel.log for errors, as this is the sort of behaviour when there is an error thrown. Commented Feb 22, 2018 at 9:43
  • @Jono20201 thank you for that , can post your comment as answer? Commented Feb 22, 2018 at 9:53

4 Answers 4

20

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?

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! after a whole morning of screwing things up again and again, this answer reminds me to keep calm and debug thoroughly... special thanks for the try/catch idea \o/
7

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

Comments

2

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

1 Comment

Thank you! THIS was exactly the problem, and I have no idea how you figured this out but you get +1 for curing my massive headache!
1

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

1 Comment

Appears your 'fix' is a bandaid rather than a solution...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.