andersb
2 months ago
286
10
Laravel

Queued jobs handled in correct order

Posted 2 months ago by andersb

I have a collection of items where I need to process each item using a queued job. This can easily be done by something like so:

$collection->each(function ($item, $key) {
    ProcessItem::dispatch($item)->onQueue('processing');
});

Challenge

Now the challenge is, that I would like to make sure that the order in which the items are handled is correct, so that the first item is handled before the next one and so on.

If a job fails, then it should still continue to process the next item.

Perfect scenario

It would be ideal if two collections could be processed simultaneously, but the order for each collection was correct. Imagine if user A posts a $collectionA and user B posts another collection $collectionB. Then the system could handle the items like so:

  • $collectionA[0]
  • $collectionA[1]
  • $collectionA[2]
  • $collectionB[0] //Now the two request are both being handled
  • $collectionA[3]
  • $collectionB[1]
  • $collectionA[4]
  • ....

Please sign in or create an account to participate in this conversation.