Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save mdutt247/85753339b2b67c332a82dacb27496ba1 to your computer and use it in GitHub Desktop.

Select an option

Save mdutt247/85753339b2b67c332a82dacb27496ba1 to your computer and use it in GitHub Desktop.
For QUEUE_CONNECTION=database on production, you must run a queue worker continuously. Otherwise password reset emails, queued notifications, etc. will sit in the jobs table and never send.

Laravel Database Queue Setup for Production

When using:

QUEUE_CONNECTION=database

on production, you must run a queue worker continuously. Otherwise queued jobs such as password reset emails, notifications, and other background tasks will remain in the jobs table and never be processed.


1. Configure .env

QUEUE_CONNECTION=database

MAIL_MAILER=smtp
MAIL_HOST=your-smtp-host
MAIL_PORT=587
MAIL_USERNAME=your-username
MAIL_PASSWORD=your-password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=no-reply@yourdomain.com
MAIL_FROM_NAME="${APP_NAME}"

2. Ensure Queue Tables Exist

Run the following on production:

php artisan queue:table
php artisan queue:failed-table
php artisan migrate

If the migrations already exist, Laravel may report that they have already been created. That is expected and safe to ignore.


3. Clear and Rebuild Caches

After updating .env, refresh Laravel's cached configuration:

php artisan optimize:clear
php artisan config:cache
php artisan route:cache
php artisan view:cache

4. Test the Queue Worker

For a quick manual test, start a worker:

php artisan queue:work database --tries=3 --timeout=90

Leave the terminal running and test a password reset email. If the email is sent successfully, the queue is working.


5. Run the Queue Worker Permanently

For production deployments, use Supervisor to keep the worker running.

Create a Supervisor configuration file:

sudo nano /etc/supervisor/conf.d/books-queue.conf

Add:

[program:project-name-queue]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/project/artisan queue:work database --sleep=3 --tries=3 --timeout=90
autostart=true
autorestart=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile=/path/to/project/storage/logs/queue-worker.log

Reload Supervisor:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start books-queue:*

6. Deployment Checklist

After every deployment:

php artisan migrate --force
php artisan optimize:clear
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan queue:restart

queue:restart is important because it signals existing workers to gracefully restart and load the latest application code.


7. Monitor Failed Jobs

View failed jobs:

php artisan queue:failed

Retry all failed jobs:

php artisan queue:retry all

Notes

For applications using QUEUE_CONNECTION=database, password reset emails and other queued mail notifications depend on an active queue worker. If no worker is running, jobs will accumulate in the jobs table and will not be processed until a worker starts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment