The document numbering system uses Laravel queues to automatically assign document numbers when documents are created. This guide explains how to set up and maintain the queue system.
When a document is created (Matrix, Activity, NonTravelMemo, etc.), the HasDocumentNumber trait automatically dispatches an AssignDocumentNumberJob:
// This happens automatically when creating any document
$matrix = Matrix::create([...]);
// Job is automatically dispatched to assign document number
The job runs in the background and:
For development and testing:
# Start queue worker (runs until stopped)
php artisan queue:work
# Process one job and stop
php artisan queue:work --once
# Process with specific options
php artisan queue:work --tries=3 --timeout=60 --memory=512
For production servers, you have two approaches:
Use Supervisor for queue workers and cron for scheduled commands:
# Quick setup
cd /var/www/html/staff/apm
./setup-supervisor-cron.sh
Use Supervisor for both queue workers and scheduled commands:
# Ubuntu/Debian
sudo apt-get install supervisor
# CentOS/RHEL
sudo yum install supervisor
# Run the automated setup script
cd /var/www/html/staff/apm
./setup-supervisor.sh
Queue Worker Configuration:
sudo nano /etc/supervisor/conf.d/laravel-worker.conf
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/staff/apm/artisan queue:work --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
numprocs=2
redirect_stderr=true
stdout_logfile=/var/www/html/staff/apm/storage/logs/worker.log
stopwaitsecs=3600
Scheduler Configuration:
sudo nano /etc/supervisor/conf.d/laravel-scheduler.conf
[program:laravel-scheduler]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/staff/apm/artisan schedule:run --verbose
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/html/staff/apm/storage/logs/scheduler.log
stopwaitsecs=60
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*
sudo supervisorctl start laravel-scheduler:*
Create a systemd service for the queue worker:
sudo nano /etc/systemd/system/laravel-worker.service
Add this content:
[Unit]
Description=Laravel Queue Worker
After=network.target
[Service]
User=www-data
Group=www-data
Restart=always
ExecStart=/usr/bin/php /opt/homebrew/var/www/staff/apm/artisan queue:work --sleep=3 --tries=3 --max-time=3600
WorkingDirectory=/opt/homebrew/var/www/staff/apm
[Install]
WantedBy=multi-user.target
sudo systemctl enable laravel-worker
sudo systemctl start laravel-worker
sudo systemctl status laravel-worker
# Use the monitoring script (recommended)
./monitor-supervisor.sh
# Or check manually
sudo supervisorctl status
# Monitor jobs in real-time
php artisan monitor:document-jobs --watch
# Check once
php artisan monitor:document-jobs
# Create test documents
php artisan test:document-creation --count=5
# Check if jobs are being processed
php artisan queue:work --once --verbose
# Check queue size
php artisan queue:size
# Clear all jobs
php artisan queue:clear
# Retry failed jobs
php artisan queue:retry all
# Check failed jobs
php artisan queue:failed
ps aux | grep "queue:work"
php artisan config:show queue
php artisan tinker
>>> DB::connection()->getPdo();
php artisan queue:failed
php artisan queue:retry [job-id]
php artisan queue:retry all
php artisan queue:work --memory=512
php artisan queue:work --max-time=3600
php artisan queue:work --queue=high,default
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /opt/homebrew/var/www/staff/apm/artisan queue:work --sleep=3 --tries=3 --max-time=3600 --memory=512
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
numprocs=4
redirect_stderr=true
stdout_logfile=/opt/homebrew/var/www/staff/apm/storage/logs/worker.log
stopwaitsecs=3600
# /etc/logrotate.d/laravel-worker
/opt/homebrew/var/www/staff/apm/storage/logs/worker.log {
daily
missingok
rotate 14
compress
notifempty
create 644 www-data www-data
postrotate
supervisorctl restart laravel-worker:*
endscript
}
#!/bin/bash
# /opt/homebrew/var/www/staff/apm/monitor-queue.sh
QUEUE_SIZE=$(php /opt/homebrew/var/www/staff/apm/artisan queue:size)
FAILED_JOBS=$(php /opt/homebrew/var/www/staff/apm/artisan queue:failed | wc -l)
if [ $QUEUE_SIZE -gt 100 ]; then
echo "ALERT: Queue size is $QUEUE_SIZE"
fi
if [ $FAILED_JOBS -gt 10 ]; then
echo "ALERT: $FAILED_JOBS failed jobs"
fi
php artisan test:document-creation --count=10
php artisan monitor:document-jobs --watch
php artisan tinker
>>> App\Models\Matrix::whereNotNull('document_number')->count()
If you encounter issues:
tail -f storage/logs/worker.logphp artisan monitor:document-jobsphp artisan queue:failedsudo supervisorctl restart laravel-worker:*