Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Short Schedule Laravel Package

spatie/laravel-short-schedule

Run Laravel Artisan commands at sub-minute intervals (every second or even 0.5s). Adds a short-scheduler powered by a ReactPHP event loop, running separately from schedule:run so high-frequency tasks don’t block or get delayed.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Sub-minute scheduling: Fills a critical gap in Laravel’s native scheduler (which is limited to 1-minute granularity). Ideal for real-time systems (e.g., IoT telemetry, live analytics, or high-frequency data processing).
  • Event-loop architecture: Leverages ReactPHP for non-blocking execution, ensuring low-latency task dispatch. This aligns well with modern PHP applications using async workflows (e.g., queues, WebSockets).
  • Process isolation: Each command runs in a separate process via Symfony\Component\Process, preventing memory leaks or blocking the scheduler loop. This is a major advantage over Laravel’s native scheduler, which runs tasks sequentially in the foreground.
  • Constraints system: Supports time-based (between), environment-specific (environments), and conditional (when) constraints, enabling fine-grained control over execution.

Integration Feasibility

  • Laravel-native: Designed for Laravel (v9–13), with minimal boilerplate. Integrates seamlessly into Console/Kernel.php via the shortSchedule() method or routes/console.php via the ShortSchedule facade.
  • Dependency compatibility:
    • Requires PHP 8.3+ (aligns with Laravel 11/12/13).
    • Uses ReactPHP (lightweight, no heavy dependencies).
    • No conflicts with Laravel’s built-in scheduler (runs as a separate process).
  • Artisan command support: Works with both custom Artisan commands and raw shell commands (exec()), broadening use cases (e.g., calling external APIs or scripts).

Technical Risk

  • ReactPHP dependency: While lightweight, ReactPHP may introduce complexity for teams unfamiliar with event loops. Requires understanding of non-blocking I/O.
  • Process management: Running tasks as separate processes adds overhead (memory, CPU) and requires Supervisor or similar tools for process monitoring. Misconfiguration could lead to orphaned processes.
  • Memory leaks: Long-running tasks or unmanaged processes may leak memory. Mitigated by the --lifetime flag (e.g., --lifetime=60), but requires proactive monitoring.
  • Event listener blocking: Events (e.g., ShortScheduledTaskStarting) run in the loop and can delay other tasks if slow. Critical: Offload heavy logic to queues.
  • Testing complexity: ReactPHP-based loops require specialized testing (e.g., mocking timers). Spatie provides testing guidance, but teams may need additional tooling (e.g., spatie/reactphp-test).

Key Questions

  1. Use Case Justification:
    • Is sub-minute scheduling absolutely necessary, or could queues (e.g., Laravel Queues with delay) suffice?
    • Example: For real-time analytics, this is ideal. For batch jobs, queues may be overkill.
  2. Infrastructure Readiness:
    • Is Supervisor (or equivalent) already in place to manage long-running processes?
    • Can the team handle process isolation and memory management?
  3. Performance Impact:
    • How many tasks will run concurrently? ReactPHP handles this well, but extreme volumes may require tuning.
    • Will tasks be CPU-intensive? If so, ensure they’re offloaded to workers/queues.
  4. Observability:
    • Are there tools (e.g., Prometheus, Datadog) to monitor process health, execution times, and failures?
  5. Fallback Strategy:
    • If the scheduler fails, how will critical tasks be handled? (e.g., alerts, manual triggers)
  6. Team Expertise:
    • Does the team have experience with ReactPHP or event-driven architectures? If not, budget for ramp-up time.

Integration Approach

Stack Fit

  • Laravel Ecosystem: Perfect fit for Laravel apps needing high-frequency task execution (e.g., real-time data pipelines, cron-like but finer-grained).
  • PHP 8.3+: Requires modern PHP, but aligns with Laravel’s latest LTS versions (11/12/13).
  • Async-Friendly: Complements Laravel’s queue system, WebSocket integrations, or other async workflows.
  • Microservices: Useful in distributed systems where tasks must run on specific servers (onOneServer()).

Migration Path

  1. Assessment Phase:
    • Audit existing scheduled tasks to identify candidates for sub-minute scheduling.
    • Example: Replace a 1-minute queue:work poller with a 10-second short-schedule task.
  2. Pilot Integration:
    • Install in a staging environment:
      composer require spatie/laravel-short-schedule
      
    • Configure shortSchedule() in app/Console/Kernel.php:
      protected function shortSchedule(ShortSchedule $shortSchedule) {
          $shortSchedule->command('telemetry:process')->everySeconds(5);
      }
      
    • Test with Supervisor (sample config below):
      [program:short-schedule]
      command=php artisan short-schedule:run --lifetime=300
      autostart=true
      autorestart=true
      user=www-data
      numprocs=1
      
  3. Phased Rollout:
    • Start with non-critical tasks (e.g., logging, analytics).
    • Gradually migrate high-priority tasks, monitoring for:
      • Process stability (no crashes/orphaned tasks).
      • Performance impact (CPU/memory usage).
      • Task execution accuracy (timing consistency).
  4. Deprecation of Legacy Scheduler:
    • Once all sub-minute tasks are migrated, disable Laravel’s native scheduler for those tasks (or keep it for minute+ granularity).

Compatibility

  • Laravel Versions: Officially supports 9–13 (as of v1.7.0). Drop Laravel 10/11 support in newer versions.
  • Artisan Commands: Works with any Artisan command, including:
    • Custom commands (e.g., php artisan my:task).
    • Shell commands (e.g., ShortSchedule::exec('bash script.sh')).
  • Environment Constraints: Supports environments() to restrict tasks to specific environments (e.g., production only).
  • Maintenance Mode: Respects Laravel’s maintenance mode unless overridden (runInMaintenanceMode()).

Sequencing

  1. Initial Setup:
    • Install package → Configure Supervisor → Test in staging.
  2. Task Definition:
    • Define schedules in shortSchedule() (preferred) or routes/console.php.
    • Use constraints (between, when, environments) to refine execution.
  3. Process Management:
    • Start the scheduler:
      php artisan short-schedule:run
      
    • Monitor with Supervisor (or PM2, systemd).
  4. Scaling:
    • Add more tasks incrementally, adjusting --lifetime if memory is a concern.
    • For high-volume tasks, consider queueing event listeners or heavy constraints.

Operational Impact

Maintenance

  • Process Lifecycle Management:
    • Requires Supervisor (or equivalent) to restart crashed processes and manage lifetimes.
    • --lifetime flag helps mitigate memory leaks but adds operational overhead (e.g., logging restarts).
  • Configuration Drift:
    • Changes to schedules require restarting the scheduler (supervisorctl restart short-schedule).
    • Document this in runbooks to avoid forgotten restarts.
  • Logging:
    • Tasks run as separate processes, so logs may be harder to trace. Use structured logging (e.g., Monolog) with process IDs.
    • Example: Log the PID of spawned processes for debugging:
      ShortSchedule::command('my:task')->everySecond()->listen(function ($event) {
          logger()->info("Task started (PID: {$event->process->getPid()})");
      });
      

Support

  • Debugging Challenges:
    • Process Isolation: Debugging a crashed task requires inspecting its logs (e.g., journalctl for systemd, Supervisor logs).
    • Event Loop Blocking: Slow constraints or listeners delay all tasks. Critical: Profile these with Xdebug or Blackfire.
  • Common Issues:
    • Overlapping Tasks: Use withoutOverlapping() to prevent concurrent runs.
    • Environment Mismatches: Verify environments() constraints are applied correctly.
    • PHP Path Issues: Ensure the PHP binary path is correct (fixed in v1.6.4).
  • Support Resources:
    • Spatie’s documentation and videos are comprehensive.
    • GitHub issues are responsive (e.g., #96 for PHP path fixes).

Scaling

  • Horizontal Scaling:
    • The scheduler runs as a **
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai