brefphp / laravel-bridge

Package to use Laravel on AWS Lambda with Bref
https://bref.sh/docs/frameworks/laravel.html
MIT License
314 stars 63 forks source link

How can I setup multiple queues in 1 application? #62

Open hanachan1026 opened 2 years ago

hanachan1026 commented 2 years ago

When I setup single queue, it works like a charm. But if I want to have multiple queues, since we can only specify one queue url in SQS_QUEUE I don't know how to setup.

The reason why I want multiple queues in 1 application is that I want to know if a specific job is still processing. To do that I separate queues for each jobs and long polling the status of queue.

mnapoli commented 2 years ago

I think you would have to create a new queue connection in config/queue.php (per Laravel's documentation).

And then:

aristidesneto-bnw commented 2 years ago

@mnapoli Could you tell why this implementation does not work?

I'm dynamically getting the queue name through the event variable. It doesn't show any error, but also the message is not sent to the queue.

# File: worker.php

return function (array $event, Context $context) {

    /** @var Application $app */
    $app = require __DIR__ . '/bootstrap/app.php';

    $kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
    $kernel->bootstrap();

    $message = $event['Records'][0];

    $queueName = explode(':', $message['eventSourceARN'])[5];

    $queueUrl = getenv('SQS_PREFIX') . $queueName;

    Log::info("Message '{$message['messageId']}' sent to '{$queueUrl}' ");

    return $app->makeWith(LaravelSqsHandler::class, [
        'connection' => 'sqs',
        'queue' => $queueUrl,
    ]);
};

References:

Would you help me? Thanks

alexw23 commented 1 month ago

Did you figure this out?

I haven't tested this but you should be able to put an environment variable per handler.

constructs:
    primary-queue:
        type: queue
        worker:
            handler: Bref\LaravelBridge\Queue\QueueHandler
            runtime: php-81
            timeout: 60 # seconds
            environment:
                 QUEUE_HANDLE_CONNECTION: primary
     secondary-queue:
        type: queue
        worker:
            handler: Bref\LaravelBridge\Queue\QueueHandler
            runtime: php-81
            timeout: 60 # seconds
            environment:
                 QUEUE_HANDLE_CONNECTION: secondary

Then override your the existing service provider definition of $connection, in your own AppServiceProvider.

$this->app->when(QueueHandler::class)
            ->needs('$connection')
            ->give(env('QUEUE_HANDLE_CONNECTION'));

Ideally this would all be magically resolved by QueueHandler instead based on the Job Queue ARN itself. I'll see if I can put together a PR later.