Overview
NutriFit uses Laravel’s Task Scheduler to automate recurring tasks such as sending appointment reminders. This eliminates the need to configure individual cron jobs for each task.Task Scheduler Configuration
Current Scheduled Tasks
Tasks are defined inroutes/console.php:
routes/console.php
| Task | Schedule | Command | Purpose |
|---|---|---|---|
| Appointment Reminders | Daily at 9:00 AM | appointments:send-reminders | Sends email reminders 24 hours before appointments |
Server Cron Configuration
Single Cron Entry
Laravel’s scheduler requires only one cron entry on your server. This entry runs every minute and Laravel determines which tasks to execute.Production Setup (Linux/Unix)
-
Open your server’s crontab:
-
Add this single line (adjust path to your NutriFit installation):
- Save and exit. The scheduler will now run every minute.
Development Testing
Test scheduled tasks without waiting for the cron schedule:Schedule Frequency Options
Laravel provides multiple scheduling options beyonddailyAt():
Time-Based Schedules
Day-Specific Schedules
Adding New Scheduled Tasks
Example: Daily Database Cleanup
-
Create the command:
-
Define the command in
app/Console/Commands/CleanupOldData.php: -
Schedule it in
routes/console.php:
Production Configuration
Using Supervisor (Recommended)
While cron handles scheduled tasks, use Supervisor to ensure queue workers run continuously:Install Supervisor
Create Supervisor Configuration
File:/etc/supervisor/conf.d/nutrifit-worker.conf
Start Supervisor
Monitor Queue Workers
Monitoring Scheduled Tasks
View Scheduled Tasks List
Task Logging
Add logging to scheduled tasks:Email Task Output
Receive email notifications when tasks complete:Task Constraints
Prevent Overlaps
Ensure a task doesn’t run if the previous execution is still active:Run in Maintenance Mode
Allow tasks to run even when the application is in maintenance mode:Conditional Execution
Run tasks only when conditions are met:Development Environment
Local Development
During development, you typically don’t need to set up cron. Instead:-
Run tasks manually:
-
Or test commands directly:
Alternative: Laravel Schedule Monitor
For advanced monitoring, consider packages like spatie/laravel-schedule-monitor.Troubleshooting
Tasks Not Running
-
Verify cron is configured:
Should show the
* * * * *entry. -
Check scheduler is working:
-
Verify task is registered:
-
Check file permissions:
Cron Logs
View cron execution logs:Scheduler Output
Capture output for debugging:Best Practices
Use Single Cron Entry
Use Single Cron Entry
Never create multiple cron entries for individual Laravel commands. Use Laravel’s scheduler and one cron entry.
Monitor Task Execution
Monitor Task Execution
Use logging and monitoring to track successful and failed task executions.
Prevent Overlapping
Prevent Overlapping
Use
withoutOverlapping() for long-running tasks to prevent concurrent executions.Restart Queue Workers After Deployments
Restart Queue Workers After Deployments
Always restart queue workers after code deployments:
Related Documentation
Artisan Commands
Available Artisan commands and usage
Database Backups
Automated backup strategies