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
## 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).

  1. Define Schedules: Add schedules in app/Console/Kernel.php:

    protected function shortSchedule(ShortSchedule $shortSchedule)
    {
        $shortSchedule->command('your-command')->everySecond();
    }
    
  2. Run the Scheduler: Start the scheduler in production:

    php artisan short-schedule:run
    

    Use a process manager (e.g., Supervisor) to keep it running.

First Use Case

Schedule a command to run every 5 seconds:

ShortSchedule::command('email:send-reminders')->everySeconds(5);

Implementation Patterns

Core Workflow

  1. Define Commands: Use ShortSchedule::command() or pass a command class directly:

    $shortSchedule->command(\App\Console\Commands\ProcessData::class)->everySecond();
    
  2. 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).
  3. 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);
    
  4. Shell Commands: Schedule shell commands with exec():

    ShortSchedule::exec('php artisan optimize:clear')->everyHour();
    

Integration Tips

  • 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.


Gotchas and Tips

Pitfalls

  1. 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();
    
  2. Maintenance Mode: Commands do not run in maintenance mode unless explicitly allowed:

    ShortSchedule::command('critical-task')->everySecond()->runInMaintenanceMode();
    
  3. Environment Constraints: Ensure the correct environment is specified to avoid unintended execution:

    ShortSchedule::command('deploy')->environments('production')->everySecond();
    
  4. Sub-Second Precision: While supported (everySeconds(0.5)), excessive sub-second scheduling may overwhelm the system. Monitor CPU/memory usage.

  5. Process Isolation: The scheduler runs in a separate process. Avoid shared state between the main Laravel process and the scheduler.

Debugging

  • 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');
    

Extension Points

  1. Custom Constraints: Extend the Constraint interface to add custom logic (e.g., database-based triggers).

  2. Event Listeners: Attach listeners to ShortScheduledTaskStarting to pre-process commands or log metadata.

  3. Worker Lifecycle: Override the ShortScheduleRunCommand to customize the ReactPHP loop or add pre/post hooks.

  4. Lifetime Management: Dynamically adjust the worker lifetime via environment variables or config:

    $lifetime = config('short-schedule.lifetime', 3600);
    

Configuration Quirks

  • 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
    

Performance Tips

  • 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.


---
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