Example of using Yandex Message Queue on Laravel
To work with queues, the Laravel
Installation
Install the Laravel software using the instructions
Getting started
- Create a service account.
- Assign the editor role to the service account.
- Create a static access key.
Set the environment variables:
export AWS_ACCESS_KEY_ID="<access_key_ID>"
export AWS_SECRET_ACCESS_KEY="<secret_key>"
Create a queue in Message Queue and copy the URL.
Guide
In this example, we create a demo job that sums up two numbers and a command that adds the job to a queue in Message Queue.
To use Message Queue with Laravel, follow this guide.
-
Create a test project named
mq_example
:composer create-project --prefer-dist laravel/laravel mq_example
-
Create an
Add
job:php artisan make:job Add
-
Open the file
/app/Jobs/Add.php
and reformat it as follows:<?php namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; class Add implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; private $addend1 = null; private $addend2 = null; /** * Create a new job instance. * * @return void */ public function __construct($addend1, $addend2) { $this->addend1 = $addend1; $this->addend2 = $addend2; } /** * Execute the job. */ public function handle() { $result = $this->addend1 + $this->addend2; print("{$this->addend1} + {$this->addend2} = $result\n"); } }
-
Create a new
ScheduleAdd
command:php artisan make:command ScheduleAdd
-
Open the file
app/Console/Commands/ScheduleAdd.php
and reformat it as follows:<?php namespace App\Console\Commands; use App\Jobs\Add; use Illuminate\Console\Command; class ScheduleAdd extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'sample:schedule-add'; /** * The console command description. * * @var string */ protected $description = 'Command description'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { Add::dispatch(2, 3); } }
-
Open the file
config/queue.php
and change the string'default' = > env ('QUEUE_CONNECTION', 'sync'),
to'default' => 'sqs',
. -
Fill in the parameters in the
sqs
section.To get values for the
prefix
andqueue
parameters, split the URL of your queue into two parts: the prefix ishttps://message-queue.api.cloud.yandex.net/
, while the queue parameter isb1g8ad42m6he********/dj6000000000********
without the leading/
.'sqs' => [ 'driver' => 'sqs', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'prefix' => env('SQS_PREFIX', 'https://message-queue.api.cloud.yandex.net/'), 'queue' => env('SQS_QUEUE', 'b1g8ad42m6he********/dj6000000000********/laravel-test'), 'region' => env('AWS_DEFAULT_REGION', 'ru-central1'), ],
-
Add the
aws/aws-sdk-php
package to the project dependencies:composer require aws/aws-sdk-php
-
Update the Composer configuration:
composer update
-
Run this command:
php artisan sample:schedule-add
-
Run this command:
php artisan queue:work