Skip to content

Instantly share code, notes, and snippets.

@thiagomarini
Last active January 4, 2019 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiagomarini/c3cf4634b8329875ac0a4dcb9e0d5c91 to your computer and use it in GitHub Desktop.
Save thiagomarini/c3cf4634b8329875ac0a4dcb9e0d5c91 to your computer and use it in GitHub Desktop.
Handling SQS events example
<?php
define('LARAVEL_START', microtime(true));
require __DIR__.'/vendor/autoload.php';
$app = require_once __DIR__ . '/bootstrap/app.php';
// Laravel does not create that directory automatically so we have to create it
// You can remove this if you do not use views in your application (e.g. for an API)
if (!is_dir(storage_path('framework/views'))) {
if (!mkdir(storage_path('framework/views'), 0755, true)) {
throw new \RuntimeException('Cannot create directory ' . storage_path('framework/views'));
}
}
$bref = new \Bref\Application;
$bref->httpHandler($app->getBrefHttpAdapter());
$non_http_events_handler = function (array $event) use ($app) {
$statuses = [];
/**
* Get the console kernel
*/
$kernel = $app->make(\App\Console\Kernel::class);
/**
* Boot the kernel to make the container ready
*/
$kernel->bootstrap();
$container = resolve(\Psr\Container\ContainerInterface::class);
/**
* SQS events
*/
foreach ($event['Records'] ?? [] as $record) {
/**
* Fire the job manually
*/
$job = new \Illuminate\Queue\Jobs\SyncJob($container, $record['body'], 'sync', 'sync');
$job->fire();
$statuses[] = 1;
}
return [
'statusCode' => 200,
'headers' => [
'Content-Type' => 'application/json',
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Credentials' => true,
],
'body' => ['message' => 'All good in the hood :)'],
'status' => $statuses,
];
};
$bref->simpleHandler($non_http_events_handler);
$bref->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment