Skip to content

Commit 927a0b1

Browse files
authored
Simplify the configuration of Laravel Queues functions (#46)
1 parent 6a73084 commit 927a0b1

File tree

8 files changed

+40
-71
lines changed

8 files changed

+40
-71
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
- Logs are now written in plain text by default instead of JSON. To enable JSON logs, set `channels.stderr.formatter` to `Monolog\Formatter\JsonFormatter::class` in `config/logging.php`.
1010
- The automatic population of environment variables via `APP_SSM_PREFIX` and `APP_SSM_PARAMETERS` has been removed. The native Bref 2.0 feature to load SSM parameters into environment variables can be used instead ([#36](https://github.com/cachewerk/bref-laravel-bridge/pull/36))
1111
- If you use Octane, remove the `bref/runtime.php` file, remove the `APP_RUNTIME` environment variable (in `serverless.yml`) and set your Octane function handler to: `handler: CacheWerk\BrefLaravelBridge\Http\OctaneHandler`.
12+
- If you use Laravel Queues, remove the `bref/runtime.php` file, remove the `APP_RUNTIME` environment variable (in `serverless.yml`) and set your Octane function handler to: `handler: CacheWerk\BrefLaravelBridge\Queue\QueueHandler`.
1213

1314
## [Unreleased]
1415
## [v0.3.0] - 2022-11-15

README.md

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Next, install the package and publish the custom Bref runtime:
2525
```
2626
composer require cachewerk/bref-laravel-bridge
2727
28-
php artisan vendor:publish --tag=bref-runtime
28+
php artisan vendor:publish --tag=serverless-config
2929
```
3030

3131
This will create the `serverless.yml` config file.
@@ -71,21 +71,13 @@ functions:
7171

7272
## Laravel Queues
7373

74-
If you want to run Laravel Queues, you will need to publish the PHP runtime (just like in the "Octane" section above):
75-
76-
```
77-
php artisan vendor:publish --tag=bref-runtime
78-
```
79-
80-
Then, you can add a `queue` function to `serverless.yml`:
74+
If you want to run Laravel Queues, you will need to add a `queue` function to `serverless.yml`:
8175

8276
```yml
8377
functions:
8478
queue:
85-
handler: php/runtime.php
79+
handler: CacheWerk\BrefLaravelBridge\Queue\QueueHandler
8680
timeout: 59 # in seconds
87-
environment:
88-
APP_RUNTIME: queue
8981
layers:
9082
- ${bref:layer.php-81}
9183
events:
@@ -114,7 +106,7 @@ Lastly tell Bref to support binary responses on your `web` function:
114106
```yml
115107
functions:
116108
web:
117-
handler: php/runtime.php
109+
handler: public/index.php
118110
environment:
119111
BREF_BINARY_RESPONSES: 1
120112
```
@@ -134,9 +126,8 @@ Lastly, set the `OCTANE_PERSIST_DATABASE_SESSIONS` environment variable.
134126
```yml
135127
functions:
136128
web:
137-
handler: php/runtime.php
129+
handler: CacheWerk\BrefLaravelBridge\Http\OctaneHandler
138130
environment:
139-
APP_RUNTIME: octane
140131
BREF_LOOP_MAX: 250
141132
OCTANE_PERSIST_DATABASE_SESSIONS: 1
142133
```

examples/getting-started/serverless.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,8 @@ functions:
6666
- httpApi: '*'
6767

6868
cli:
69-
handler: php/runtime.php
69+
handler: artisan
7070
timeout: 720
71-
environment:
72-
APP_RUNTIME: cli
7371
layers:
7472
- ${bref:layer.php-81}
7573
- ${bref:layer.console}
@@ -79,10 +77,8 @@ functions:
7977
input: '"schedule:run"'
8078

8179
queue:
82-
handler: php/runtime.php
80+
handler: CacheWerk\BrefLaravelBridge\Queue\QueueHandler
8381
timeout: 59
84-
environment:
85-
APP_RUNTIME: queue
8682
layers:
8783
- ${bref:layer.php-81}
8884
events:

phpcs.xml.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
</rule>
88

99
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
10-
<exclude-pattern>*/stubs/runtime.php</exclude-pattern>
1110
<exclude-pattern>src/bref-init.php</exclude-pattern>
1211
</rule>
1312

src/BrefServiceProvider.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace CacheWerk\BrefLaravelBridge;
44

5+
use CacheWerk\BrefLaravelBridge\Queue\QueueHandler;
6+
57
use Illuminate\Log\LogManager;
68

79
use Illuminate\Support\Facades\Config;
@@ -58,6 +60,10 @@ public function register()
5860
Config::set('queue.connections.sqs.secret');
5961
Config::set('queue.connections.sqs.token', env('AWS_SESSION_TOKEN'));
6062
Config::set('queue.connections.sqs.prefix', env('SQS_PREFIX', "https://sqs.{$region}.amazonaws.com/{$account}"));
63+
64+
$this->app->when(QueueHandler::class)
65+
->needs('$connection')
66+
->giveConfig('queue.default');
6167
}
6268

6369
/**
@@ -77,10 +83,6 @@ public function boot(Dispatcher $dispatcher, LogManager $logManager, FailedJobPr
7783
$this->publishes([
7884
__DIR__ . '/../config/bref.php' => config_path('bref.php'),
7985
], 'bref-config');
80-
81-
$this->publishes([
82-
__DIR__ . '/../stubs/runtime.php' => base_path('php/runtime.php'),
83-
], 'bref-runtime');
8486
}
8587

8688
$dispatcher->listen(

src/HandlerResolver.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
use Bref\Runtime\FileHandlerLocator;
88
use Psr\Container\ContainerInterface;
9+
910
use Illuminate\Foundation\Application;
11+
use Illuminate\Foundation\Console\Kernel;
12+
1013
use CacheWerk\BrefLaravelBridge\Http\OctaneHandler;
1114

1215
/**
@@ -17,14 +20,14 @@
1720
*/
1821
class HandlerResolver implements ContainerInterface
1922
{
20-
private ?Application $laravelApp;
23+
private ?Application $laravel;
2124
private FileHandlerLocator $fileLocator;
2225

2326
public function __construct()
2427
{
2528
// Bref's default handler resolver
2629
$this->fileLocator = new FileHandlerLocator;
27-
$this->laravelApp = null;
30+
$this->laravel = null;
2831
}
2932

3033
public function get(string $id)
@@ -40,7 +43,7 @@ public function get(string $id)
4043
}
4144

4245
// If not, we try to get the handler from the Laravel container
43-
return $this->laravelApp()->get($id);
46+
return $this->laravel()->get($id);
4447
}
4548

4649
public function has(string $id): bool
@@ -56,17 +59,17 @@ public function has(string $id): bool
5659
}
5760

5861
// If not, we try to get the handler from the Laravel container
59-
return $this->laravelApp()->has($id);
62+
return $this->laravel()->has($id);
6063
}
6164

6265
/**
6366
* Create and return the Laravel application.
6467
*/
65-
private function laravelApp(): Application
68+
private function laravel(): Application
6669
{
6770
// Only create it once
68-
if ($this->laravelApp) {
69-
return $this->laravelApp;
71+
if ($this->laravel) {
72+
return $this->laravel;
7073
}
7174

7275
$bootstrapFile = getcwd() . '/bootstrap/app.php';
@@ -77,17 +80,20 @@ private function laravelApp(): Application
7780
);
7881
}
7982

80-
$this->laravelApp = require $bootstrapFile;
83+
$this->laravel = require $bootstrapFile;
8184

82-
if (! $this->laravelApp instanceof Application) {
85+
if (! $this->laravel instanceof Application) {
8386
throw new RuntimeException(sprintf(
8487
"Expected the `%s` file to return a %s object, instead it returned `%s`",
8588
$bootstrapFile,
8689
Application::class,
87-
is_object($this->laravelApp) ? get_class($this->laravelApp) : gettype($this->laravelApp),
90+
is_object($this->laravel) ? get_class($this->laravel) : gettype($this->laravel),
8891
));
8992
}
9093

91-
return $this->laravelApp;
94+
$kernel = $this->laravel->make(Kernel::class);
95+
$kernel->bootstrap();
96+
97+
return $this->laravel;
9298
}
9399
}

src/Queue/QueueHandler.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,27 @@ class QueueHandler extends SqsHandler
3636
*/
3737
protected const JOB_TIMEOUT_SAFETY_MARGIN = 1.0;
3838

39+
/**
40+
* The name of the SQS queue.
41+
*
42+
* @var string
43+
*/
44+
protected string $queueName;
45+
3946
/**
4047
* Creates a new SQS queue handler instance.
4148
*
4249
* @param \Illuminate\Container\Container $container
4350
* @param \Illuminate\Contracts\Events\Dispatcher $events
4451
* @param \Illuminate\Contracts\Debug\ExceptionHandler $exceptions
4552
* @param string $connection
46-
* @param string $queue
4753
* @return void
4854
*/
4955
public function __construct(
5056
protected Container $container,
5157
protected Dispatcher $events,
5258
protected ExceptionHandler $exceptions,
5359
protected string $connection,
54-
protected string $queue,
5560
) {
5661
$queue = $container->make(QueueManager::class)
5762
->connection($connection);
@@ -60,6 +65,7 @@ public function __construct(
6065
throw new RuntimeException('Default queue connection is not a SQS connection');
6166
}
6267

68+
$this->queueName = $queue->getQueue(null);
6369
$this->sqs = $queue->getSqs();
6470
}
6571

@@ -112,7 +118,7 @@ protected function marshalJob(SqsRecord $sqsRecord): SqsJob
112118
$this->sqs,
113119
$message,
114120
$this->connection,
115-
$this->queue,
121+
$this->queueName,
116122
);
117123
}
118124

stubs/runtime.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)