jericopulvera's avatar

Question about queue

I have this code.

 foreach ($properties as $property) {
     // Upload the property
     dispatch(new SetProperties($property, $this->soapWrapper));
     
      // Upload property images
      foreach ($property->images as $image) {
         dispatch(new UploadImage($property, $image, $this->soapWrapper));
     }
}
    

Now this code will generate lots of jobs in order.

  • SetPropertiesJob
  • UploadImageJob
  • UploadImageJob
  • UploadImageJob
  • UploadImageJob
  • UploadImageJob
  • UploadImageJob
  • UploadImageJob
  • UploadImageJob
  • UploadImageJob
  1. what will happen if I run 10 separate php artisan queue:work?

  2. what if the SetPropertiesJob Failed what will happen to the other jobs that depends on it?

0 likes
2 replies
spekkionu's avatar
Level 48

You cannot depend on the order the jobs are processed (they will be pulled from the queue in order but may take varying amounts of time to complete) so if you have a job that depends on another it should not be queued until the job it depends on completes. Easy way to do that would be to have the job itself dispatch the dependent jobs (or fire an event).

So if in your example the UploadImage job would fail if it run before the SetProperties job is complete you should move the dispatching of those jobs into the SetProperties job.

If you are using a queue driver that uses a dedicated queuing system like beanstalkd, amazon sqs, or ironmq running multiple instances of the queue worker will result in jobs being processed in parallel. You wont have to worry about them trying to process the same job as that is built into the service.

I'm not sure if this is as safe with the redis or database queue drivers. There might be a risk of a race condition where multiple workers try to start the job before it is locked as being processed.

2 likes
bobbybouwmann's avatar
  1. The jobs are runned in the order that they are placed on the queue, so I don't think you would have a real problem here since the first job (SetProperties) is run first in all cases

  2. They will probably fail as well, but I think you need to add a fail save to it just to be sure. There is probably a way in your code to check if the SetProperties job succeeded at all.

1 like

Please or to participate in this conversation.