Skip to content

Commit 734463a

Browse files
committed
AL 2023 Updates and Support up to Laravel 11.x
- increased Laravel version constraint to 11 - dropped support for Laravel 5 - updated Readme - updated supervisor deploy script to work on Amazon Linux 2023
1 parent 31d05e8 commit 734463a

File tree

8 files changed

+68
-225
lines changed

8 files changed

+68
-225
lines changed

README.md

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Laravel 5 - 9.x Queue Worker for Elastic Beanstalk
1+
# Laravel 6 - 11.x Queue Worker for Elastic Beanstalk
22

33
*Use your Laravel application as a worker to consume queues on AWS Elasticbeanstalk*
44

5-
Laravel provides a [wonderful array](https://laravel.com/docs/5.1/queues) of drivers for consuming queues within your application as well as [some documentation](https://laravel.com/docs/5.1/queues#supervisor-configuration) on how to manage your application with [Supervisord](http://supervisord.org/) when it is acting as a worker.
5+
Laravel provides a [wonderful array](https://laravel.com/docs/11.x/queues) of drivers for consuming queues within your application as well as [some documentation](https://laravel.com/docs/11.x/queues#supervisor-configuration) on how to manage your application with [Supervisord](http://supervisord.org/) when it is acting as a worker.
66

77
Unfortunately that's where the documentation ends. There is no guidance on how to manage multiple workers from a devops context which is a huge bummer. But don't worry fam I've got your covered.
88

@@ -16,11 +16,11 @@ Unfortunately that's where the documentation ends. There is no guidance on how t
1616
* **Parsing of EB environmental variables to generate supervisor config**
1717
* **Or using a pre-built supervisor config supplied in project**
1818

19-
## Amazon Linux 1 deprecation
19+
## Amazon Linux 1 is deprecated and Amazon Linux 2023 is recommended
2020

21-
Amazon Linux 1 (AL1) is going to be unsupported soon, it is advised to migrate to use Amazon Linux 2 (AL2)
21+
Amazon Linux 1 (AL1) is already retired, even Amazon Linux 2 (AL2) will soon going to be unsupported too, so it's recommended to migrate to use Amazon Linux 2023 (AL2023)
2222
https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.migration-al.html
23-
Starts from this release will only support AL2, please use previous releases for use in AL1
23+
Starts from this release will only support AL2023, please use previous releases for use in AL2, with this release will also drop support for Laravel 5 (PHP 7) since EB only support starting from PHP 8.1
2424

2525
# Let's get down to business
2626

@@ -32,21 +32,15 @@ Require this package
3232
composer require "foxxmd/laravel-elasticbeanstalk-queue-worker"
3333
```
3434

35-
or for Amazon Linux 1,
35+
or for Amazon Linux 2,
3636

3737
```bash
38-
composer require "foxxmd/laravel-elasticbeanstalk-queue-worker@^0.3"
38+
composer require "foxxmd/laravel-elasticbeanstalk-queue-worker@^1.0"
3939
```
4040

4141
**After installing the package you can either:**
4242

43-
Add the ServiceProvider to the providers array in `config/app.php` (for Laravel 5.4 or lower)
44-
45-
```php
46-
FoxxMD\LaravelElasticBeanstalkQueueWorker\ElasticBeanstalkQueueWorkerProvider::class
47-
```
48-
49-
Then, publish using artisan
43+
Publish using artisan
5044

5145
```php
5246
php artisan vendor:publish --tag=ebworker
@@ -58,7 +52,7 @@ Copy everything from `src/.ebextensions` into your own `.ebextensions` folder an
5852

5953
**Note:** This library only consists of the EB deploy steps -- the provider is only for a convenience -- so if you want to you can modify/consolidate the `.ebextensions` / `.platform` folder if you're not into me overwriting your stuff.
6054

61-
Don't forget to add +x permission to the EB Platform Hooks scripts
55+
Don't forget to add +x permission to the EB Platform Hooks scripts ([no longer required for Amazon Linux platform that released on or after April 29, 2022](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.hooks.html#platforms-linux-extend.hooks.more))
6256

6357
```bash
6458
find .platform -type f -iname "*.sh" -exec chmod +x {} +
@@ -79,13 +73,7 @@ IS_WORKER = true
7973

8074
### Set Queue Driver / Connection
8175

82-
Set the [driver](https://laravel.com/docs/5.1/queues#introduction) in your your EB environmental variables:
83-
84-
```bash
85-
QUEUE_DRIVER = [driver]
86-
```
87-
88-
Since Laravel 5.7 the variable name got changed so will also support the new name:
76+
Set the [driver](https://laravel.com/docs/11.x/queues#driver-prerequisites) in your your EB environmental variables:
8977

9078
```bash
9179
QUEUE_CONNECTION = [driver]
@@ -98,15 +86,15 @@ All queues are configured using EB environmental variables with the following sy
9886
**Note**: brackets are placeholders only, do not use them in your actual configuration
9987

10088
```bash
101-
queue[QueueName] = [queueName] # Required. The name of the queue that should be run.
89+
queue[QueueName] = [driver] # Required. The name of the queue that should be run. Value is driver/connection name
10290
[QueueName]NumProcs = [value] # Optional. The number of instances supervisor should run for this queue. Defaults to 1
10391
[QueueName]Tries = [value] # Optional. The number of times the worker should attempt to run in the event an unexpected exit code occurs. Defaults to 5
10492
[QueueName]Sleep = [value] # Optional. The number of seconds the worker should sleep if no new jobs are in the queue. Defaults to 5
10593
[QueueName]StartSecs = [value] # Optional. How long a job should run for to be considered successful. Defaults to 1
10694
[QueueName]Delay = [value] # Optional. Time in seconds a job should be delayed before returning to the ready queue. Defaults to 0
10795
```
10896

109-
Add one `queue[QueueName] = [queueName]` entry in your EB environmental variables for each queue you want to run. The rest of the parameters are optional.
97+
Add one `queue[QueueName] = [driver]` entry in your EB environmental variables for each queue you want to run. The rest of the parameters are optional.
11098

11199
That's it! On your next deploy supervisor will have its configuration updated/generated and start chugging along on your queues.
112100

@@ -158,11 +146,11 @@ Otherwise `parseConfig.php` looks for a json file generated earlier that contain
158146

159147
```bash
160148
[program:$queue]
161-
command=php artisan queue:work $connection --queue=$queue --tries=$tries --sleep=$sleep --daemon
149+
process_name=%(program_name)s_%(process_num)02d
150+
command=php artisan queue:work $connection --queue=$queue --tries=$tries --sleep=$sleep --delay=$delay --daemon
162151
directory=/var/app/current/
163152
autostart=true
164153
autorestart=true
165-
process_name=$queue-%(process_num)s
166154
numprocs=$numProcs
167155
startsecs=$startSecs
168156
user=webapp
@@ -173,16 +161,16 @@ After parsing all queues it then appends the programs to a clean `supervisord.co
173161

174162
### 3. Deploy Supervisor
175163

176-
Now a bash script `workerDeploy.sh` checks for `IS_WORKER=TRUE` in the EB environmental variables:
164+
Now a bash script `02worker-deploy.sh` checks for `IS_WORKER=TRUE` in the EB environmental variables:
177165

178-
* If none is found the script does nothing and exists.
166+
* If none is found the script does nothing and exits.
179167
* If it is found
180-
* And there is no `init.d` script
181-
* Supervisor is installed using pip and the custom `supervisord` init script in this project is copied to `/etc/init.d`
168+
* And there is no `supervisord.conf`
169+
* Supervisor is installed using pip
182170
* Configuration is parsed
183-
* Supervisor is started
184171
* Supervisor is set to start at boot
185-
* And there is an `init.d` script
172+
* Supervisor is started
173+
* And there is an `supervisord.conf`
186174
* Supervisor is stopped
187175
* Configuration is parsed
188176
* Laravel artisan is used to restart the queue to refresh the daemon

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
}
1212
],
1313
"require": {
14-
"illuminate/console": "5 - 9",
15-
"illuminate/support": "5 - 9"
14+
"illuminate/console": "6 - 11",
15+
"illuminate/support": "6 - 11"
1616
},
1717
"autoload": {
1818
"psr-4": {

src/.platform/confighooks/postdeploy/02worker-deploy.sh

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@
33
#
44
# Description: For use with AWS elasticbeanstalk and Laravel. As part of a deployment will either skip, install,
55
# or update supervisord depending on environmental variables set in EB.
6-
# This script has been modified as necessary to support Amazon Linux 2
6+
# This script has been modified as necessary to support Amazon Linux 2023
77

88
updateSupervisor(){
99
cp .platform/queue-worker/supervisord.conf /etc/supervisord.conf
10-
sudo service supervisord stop
10+
systemctl stop supervisord
1111
php /var/app/current/artisan queue:restart # If this worker is running in daemon mode (most likely) we need to restart it with the new build
1212
echo "Sleeping a few seconds to make sure supervisor shuts down..." # https://github.com/Supervisor/supervisor/issues/48#issuecomment-2684400
1313
sleep 5
14-
sudo service supervisord start
14+
systemctl start supervisord
1515
}
1616

1717
installSupervisor(){
18-
easy_install supervisor
19-
cp /var/app/current/.platform/queue-worker/supervisord /etc/init.d/supervisord
20-
chmod 777 /etc/init.d/supervisord
21-
mkdir -m 766 /var/log/supervisor
22-
umask 022
23-
touch /var/log/supervisor/supervisord.log
18+
dnf install python3-pip -y
19+
pip install supervisor
20+
mkdir -p /var/log/supervisor
21+
mkdir -p /var/run/supervisor
22+
cp .platform/queue-worker/supervisord.service /usr/lib/systemd/system/supervisord.service
2423
cp .platform/queue-worker/supervisord.conf /etc/supervisord.conf
25-
/etc/init.d/supervisord start
26-
sudo chkconfig supervisord on
24+
systemctl daemon-reload
25+
systemctl enable supervisord
26+
systemctl start supervisord
2727
}
2828

2929
/opt/elasticbeanstalk/bin/get-config --output YAML environment | sed -e 's/^\(.*\): /\1=/g' > bashEnv
@@ -47,7 +47,7 @@ then
4747
echo 'Found worker key!';
4848
echo 'Starting worker deploy process...';
4949

50-
if [ -f /etc/init.d/supervisord ]
50+
if [ -f /etc/supervisord.conf ]
5151
then
5252
echo 'Config found. Supervisor already installed';
5353
updateSupervisor;

src/.platform/hooks/postdeploy/02worker-deploy.sh

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@
33
#
44
# Description: For use with AWS elasticbeanstalk and Laravel. As part of a deployment will either skip, install,
55
# or update supervisord depending on environmental variables set in EB.
6-
# This script has been modified as necessary to support Amazon Linux 2
6+
# This script has been modified as necessary to support Amazon Linux 2023
77

88
updateSupervisor(){
99
cp .platform/queue-worker/supervisord.conf /etc/supervisord.conf
10-
sudo service supervisord stop
10+
systemctl stop supervisord
1111
php /var/app/current/artisan queue:restart # If this worker is running in daemon mode (most likely) we need to restart it with the new build
1212
echo "Sleeping a few seconds to make sure supervisor shuts down..." # https://github.com/Supervisor/supervisor/issues/48#issuecomment-2684400
1313
sleep 5
14-
sudo service supervisord start
14+
systemctl start supervisord
1515
}
1616

1717
installSupervisor(){
18-
easy_install supervisor
19-
cp /var/app/current/.platform/queue-worker/supervisord /etc/init.d/supervisord
20-
chmod 777 /etc/init.d/supervisord
21-
mkdir -m 766 /var/log/supervisor
22-
umask 022
23-
touch /var/log/supervisor/supervisord.log
18+
dnf install python3-pip -y
19+
pip install supervisor
20+
mkdir -p /var/log/supervisor
21+
mkdir -p /var/run/supervisor
22+
cp .platform/queue-worker/supervisord.service /usr/lib/systemd/system/supervisord.service
2423
cp .platform/queue-worker/supervisord.conf /etc/supervisord.conf
25-
/etc/init.d/supervisord start
26-
sudo chkconfig supervisord on
24+
systemctl daemon-reload
25+
systemctl enable supervisord
26+
systemctl start supervisord
2727
}
2828

2929
/opt/elasticbeanstalk/bin/get-config --output YAML environment | sed -e 's/^\(.*\): /\1=/g' > bashEnv
@@ -47,7 +47,7 @@ then
4747
echo 'Found worker key!';
4848
echo 'Starting worker deploy process...';
4949

50-
if [ -f /etc/init.d/supervisord ]
50+
if [ -f /etc/supervisord.conf ]
5151
then
5252
echo 'Config found. Supervisor already installed';
5353
updateSupervisor;

src/.platform/queue-worker/parseConfig.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22

3-
function generateProgram($connection, $queue, $tries, $sleep, $numProcs, $delay, $startSecs, $environmentVal) {
4-
$queueVal = $queue !== null ? " --queue=$queue": '';
3+
function generateProgram($connection, $queue, $tries, $sleep, $numProcs, $delay, $startSecs, $environmentVal)
4+
{
5+
$queueVal = $queue !== null ? " --queue=$queue" : '';
56
$program = <<<EOT
67
78
[program:$queue]
@@ -20,7 +21,8 @@ function generateProgram($connection, $queue, $tries, $sleep, $numProcs, $delay,
2021
return $program;
2122
}
2223

23-
function getEBWorkerConfig($path) {
24+
function getEBWorkerConfig($path)
25+
{
2426
if (null === $path) {
2527
return null;
2628
} else {
@@ -84,7 +86,7 @@ function getEBWorkerConfig($path) {
8486
foreach ($envVars as $key => $val) {
8587
if (
8688
ctype_alnum($val) // alphanumeric doesn't need quotes
87-
|| (strpos($val, '"') === 0 && strrpos($val, '"') === count($val) -1) // if the value is already quoted don't double-quote it
89+
|| (strpos($val, '"') === 0 && strrpos($val, '"') === count($val) - 1) // if the value is already quoted don't double-quote it
8890
) {
8991
$formattedVal = $val;
9092
} else { // otherwise put everything in quotes for environment param http://supervisord.org/configuration.html#program-x-section-values
@@ -97,15 +99,12 @@ function getEBWorkerConfig($path) {
9799
$programs = '';
98100

99101
$isBeanstalk = false;
100-
if (
101-
!empty($lowerEnvVars['queue_connection']) && $lowerEnvVars['queue_connection'] === 'beanstalkd' ||
102-
!empty($lowerEnvVars['queue_driver']) && $lowerEnvVars['queue_driver'] === 'beanstalkd'
103-
) {
102+
if (!empty($lowerEnvVars['queue_connection']) && $lowerEnvVars['queue_connection'] === 'beanstalkd') {
104103
$isBeanstalk = true;
105104
}
106105

107106
foreach ($lowerEnvVars as $key => $val) {
108-
if (strpos($key, 'queue') !== false && !in_array($key, ['queue_driver', 'queue_connection'])) {
107+
if (substr($key, 0, 5) === 'queue' && $key !== 'queue_connection') {
109108
$tryKey = substr($key, 5) . 'tries'; //get queue $key + tries to see if custom tries is set
110109
$sleepKey = substr($key, 5) . 'sleep'; //get queue $key + sleep to see if custom sleep is set
111110
$numProcKey = substr($key, 5) . 'numprocs'; //get queue $key + num process to see if custom number of processes is set
@@ -117,7 +116,7 @@ function getEBWorkerConfig($path) {
117116
$numProcs = isset($lowerEnvVars[$numProcKey]) ? $lowerEnvVars[$numProcKey] : 1;
118117
$startSecs = isset($lowerEnvVars[$startSecsKey]) ? $lowerEnvVars[$startSecsKey] : 1;
119118
$delay = isset($lowerEnvVars[$delayKey]) ? $lowerEnvVars[$delayKey] : 0;
120-
// if using beanstalk connection should always be beanstalkd and specify tube in queue, otherwise us queue name as connection
119+
// if using beanstalk connection should always be beanstalkd and specify tube in queue, otherwise use queue driver name as connection
121120
$connection = $isBeanstalk ? 'beanstalkd' : $val;
122121
// if not using beanstalk we don't need queue probably
123122
$queue = $isBeanstalk ? $val : null;

0 commit comments

Comments
 (0)