Certainly! You can achieve this by defining a queue name prefix in your .env file and referencing it in your config/queue.php configuration. Here’s how you can do it:
Step 1: Add a Prefix to Your .env
Add a variable to your .env file for the queue prefix:
QUEUE_PREFIX=qa-
For production, you might set:
QUEUE_PREFIX=prod-
Step 2: Update config/queue.php
In your config/queue.php, update the SQS connection to use the prefix:
'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', env('QUEUE_PREFIX', '') . 'admin-medium'),
'suffix' => env('SQS_SUFFIX'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],
But for more flexibility, you can create a helper function to prefix the queue name when dispatching:
Step 3: Create a Helper Function (Optional, Recommended)
You can create a global helper in app/helpers.php (make sure to autoload it via composer):
if (! function_exists('queue_name')) {
function queue_name($name)
{
return env('QUEUE_PREFIX', '') . $name;
}
}
Step 4: Use the Helper When Dispatching
Now, when dispatching jobs, use the helper:
MyJob::dispatch()->onQueue(queue_name('admin-medium'));
This way, you only need to specify the base queue name (admin-medium), and the environment prefix will be automatically added.
Summary
- Add
QUEUE_PREFIXto your.env - Use a helper to prefix queue names when dispatching
- Now you can call
->onQueue('admin-medium')and it will resolve toqa-admin-mediumorprod-admin-mediumdepending on the environment.
Let me know if you need further clarification!