Monday, April 3, 2017

Queue worker fails on all second jobs

My application has a serie of jobs that are handled by Laravel's queuing system (using Beanstalkd driver).

The application does the following: Each job is handled by a Job class. This Job class creates a Command class which is dispatched through Laravel's command bus.

This worked up until Laravel 5.3.10. Issue occurs since Laravel 5.3.11, and remains an issue in Laravel 5.4. Every job after the first fails. What happens is the following:

First job:

  • Handle on the Job class is called.
  • Command class is created, as well as a pipeline (Laravel's command bus) to put the command through.
  • Command is dispatched through the pipeline.
  • All is well, Job successful.

Second job

  • Handle on the Job class is called.
  • No new Command is created.
  • The Job class is dispatched through the command bus, instead of the lacking Command class. Resulting in fatal errors.

If I start a queue worker and have it just sync one job at a time (and start the worker again each time), no problems. Queue:listen also works. It only fails when using queue:work, which uses the deamon parameter.

I'm not really sure where to look. Maybe something changed in either Laravel's command bus or in it's queueing system? Why would new Command() not work in a second job?

Edit: To clear stuff up I created a simplified code example:

class Job implements ShouldQueue 
{
  public function handle()
  {
    $command = new Command();

    Bus::pipeThrough(['stuff']);
    Bus::dispatch($command);
  }
}

class Command
{
   public function __construct()
   {
     dump("Command created");
   }
}

Next I submit three of the same jobs on the queue. If I use queue:work outputs the following on the command line:

"Command created"

[2017-04-02 19:16:58] Processed: App\Jobs\Job

[2017-04-02 19:16:58] Failed: App\Jobs\ItemJob

[2017-04-02 19:16:58] Failed: App\Jobs\Job

However, if I use queue:listen it outputs:

"Command created"

[2017-04-02 19:16:58] Processed: App\Jobs\Job

"Command created"

[2017-04-02 19:16:58] Processed: App\Jobs\Job

"Command created"

[2017-04-02 19:16:58] Processed: App\Jobs\Job

If I comment out the Bus::pipeThrough(['stuff']); line, the Commands are created for every job.



via wouthoekstra

Advertisement