Example of using Yandex Message Queue on Laravel

To work with queues, the Laravel PHP framework provides the Queue primitive. As a message broker, you can use Message Queue.

Installation

Install the Laravel software using the instructions on the framework's official website.

Before you start

  1. Create a service account.
  2. Assign a role to a editor service account.
  3. 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.

Instructions

In this example, we create a demo Job that adds up two numbers and a Command that queues the task.

To use Message Queue with Laravel, follow these instructions.

  1. Create a test project named ymq_example:

    $ composer create-project --prefer-dist laravel/laravel ymq_example
    
  2. Create an Add task:

    $ php artisan make:job Add
    
  3. 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");
        }
    }
    
  4. Create a new ScheduleAdd command:

    $ php artisan make:command ScheduleAdd
    
  5. 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);
        }
    }
    
  6. Open the file config/queue.php and change the string 'default' = > env ('QUEUE_CONNECTION', 'sync'), to 'default' => 'sqs',.

  7. Fill in the parameters in the sqs section.

    To get values for the prefix and queue parameters, split the URL of your queue into two parts: the prefix is https://message-queue.api.cloud.yandex.net/, while the queue parameter isb1gvlrnlei4l5idm9cbj/dj6000000000g53305qi 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', 'b1gvlrnlei4l5idm9cbj/dj6000000000g53305qi/laravel-test'),
        'region' => env('AWS_DEFAULT_REGION', 'ru-central1'),
    ],
    
  8. Add the aws/aws-sdk-php package to the project dependencies:

    $ composer require aws/aws-sdk-php
    
  9. Update the Composer configuration:

    $ composer update
    
  10. Run the command:

    $ php artisan sample:schedule-add
    
  11. Run the command:

    $ php artisan queue:work