Architecture Overview
The scheduled message system works as follows:- Cron executes
php artisan schedule:runevery minute - Laravel Scheduler triggers the
scheduled-messages:processcommand - Command finds messages where
next_send_at <= now() - Job dispatches
SendScheduledMessageto the Redis queue - Worker (managed by Supervisor):
- Sends the payload to Discord API
- For recurring messages: calculates next send time
- For one-time messages: marks as completed
- Deletes attachment files after sending
Queue Worker Setup (Supervisor)
1. Install Supervisor
If not already installed:2. Create Worker Configuration
Create a Supervisor configuration file:Configuration Breakdown
command
The queue worker command with options:
queue:work redis: Process jobs from Redis queue--queue=default: Listen to the default queue--sleep=3: Sleep 3 seconds when no jobs available--tries=3: Retry failed jobs up to 3 times--max-time=3600: Restart worker after 1 hour (prevents memory leaks)
numprocs
Number of worker processes (2 recommended for production)
Increase to 4-8 workers for high-volume deployments.
3. Update Supervisor
Load the new configuration and start workers:4. Verify Workers Are Running
If workers show
FATAL or EXITED status, check the worker log for errors:Laravel Scheduler Setup (Cron)
1. Understanding the Scheduler
Laravel’s scheduler is a single cron entry that runs every minute. It then internally manages all scheduled tasks defined in the application.2. Configure Cron
Edit the crontab for thewww-data user:
Cron Entry Breakdown
* * * * *: Run every minutecd /var/www/discord-webhook-manager/app: Change to application directoryphp artisan schedule:run: Execute Laravel scheduler>> /dev/null 2>&1: Suppress output (logged by Laravel instead)
3. Verify Cron is Active
Check the crontab was saved:4. Test the Scheduler
Manually run the scheduler to test:Worker Management Commands
Supervisor Commands
Laravel Queue Commands
Monitoring and Maintenance
1. Check Worker Logs
Monitor worker activity:2. Check Laravel Logs
View application logs:3. Monitor Queue Size
Check pending jobs in Redis:4. Monitor System Resources
Check resource usage:Updating Worker Configuration
When you change the Supervisor configuration:Deployment Updates
After deploying code updates that affect queue jobs:The
queue:restart command signals workers to restart gracefully after finishing current jobs.Alternative: Database Queue
If Redis is unavailable, you can use database-backed queues:1. Update Environment
2. Update Supervisor Configuration
3. Create Jobs Table
Troubleshooting
Workers Not Processing Jobs
-
Check workers are running:
-
Check worker logs:
-
Verify queue connection:
-
Test Redis connection:
Scheduler Not Running
-
Verify cron is configured:
-
Check cron logs:
-
Manually test scheduler:
High Memory Usage
If workers consume too much memory:-
Reduce max-time:
-
Add memory limit:
-
Restart workers more frequently:
Failed Jobs Accumulating
-
View failed jobs:
-
Retry specific job:
-
Retry all failed jobs:
-
Clear failed jobs:
Performance Tuning
Increase Workers for High Volume
For high-traffic deployments:Multiple Queue Priorities
Create separate workers for priority queues:Optimize Redis
Tune Redis for queue performance:Production Checklist
Before going live, verify:- Supervisor is installed and enabled
- Workers are running (
supervisorctl status) - Cron is configured for
www-datauser - Scheduler runs successfully (
schedule:run) - Worker logs are being written
- Redis connection is working
- Queue jobs process correctly
- Failed jobs retry mechanism works
- Workers restart after code deploys
Next Steps
- Monitor worker logs regularly
- Set up alerting for worker failures
- Configure log rotation for worker logs
- Plan for scaling workers based on load
- Review Redis migration guide for optimization
