Laravel Docker Queue
We have a containerized app stack using laravel 5.6, nginx, redis, supervisor and mysql.
docker-compose.yml
version: '2'
services:
# Database service
db:
image: 'mysql:5.7.22'
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=root
volumes:
- './database:/var/lib/mysql'
ports:
- '3309:3306'
networks:
- mysql_net
# Cache service
redis:
image: 'redis:alpine'
command: 'redis-server --requirepass password'
volumes:
- './redis:/data'
networks:
- redis_net
# API Source Code
php:
image: ...
volumes:
- './src:/srv/src'
networks:
- nginx_net
- mysql_net
- redis_net
depends_on:
- db
- redis
# SUPERVISOR Queue worker
queue:
image: ...
volumes:
- './src:/srv/src'
networks:
- mysql_net
- redis_net
depends_on:
- db
- redis
# CRON Task Scheduler
cron:
image: ...
volumes:
- './src:/srv/src'
networks:
- redis_net
- mysql_net
depends_on:
- db
# NGINX Web Server
web:
image: ...
volumes:
- './src:/srv/src'
ports:
- '88:80'
networks:
- nginx_net
depends_on:
- php
networks:
nginx_net:
driver: bridge
mysql_net:
driver: bridge
redis_net:
driver: bridge
The cron service only handles the processing of the scheduled tasks
Problem
$schedule->command('email:notifications specific_time')
->everyMinute()
->withoutOverlapping(2)
->onOneServer();
The above task simply runs a query to check if there were any new items were imported and triggers an email to the user, which gets queued.
When scaling the cron service to multiple instances the task runs on all instances and the user receives as many emails as there are instances.
According to the Laravel documentation chaining the onOneServer() method on the scheduler tasks should prevent this behaviour. This is not the case for us, any help will be appreciated.
Please or to participate in this conversation.