spatie/laravel-short-schedule
Schedule Laravel Artisan commands at sub-minute intervals (every second or even fractions). Define short schedules via ShortSchedule facade or Console Kernel. Note: Laravel now supports sub-minute scheduling, so this package is largely unnecessary.
## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require spatie/laravel-short-schedule
Register the service provider in config/app.php (automatically done via package discovery).
Define Schedules:
Add schedules in app/Console/Kernel.php:
protected function shortSchedule(ShortSchedule $shortSchedule)
{
$shortSchedule->command('your-command')->everySecond();
}
Run the Scheduler: Start the scheduler in production:
php artisan short-schedule:run
Use a process manager (e.g., Supervisor) to keep it running.
Schedule a command to run every 5 seconds:
ShortSchedule::command('email:send-reminders')->everySeconds(5);
Define Commands:
Use ShortSchedule::command() or pass a command class directly:
$shortSchedule->command(\App\Console\Commands\ProcessData::class)->everySecond();
Frequency Control:
everySecond(): Runs every 1 second.everySeconds(30): Runs every 30 seconds.everySeconds(0.5): Sub-second precision (e.g., every 0.5 seconds).Constraints: Combine constraints for granular control:
ShortSchedule::command('sync-data')
->between('09:00', '17:00')
->environments(['staging', 'production'])
->when(fn() => config('features.sync_enabled'))
->everySeconds(10);
Shell Commands:
Schedule shell commands with exec():
ShortSchedule::exec('php artisan optimize:clear')->everyHour();
Process Management:
Use Supervisor to manage the short-schedule:run process. Example config:
[program:short-schedule]
command=php /path/to/artisan short-schedule:run
autostart=true
autorestart=true
user=www-data
numprocs=1
Memory Leak Handling: Set a lifetime to restart the worker periodically:
php artisan short-schedule:run --lifetime=3600 # Restart every hour
Lumen Support:
Copy ShortScheduleRunCommand to app/Console/Commands/ and update the namespace.
Overlapping Execution:
By default, commands run even if the previous invocation is still running. Use withoutOverlapping() to prevent this:
ShortSchedule::command('long-running-task')->everySecond()->withoutOverlapping();
Maintenance Mode: Commands do not run in maintenance mode unless explicitly allowed:
ShortSchedule::command('critical-task')->everySecond()->runInMaintenanceMode();
Environment Constraints: Ensure the correct environment is specified to avoid unintended execution:
ShortSchedule::command('deploy')->environments('production')->everySecond();
Sub-Second Precision:
While supported (everySeconds(0.5)), excessive sub-second scheduling may overwhelm the system. Monitor CPU/memory usage.
Process Isolation: The scheduler runs in a separate process. Avoid shared state between the main Laravel process and the scheduler.
Logs:
Check storage/logs/laravel.log for scheduler errors or use --verbose:
php artisan short-schedule:run --verbose
Events:
Listen to ShortScheduledTaskStarting/ShortScheduledTaskStarted for debugging:
ShortScheduledTaskStarting::dispatch($command, $process);
Testing:
Use ShortSchedule::fake() in tests to assert command execution:
ShortSchedule::fake();
$this->artisan('short-schedule:run');
ShortSchedule::assertRan('your-command');
Custom Constraints:
Extend the Constraint interface to add custom logic (e.g., database-based triggers).
Event Listeners:
Attach listeners to ShortScheduledTaskStarting to pre-process commands or log metadata.
Worker Lifecycle:
Override the ShortScheduleRunCommand to customize the ReactPHP loop or add pre/post hooks.
Lifetime Management: Dynamically adjust the worker lifetime via environment variables or config:
$lifetime = config('short-schedule.lifetime', 3600);
Lifetime Default:
No default lifetime is set in production (avoids unexpected restarts). Always specify --lifetime explicitly if needed.
PHP Binary Path:
Ensure the PHP binary path in ShortScheduleRunCommand matches your environment (e.g., /usr/bin/env php for Docker).
Timezone:
The scheduler uses the system timezone. Set TZ environment variable if needed:
TZ=America/New_York php artisan short-schedule:run
Batch Commands: Group related commands into a single Artisan command to reduce overhead:
ShortSchedule::command('batch:process')->everySeconds(10);
Avoid Blocking: Offload heavy tasks to queues and trigger them via the scheduler:
ShortSchedule::command('queue:work')->everySecond()->withoutOverlapping();
Monitoring:
Use tools like htop or glances to monitor CPU/memory usage of the scheduler process.
---
How can I help you explore Laravel packages today?