8000 Add queue workers through Laravel Horizon. · jarnovanleeuwen/laravel-dock@aff9fd4 · GitHub
[go: up one dir, main page]

Skip to content

Commit aff9fd4

Browse files
Add queue workers through Laravel Horizon.
1 parent 501d464 commit aff9fd4

23 files changed

+521
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ The stack can be managed through the `dock <command>` command-line executable. I
2929
|---------|-------------|
3030
| `up` | Start the local stack (webserver, database) for development. |
3131
| `scheduler` | Start Laravel's scheduler daemon. |
32+
| `queue` | Start Laravel Horizon (queue workers). |
3233
| `down` | Stop the local stack. |
3334
| `tail` | Tail and follow Docker logs from all running containers. |
3435
| `restart` | Restart the local stack. |

build/Dockerfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ RUN useradd --create-home webdev
1313
WORKDIR /var/www/html
1414

1515
# Install dependencies
16-
RUN apt-get update && apt-get install -y mysql-client zip
16+
RUN apt-get update && apt-get install -y mysql-client nano supervisor zip
1717

1818
# Install extensions
1919
RUN docker-php-ext-install opcache pcntl pdo_mysql
@@ -30,6 +30,10 @@ COPY ./config/php/local.ini $PHP_INI_DIR/conf.d/local.ini
3030
# Overwrite the default Apache vhost with the custom vhost
3131
COPY ./config/apache/vhost.conf /etc/apache2/sites-available/000-default.conf
3232

33+
# Add Supervisor config for Laravel Horizon
34+
RUN rm -rf /var/log/supervisor ; mkdir -m 777 /var/log/supervisor
35+
COPY ./config/supervisor/ /etc/supervisor/
36+
3337
# Install Composer
3438
COPY --from=composer /usr/bin/composer /usr/bin/composer
3539

build/docker-compose.local.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ services:
3131
networks:
3232
- network
3333

34+
# Queue worker
35+
queue:
36+
image: app-local
37+
depends_on:
38+
- app
39+
volumes:
40+
- ../src:/var/www/html:delegated
41+
networks:
42+
- network
43+
3444
# MySQL
3545
db:
3646
networks:

build/docker-compose.prod.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ services:
3737
restart_policy:
3838
condition: on-failure
3939

40+
# Queue worker
41+
queue:
42+
image: ${DOCKER_REPOSITORY}
43+
volumes:
44+
- /var/www/html/storage
45+
networks:
46+
- network
47+
deploy:
48+
replicas: 1
49+
placement:
50+
constraints:
51+
- node.labels.tag == web
52+
restart_policy:
53+
condition: on-failure
54+
4055
# MySQL
4156
db:
4257
networks:

build/docker-compose.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ services:
2222

2323
# Scheduler
2424
scheduler:
25-
user: www-data
25+
user: webdev
2626
environment:
2727
- CONTAINER_ROLE=scheduler
2828
- DB_HOST=db
@@ -31,6 +31,17 @@ services:
3131
- REDIS_PORT=6379
3232
env_file: ../.env
3333

34+
# Queue worker
35+
queue:
36+
user: webdev
37+
environment:
38+
- CONTAINER_ROLE=queue
39+
- DB_HOST=db
40+
- DB_PORT=3306
41+
- REDIS_HOST=redis
42+
- REDIS_PORT=6379
43+
env_file: ../.env
44+
3445
# MySQL
3546
db:
3647
image: mysql:5.7

build/docker-entrypoint

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ if [ "$role" = "app" ]; then
1414
exec apache2-foreground
1515

1616
elif [ "$role" = "queue" ]; then
17-
echo "Running queue worker..."
18-
php artisan queue:work --verbose --tries=1 --timeout=90
17+
echo "Starting Supervisor..."
18+
supervisord -n
1919

2020
elif [ "$role" = "scheduler" ]; then
2121
echo "Running scheduler..."

config/supervisor/conf.d/horizon.conf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[program:horizon]
2+
process_name=%(program_name)s_%(process_num)02d
3+
command=php /var/www/html/artisan horizon
4+
autostart=true
5+
autorestart=true
6+
user=webdev
7+
redirect_stderr=true
8+
stdout_logfile=NONE

config/supervisor/supervisord.conf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
; supervisor config file
2+
3+
[unix_http_server]
4+
file=/tmp/supervisor.sock ; (the path to the socket file)
5+
chmod=0700 ; sockef file mode (default 0700)
6+
chown=webdev:webdev
7+
8+
[supervisord]
9+
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
10+
pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
11+
childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP)
12+
user=webdev
13+
14+
; the below section must remain in the config file for RPC
15+
; (supervisorctl/web interface) to work, additional interfaces may be
16+
; added by defining them in separate rpcinterface: sections
17+
[rpcinterface:supervisor]
18+
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
19+
20+
[supervisorctl]
21+
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
22+
23+
; The [include] section can just contain the "files" setting. This
24+
; setting can list multiple files (separated by whitespace or
25+
; newlines). It can also contain wildcards. The filenames are
26+
; interpreted as relative to this file. Included files *cannot*
27+
; include files themselves.
28+
29+
[include]
30+
files = /etc/supervisor/conf.d/*.conf

dock

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
set -e
44
source .env
55

6+
APP_USER="webdev"
67
BUILD_DIR="build/"
78

89
DOCKERFILE="${BUILD_DIR}/Dockerfile"
@@ -20,6 +21,9 @@ if [ $# -gt 0 ]; then
2021
elif [ "$1" == "scheduler" ]; then
2122
${COMPOSE} up --build scheduler
2223

24+
elif [ "$1" == "queue" ]; then
25+
${COMPOSE} up --build queue
26+
2327
elif [ "$1" == "down" ]; then
2428
${COMPOSE} down
2529

@@ -43,7 +47,7 @@ if [ $# -gt 0 ]; then
4347
echo -n "Uploading deployment configuration..."
4448
ssh ${DEPLOY_SERVER} "mkdir -p ${APP_ID}"
4549
scp -r $0 ${BUILD_DIR} ${DEPLOY_SERVER}:~/${APP_ID} > /dev/null && echo "OK"
46-
ssh ${DEPLOY_SERVER} "cd ${APP_ID}; docker-compose -f ${COMPOSE_BASE} -f ${COMPOSE_PROD} config 2>/dev/null | docker stack deploy --with-registry-auth --prune --compose-file - ${APP_ID}"
50+
ssh ${DEPLOY_SERVER} "cd ${APP_ID}; (docker login && docker-compose -f ${COMPOSE_BASE} -f ${COMPOSE_PROD} config 2>/dev/null | docker stack deploy --with-registry-auth --prune --compose-file - ${APP_ID}) || echo \"Login to registry failed\""
4751
fi
4852

4953
elif [ "$1" == "deploy-migrations" ]; then
@@ -59,17 +63,17 @@ if [ $# -gt 0 ]; then
5963
elif [ "$1" == "exec" ]; then
6064
shift 1
6165
ARGS="$@"
62-
${COMPOSE} exec app bash -c "$ARGS"
66+
${COMPOSE} exec --user ${APP_USER} app bash -c "$ARGS"
6367

6468
elif [ "$1" == "test" ]; then
6569
shift 1
6670
ARGS="$@"
67-
${COMPOSE} exec app bash -c "vendor/bin/phpunit $ARGS"
71+
${COMPOSE} exec --user ${APP_USER} app bash -c "vendor/bin/phpunit $ARGS"
6872

6973
elif [ "$1" == "artisan" ]; then
7074
shift 1
7175
ARGS="$@"
72-
${COMPOSE} exec app bash -c "php artisan $ARGS"
76+
${COMPOSE} exec --user ${APP_USER} app bash -c "php artisan $ARGS"
7377

7478
else
7579
${COMPOSE} "$@"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use Laravel\Horizon\Horizon;
6+
use Illuminate\Support\Facades\Gate;
7+
use Laravel\Horizon\HorizonApplicationServiceProvider;
8+
9+
class HorizonServiceProvider extends HorizonApplicationServiceProvider
10+
{
11+
/**
12+
* Bootstrap any application services.
13+
*
14+
* @return void
15+
*/
16+
public function boot()
17+
{
18+
parent::boot();
19+
20+
// Horizon::routeSmsNotificationsTo('15556667777');
21+
// Horizon::routeMailNotificationsTo('example@example.com');
22+
// Horizon::routeSlackNotificationsTo('slack-webhook-url', '#channel');
23+
}
24+
25+
/**
26+
* Register the Horizon gate.
27+
*
28+
* This gate determines who can access Horizon in non-local environments.
29+
*
30+
* @return void
31+
*/
32+
pro DFE4 tected function gate()
33+
{
34+
Gate::define('viewHorizon', function ($user) {
35+
return in_array($user->email, [
36+
//
37+
]);
38+
});
39+
}
40+
41+
/**
42+
* Register any application services.
43+
*
44+
* @return void
45+
*/
46+
public function register()
47+
{
48+
//
49+
}
50+
}

src/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"php": "^7.1.3",
1212
"fideloper/proxy": "^4.0",
1313
"laravel/framework": "5.7.*",
14+
"laravel/horizon": "^2.0",
1415
"laravel/tinker": "^1.0"
1516
},
1617
"require-dev": {

0 commit comments

Comments
 (0)
0