brefphp / laravel-bridge

Package to use Laravel on AWS Lambda with Bref
https://bref.sh/docs/frameworks/laravel.html
MIT License
319 stars 63 forks source link

Queued jobs trigger worker lambda, but jobs are not processed #60

Closed eneskaya closed 2 years ago

eneskaya commented 2 years ago

First of all: very nice work πŸ‘ Love the possibility to go serverless with a Laravel project, and so far everything works except for jobs processing.

I setup everything with a CDK project, so I hope I am not missing something. But here is my setup:

The queue Lambda config looks like this:

this.worker = new lambda.Function(scope, 'WorkerHandler', {
    runtime: lambda.Runtime.PROVIDED_AL2,
    handler: 'worker.php',
    layers: [
        lambda.LayerVersion.fromLayerVersionArn(
            scope,
            'BrefPHPWorkerLayer',
            'arn:aws:lambda:eu-central-1:209497400698:layer:php-81:16'
        ),
        lambda.LayerVersion.fromLayerVersionArn(
            scope,
            'ConsoleLayer',
            'arn:aws:lambda:eu-central-1:209497400698:layer:console:55'
        ),
    ],
    code: codeBundle,
    memorySize: 512,
    environment,
    timeout: cdk.Duration.minutes(2),
})

The job that I trigger simply does

/**
 * Execute the job.
 *
 * @return void
 */
public function handle()
{
    Log::info('IT WORKED!');
}

Debugging Setup

In the worker.php I placed a log which just logs Log::info('WORKER STARTED!');. I also went ahead and put a log in the LaravelSqsHandler within the handleSqs method, before the foreach loop

Actual result

Now, when a message lands in the queue, it definitely get's handled, this is what I see in the logs:

Bildschirmfoto 2021-12-28 um 13 15 26

Where I expected to also see IT WORKED!. What am I missing here? Thank you for a hint.

mnapoli commented 2 years ago

Hi, could you post the content of worker.php?

eneskaya commented 2 years ago

Hi, could you post the content of worker.php?

Yes, of course. Here you go:

<?php declare(strict_types=1);

use Bref\LaravelBridge\Queue\LaravelSqsHandler;
use Illuminate\Foundation\Application;

require __DIR__ . '/vendor/autoload.php';
/** @var Application $app */
$app = require __DIR__ . '/bootstrap/app.php';

/**
 * For Lumen, use:
 * $app->make(Laravel\Lumen\Console\Kernel::class);
 * $app->boot();
 */
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();

Log::info('WORKER STARTED!');

return $app->makeWith(LaravelSqsHandler::class, [
    'connection' => 'sqs', // this is the Laravel Queue connection
    'queue' => getenv('SQS_QUEUE'),
]);

Edit: Versions I am using

mnapoli commented 2 years ago

OH I found it. It confused me so much πŸ˜…

In your CDK config you are adding the console layer, but it doesn't make sense here. The console layer is specifically for running CLI scripts or binaries.

Here you are using PHP code (files & classes) to handle the event, so you can remove it and give it another try.

I guess that nothing was displayed in the output because with the console layer your worker.php file was executed "as-is", and the only thing it does is boot Laravel and return a service, and then it would exit (with the console layer).

eneskaya commented 2 years ago

Oh man, that was really the problem! It only cost me like 1 week of debugging πŸ˜„ Thank you so much for your quick help!

mnapoli commented 2 years ago

Oh yeah I hate when that happens ^^

No problem!