ideas icon indicating copy to clipboard operation
ideas copied to clipboard

SQS batch method in SQSQueue::bulk

Open AbdullahZN opened this issue 5 years ago • 7 comments

Hi,

Since SQS has a sendMessageBatch action. Would it be ok to implement a bulk method for SQSQueue with the following:

class SqsQueue
{    
    public function bulk($jobs, $data = '', $queue = null)
    {
        $entries = [];
        foreach ($jobs as $id => $job) {
            $entries[] = [
                'Id' => $id,
                'MessageBody' => $this->createPayload($job, $data),
            ];
        }

        return $this->sqs->sendMessageBatch(
            [
                'QueueUrl' => $this->getQueue($queue),
                'Entries' => $entries,
            ]
        );
    }
}

I'd be glad to submit a PR

AbdullahZN avatar Apr 11 '19 09:04 AbdullahZN

What would be the API change for somebody to bulk queue jobs?

Currently you dispatch jobs based on either dispatch(new JobName) or JobName::dispatch.

What is the proposed change to send many at once?

alexbowers avatar Apr 29 '19 18:04 alexbowers

@alexbowers There are many more ways to dispatch jobs, the most relevant one would be Queue::bulk($arrayOfJobs, $data, $queue);

sisve avatar Apr 30 '19 05:04 sisve

Yes exactly, you'll need to explicitly tell that you want to batchPush.

Using bulk will work for other type of queues as well since the bulk method is implemented in Illuminate\Queue\Queue eg: you are using SyncQueue locally & SqsQueue on production

AbdullahZN avatar Apr 30 '19 07:04 AbdullahZN

Well damn, @sisve That's going to be crazy helpful to me. Didn't realise you could do bulk already. Is this documented? :o

alexbowers avatar Apr 30 '19 09:04 alexbowers

@alexbowers Kind of. Does the api docs count? The contract got the bulk method in Laravel 5.4, but the implementation has supported it since Laravel 4.0.7. The base Queue class implements it by looping through the jobs and calling push() on them one-by-one, so all queues "support" the method, they are perhaps not just optimized for it.

https://laravel.com/api/5.8/Illuminate/Queue/Queue.html#method_bulk

sisve avatar May 01 '19 08:05 sisve

Bumping this issue. I plan to override the existing method in my app so it does the following:

  • 0 messages -> ignore
  • 1 - 10 messages under 256kb -> batch dispatch
  • 10+ messages -> loop, batch dispatching when size or count threshold is met.

Would laravel accept a PR?

atymic avatar Jun 18 '21 07:06 atymic

For anyone wondering, i implemented this, async bulk dispatch :) https://packagist.org/packages/atymic/laravel-bulk-sqs-queue

atymic avatar Jun 20 '21 11:06 atymic