- How does Spatie’s ShortSchedule differ from Laravel’s native scheduler for sub-minute tasks?
- Laravel’s native scheduler runs tasks sequentially with 1-minute granularity, which can cause delays or blockages. ShortSchedule uses ReactPHP’s event loop to run tasks asynchronously in separate processes, enabling true sub-minute execution (e.g., every 0.5 seconds) without impacting your main scheduler.
- Can I use ShortSchedule with Laravel 9 or do I need the latest version?
- ShortSchedule supports Laravel 9 through 13. However, it requires PHP 8.1+ (Laravel 9) or PHP 8.3+ (Laravel 11–13). Always check the package’s [Packagist page](https://packagist.org/packages/spatie/laravel-short-schedule) for version-specific requirements before installing.
- Will running tasks every second or faster block my Laravel application?
- No, ShortSchedule runs tasks in separate processes via ReactPHP, so they won’t block your main Laravel application or the native `schedule:run` command. Each task executes independently, ensuring high-frequency jobs don’t interfere with other processes.
- How do I monitor or log tasks scheduled with ShortSchedule?
- ShortSchedule emits events (`ShortScheduledTaskStarting` and `ShortScheduledTaskStarted`) that you can listen to for logging or monitoring. Use Laravel’s event system or tools like Laravel Horizon to track task execution. For process-level monitoring, integrate with Supervisor or PM2.
- Can I restrict ShortSchedule tasks to run only on specific servers in a cluster?
- Yes, use the `onOneServer()` constraint to ensure tasks run on only one server in a multi-server environment. This prevents duplicate executions and conflicts. Combine it with `when()` or `between()` for additional control over task timing.
- What happens if a scheduled task takes longer than its interval (e.g., a task runs every 2 seconds but takes 3 seconds)?
- ShortSchedule launches each task in a separate process, so the next scheduled execution won’t be delayed by a slow task. However, if the task’s logic is blocking (e.g., slow `when()` conditions), it could delay the ReactPHP event loop. Offload heavy work to queues or use the `--lifetime` flag to manage process memory.
- Do I need to manually manage processes like Supervisor for ShortSchedule?
- Yes, ShortSchedule runs as a long-lived ReactPHP process, so you’ll need a process manager like Supervisor, PM2, or systemd to keep it running in production. Configure it to auto-restart the `short-schedule:run` command if it crashes, ensuring reliability.
- Can I schedule shell commands (not just Artisan) with ShortSchedule?
- Absolutely. Use `ShortSchedule::exec('bash-script')` to run arbitrary shell commands at sub-minute intervals. This is useful for integrating with external tools or scripts that aren’t Laravel Artisan commands.
- How do I test ShortSchedule in CI/CD where timing is unpredictable?
- Mock ReactPHP timers in your tests using libraries like `react/event-loop` or `mockery` to simulate sub-second intervals. Focus on verifying task execution logic rather than precise timing. For integration tests, use the `--lifetime` flag to limit process runtime.
- Are there alternatives to ShortSchedule for high-frequency Laravel tasks?
- Alternatives include custom cron jobs with sub-minute granularity (e.g., `* * * * * *` in cron) or queue-based solutions like Laravel Queues with delayed jobs. However, these often lack the non-blocking, event-driven design of ShortSchedule, which is optimized for Laravel’s ecosystem and ReactPHP’s efficiency.