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

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.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Legacy Use Case: The package is obsolete for new Laravel 11+ projects due to native sub-minute scheduling support. However, it may still be relevant for:
    • Legacy Laravel 9/10 projects (pre-Laravel 11) where native sub-minute scheduling is unavailable.
    • Custom high-frequency workflows requiring granular control (e.g., real-time analytics, IoT telemetry, or financial tickers) where native Laravel’s scheduler lacks precision.
    • Edge cases where constraints (e.g., withoutOverlapping, onOneServer) are needed in older versions.
  • Event-Driven Architecture: The package leverages ReactPHP for non-blocking execution, making it suitable for high-frequency tasks without blocking the main thread. This aligns with microservices or event-driven architectures.
  • Constraint Flexibility: Supports composite constraints (time windows, environment checks, truth tests), which is valuable for conditional high-frequency tasks.

Integration Feasibility

  • Laravel Compatibility:
    • Laravel 9–10: Full support (primary use case).
    • Laravel 11+: Not recommended (native alternative exists).
    • Lumen: Requires manual command copying (minor overhead).
  • Dependency Overhead:
    • Adds ReactPHP (~10MB) and Symfony Process (~500KB), which may be acceptable for high-frequency tasks but could bloat smaller projects.
    • No database or external service dependencies (self-contained).
  • Artisan Integration:
    • Seamlessly integrates with existing Console/Kernel.php via shortSchedule() method.
    • Supports both string commands ('command:name') and class-based commands (resolved from container).

Technical Risk

  • Deprecation Risk: High for new projects (native Laravel 11+ replaces this functionality). Mitigation: Only adopt if maintaining Laravel <11.
  • Performance Impact:
    • Memory Leaks: Requires explicit --lifetime flag to terminate workers (e.g., --lifetime=60). Neglecting this risks OOM crashes.
    • CPU Load: Sub-second tasks (e.g., everySeconds(0.5)) may spike CPU usage. Mitigation: Benchmark under load; consider rate-limiting.
  • Process Management:
    • Requires Supervisor or similar to persist the short-schedule:run process. Risk: Downtime if not configured.
    • Signal Handling: No built-in graceful shutdown (e.g., SIGTERM). Workaround: Use Supervisor’s stopasgroup=true.
  • Testing Complexity:
    • ReactPHP-based loops complicate unit testing. Mitigation: Use Spatie’s provided testing utilities or mock the ShortSchedule facade.

Key Questions

  1. Why not use Laravel 11’s native scheduler?
    • If targeting Laravel 11+, this package is unnecessary. Native support is more maintainable.
  2. What’s the expected frequency and volume?
    • Sub-second tasks (e.g., everySeconds(0.1)) may overwhelm the system. Validate with load tests.
  3. How will memory leaks be managed?
    • Will --lifetime be set in production? What’s the acceptable trade-off between uptime and memory usage?
  4. Is process isolation required?
    • For critical systems, consider running the scheduler in a dedicated container or VM to isolate failures.
  5. How will failures be monitored?
    • Supervisor logs are critical. Will alerts be configured for process crashes or high error rates?
  6. Are there existing constraints that native Laravel doesn’t support?
    • E.g., withoutOverlapping or onOneServer may justify adoption even in Laravel 11+.

Integration Approach

Stack Fit

  • Laravel 9/10 Projects: Native fit. Replace or augment existing scheduler.
  • Laravel 11+ Projects: Avoid. Use schedule()->command()->everyMinute() with runInBackground() for sub-minute precision.
  • Non-Laravel PHP: Not recommended. Designed for Laravel’s Artisan ecosystem.
  • Microservices: Good fit if the service is Laravel-based and requires high-frequency internal tasks (e.g., cache invalidation, real-time data processing).

Migration Path

  1. Assess Laravel Version:
    • Laravel 11+: Skip. Use native scheduling.
    • Laravel 9/10: Proceed.
  2. Installation:
    composer require spatie/laravel-short-schedule
    
  3. Configure Supervisor: Add to /etc/supervisor/conf.d/laravel-short-schedule.conf:
    [program:laravel-short-schedule]
    process_name=%(program_name)s_%(process_num)02d
    command=php /path/to/artisan short-schedule:run --lifetime=60
    autostart=true
    autorestart=true
    user=www-data
    numprocs=1
    redirect_stderr=true
    stdout_logfile=/var/log/laravel-short-schedule.log
    
  4. Update Console Kernel:
    // app/Console/Kernel.php
    protected function shortSchedule(ShortSchedule $shortSchedule) {
        $shortSchedule->command('analytics:process')->everySeconds(10);
        $shortSchedule->command('cache:invalidate')->everySeconds(0.5)->withoutOverlapping();
    }
    
  5. Test Locally:
    • Run php artisan short-schedule:run manually to verify tasks.
    • Use withoutOverlapping cautiously—test for race conditions.
  6. Deploy:
    • Restart Supervisor: supervisorctl reread && supervisorctl update && supervisorctl restart laravel-short-schedule.
    • Monitor logs for errors.

Compatibility

  • Artisan Commands: Works with any Artisan command, including custom ones.
  • Shell Commands: Supports ShortSchedule::exec() for bash scripts (e.g., exec('php script.php')).
  • Environment Constraints: Respects .env (e.g., environments(['production'])).
  • Lumen: Requires manual command copying (see README). Overhead: ~5 minutes for setup.

Sequencing

  1. Critical Tasks First:
    • Schedule high-priority tasks (e.g., everySecond()) before lower-frequency ones to avoid queueing delays.
  2. Overlap Mitigation:
    • Use withoutOverlapping for long-running commands (e.g., database migrations).
  3. Time Windows:
    • Constrain tasks to business hours (e.g., between('09:00', '17:00')) to reduce unnecessary load.
  4. Phased Rollout:
    • Start with a single task (e.g., everySeconds(30)) and monitor before adding more.

Operational Impact

Maintenance

  • Dependency Updates:
    • ReactPHP/Symfony Process updates may introduce breaking changes. Monitor Spatie’s GitHub.
  • Configuration Drift:
    • Changes to shortSchedule() require Supervisor restarts. Mitigation: Use feature flags or blue-green deployments.
  • Logging:
    • Logs are written to the file specified in Supervisor. Recommendation: Centralize logs (e.g., ELK stack) for observability.
  • Documentation:
    • Internal docs should include:
      • Supervisor config template.
      • --lifetime settings for each environment.
      • Task dependencies (e.g., "Task A must run before Task B").

Support

  • Troubleshooting:
    • Common Issues:
      • Tasks not running: Check Supervisor logs (/var/log/laravel-short-schedule.log) for crashes or permission errors.
      • High CPU: Verify task duration and adjust frequency (e.g., everySeconds(1) instead of everySeconds(0.1)).
      • Memory leaks: Confirm --lifetime is set and Supervisor is restarting processes.
    • Debugging Tools:
      • php artisan short-schedule:run --verbose for real-time output.
      • Events (ShortScheduledTaskStarting) for observability.
  • SLA Impact:
    • Sub-second tasks may violate SLAs if not monitored. Mitigation: Set up alerts for:
      • Process downtime (>5 minutes).
      • Task execution failures (e.g., command exit code ≠ 0).
  • Vendor Lock-in:
    • Low. The package is MIT-licensed and open-source. Migration to native Laravel 11+ is straightforward.

Scaling

  • Horizontal Scaling:
    • Not recommended for onOneServer tasks (single-server lock). For distributed setups:
      • Use withoutOverlapping + external locking (e.g., Redis).
      • Or switch to Laravel Queues for distributed task execution.
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.
redaxo/debug
redaxo/test
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder