Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lumen 5.1 SQS queue configuration

I've created a new project using Lumen, to take care of sending emails and some other small tasks. However, for some of the emails that need to be sent I want to use Amazon's queue.

In the documentation of Lumen, they only thing that is being mentioned is: The QUEUE_DRIVER option in your .env file determines the queue "driver" that will be used by your application.

I have set this to SQS as suggested, however I'm a bit puzzled about the actual amazon credentials. How am I supposed to define those? The only "relevant" thing I've seen is from Amazon's documentation here and here. I have also installed the "aws/aws-sdk-php": "~3.0" package.

However these resources refer to using SQS's API directly. I want to be able to invoke the queue as it is being described in Lumen, so something like Queue::push() or $this->dispatch().

Here's the relevant code that I have.

This is in my .env file

QUEUE_DRIVER=sqs
AWS_KEY=key
AWS_SECRET=secretstuff
AWS_QUEUE=https://sqs.eu-west-1.amazonaws.com/
AWS_REGION=eu-west-1

This is my controller

<?php namespace App\Http\Controllers;

use App\Jobs\GuestEmailJob;
use Laravel\Lumen\Routing\Controller as BaseController;
use Log, Queue;
use Aws\Sqs\SqsClient;

class NotificationEmailController extends BaseController
{
    public function pushToQueue($id)
    {
        Queue::push(new GuestEmailJob($id));
        //$this->dispatch(new GuestEmailJob($id));
    }
}

And finally this is my job

<?php namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
use Log;

abstract class GuestEmailJob implements SelfHandling, ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    public function handle($id)
    {
        Log::info('within handle');
        $user = User::find($id);
    }
}

I've tried it both with Queue::push and $this->dispatch(). When a request hits the controller, everything goes fine until pushing to the queue and then nothing happens (no exceptions either). Anybody has any idea what am I doing wrong/missing?

like image 617
Fotis Avatar asked Mar 16 '23 12:03

Fotis


1 Answers

After looking a bit into the source code, apparently you should have the queue.php config file under the config directory. So I just pasted the default config file from laravel 5 and customized it and now it works fine. Pretty silly issue after all, but wasn't clear from the documentation itself. Here's the default queue configuration just in case somebody also runs into this.

<?php

return [

    'default' => env('QUEUE_DRIVER', 'sync'),

    'connections' => [

        'sync' => [
            'driver' => 'sync',
        ],

        'database' => [
            'driver' => 'database',
            'table' => 'jobs',
            'queue' => 'default',
            'expire' => 60,
        ],

        'beanstalkd' => [
            'driver' => 'beanstalkd',
            'host'   => 'localhost',
            'queue'  => 'default',
            'ttr'    => 60,
        ],

        'sqs' => [
            'driver' => 'sqs',
            'key'    => 'your-public-key',
            'secret' => 'your-secret-key',
            'queue'  => 'your-queue-url',
            'region' => 'us-east-1',
        ],

        'iron' => [
            'driver'  => 'iron',
            'host'    => 'mq-aws-us-east-1.iron.io',
            'token'   => 'your-token',
            'project' => 'your-project-id',
            'queue'   => 'your-queue-name',
            'encrypt' => true,
        ],

        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
            'queue'  => 'default',
            'expire' => 60,
        ],

    ],

    'failed' => [
        'database' => 'mysql', 'table' => 'failed_jobs',
    ],

];
like image 180
Fotis Avatar answered Mar 24 '23 05:03

Fotis