Skip to main content

Laravel and Amazon SQS Queues

Premium
7 min read

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 01 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

M
mfiazahmad ✓ Link copied!

I have setup two queues (calculator-webhook-queue & offers-queue) on Amazon SQS with the following code:-

In .env file

QUEUE_CONNECTION=sqs
AWS_DEFAULT_REGION=us-east-1
AWS_ACCESS_KEY_ID=*******
AWS_SECRET_ACCESS_KEY=*******
SQS_PREFIX=*******
SQS_QUEUE=calculator-webhook-queue

In queue.php config file

'connections' => [
'sqs' => [
'driver' => 'sqs',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'prefix' => env('SQS_PREFIX', 'https://sqs.us-east-1.amazonaws.com/your-account-id'),
'queue' => env('SQS_QUEUE', 'default'),
'suffix' => env('SQS_SUFFIX'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
'after_commit' => false,
]
 
],

In supervisor config file on Amazon server

[program:amazon-sqs]
directory=/var/www/html/****
command=php artisan queue:work sqs --queue=calculator-webhook-queue,offers-queue
 
process_name=%(program_name)s_%(process_num)02d
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
;user=web
numprocs=3
redirect_stderr=true
stdout_logfile=/var/www/html/****/storage/logs/amazon-sqs.log
stopwaitsecs=58

I have two jobs that are dispatched like this into separate queue

PropertyOffersJob::dispatch($webhookData)->onConnection('sqs')->onQueue('offers-queue');
PropertyTaxJob::dispatch($webhookData)->onConnection('sqs')->onQueue('calculator-webhook-queue');

Both are working fine but are only pushed to the same queue "calculator-webhook-queue". I want each to go to their respective queues. Please help me where I am messing?

M
Modestas ✓ Link copied!

I haven't worked with the SQS, but I think that there is an issue with your queue is working in this manner:

start with calculator-webhook-queue and if there are no more jobs there - take from offers-queue.

While you should also start two workers, one for each of your Queues:

https://laravel.com/docs/11.x/queues#specifying-the-connection-queue

ps. Documentation of what you did right now is this: https://laravel.com/docs/11.x/queues#queue-priorities

M
mfiazahmad ✓ Link copied!

Thank you for your reply. I just solved it by creating two configuration files in /etc/supervisor/conf.d directory, each for one queue and it started working.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.