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 Cronless Schedule Laravel Package

spatie/laravel-cronless-schedule

Run Laravel’s scheduler without cron. This dev/test-friendly package adds php artisan schedule:run-cronless, which uses a ReactPHP loop to execute the scheduler on a timer (default every minute), with optional custom frequency and manual runs.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Use Case Alignment: Ideal for test environments, CI/CD pipelines, or serverless/ephemeral deployments where cron is unavailable or impractical (e.g., Docker containers, AWS Lambda, or local development).
  • Laravel Native Compatibility: Directly replaces cron-triggered schedule:run with a ReactPHP-based loop, maintaining 100% compatibility with Laravel’s scheduling syntax (e.g., Schedule::command()->everyMinute()).
  • Non-Blocking Design: Uses ReactPHP’s event loop to simulate cron’s periodic execution without blocking the PHP process, enabling concurrent HTTP requests (critical for shared hosting or serverless).
  • Limitation: Not a drop-in replacement for production cron (e.g., lacks persistence across process restarts, no distributed task coordination).

Integration Feasibility

  • Low Friction: Zero changes to existing schedule definitions; only requires replacing * * * * * php artisan schedule:run with php artisan schedule:run-cronless.
  • Dependency: Introduces ReactPHP (lightweight, ~1MB), which may require minor PHP extensions (e.g., ext-pcntl for process management in some environments).
  • CI/CD Friendly: Perfect for GitHub Actions, GitLab CI, or CircleCI where cron is unavailable. Example:
    # .github/workflows/schedule.yml
    jobs:
      run-scheduler:
        runs-on: ubuntu-latest
        steps:
          - run: php artisan schedule:run-cronless &
          - run: sleep 60  # Simulate 1 minute
    

Technical Risk

  • Process Longevity: The command runs indefinitely; SIGTERM handling must be tested in CI/CD (e.g., Kubernetes pods, serverless containers).
  • Concurrency: ReactPHP loop may conflict with other async PHP processes (e.g., Laravel Horizon). Test under load if using shared hosting.
  • Time Accuracy: ReactPHP’s timer precision (~1s drift) is less reliable than cron (sub-second accuracy). Mitigate by:
    • Using sleep(60) in tests instead of relying on exact timing.
    • Avoiding time-sensitive schedules (e.g., high-frequency trading).
  • Debugging: Logs may be harder to trace than cron’s cron.log. Instrument with Laravel’s logging or Spatie\CronlessSchedule\CronlessSchedule::log().

Key Questions

  1. Environment Scope:
    • Is this for local dev, CI/CD, or production? (Production use is not recommended.)
  2. Process Management:
    • How will the process be terminated/restarted? (e.g., PM2, Docker restart: unless-stopped, or Kubernetes livenessProbe?)
  3. Concurrency:
    • Are other async PHP processes (e.g., queues, HTTP servers) running concurrently?
  4. Testing:
    • How will you verify schedule execution in tests? (e.g., mock React\EventLoop\LoopInterface?)
  5. Alternatives:
    • For production, consider Laravel Forge, Envoyer, or AWS EventBridge instead of cronless.

Integration Approach

Stack Fit

Stack Component Compatibility Notes
Laravel Version 8.x–11.x (tested) Check composer.json for exact version constraints.
PHP Version 8.0+ ReactPHP 1.0+ required.
Web Server N/A (CLI-only) No impact on Apache/Nginx.
CI/CD GitHub Actions, GitLab CI, CircleCI, etc. Ideal for ephemeral environments.
Serverless AWS Lambda, Cloudflare Workers (with PHP runtime) Use Lambda’s timeout settings carefully (60s+ for long-running tasks).
Containerized Docker, Kubernetes Use command: ["php", "artisan", "schedule:run-cronless"] in Dockerfile.
Shared Hosting cPanel, Heroku (with PHP CLI access) Avoid if pcntl or ReactPHP is blocked.

Migration Path

  1. Replace Cron Entry:

    • Remove * * * * * php artisan schedule:run from crontab.
    • Add to package.json (for Node.js-based setups):
      "scripts": {
        "schedule": "php artisan schedule:run-cronless"
      }
      
    • Or use a systemd service (for Linux):
      [Service]
      ExecStart=/usr/bin/php /path/to/artisan schedule:run-cronless
      Restart=always
      
  2. Test Locally:

    • Run in a terminal: php artisan schedule:run-cronless.
    • Verify schedules execute via Tinker:
      php artisan tinker
      >>> \Spatie\CronlessSchedule\CronlessSchedule::getLastRunTime();
      
  3. CI/CD Integration:

    • Add to workflow (example for GitHub Actions):
      - name: Run scheduler
        run: php artisan schedule:run-cronless &
        timeout-minutes: 1
      
  4. Production (If Absolutely Necessary):

    • Not recommended, but if used:
      • Run in a separate process (e.g., PM2, Supervisor).
      • Monitor with htop/docker stats for CPU/memory spikes.
      • Set up health checks to restart on failure.

Compatibility

  • Fully Compatible With:
    • Laravel’s native Schedule facade.
    • All schedule syntax (e.g., ->hourly(), ->cron(), ->command()).
    • Laravel’s logging and queue systems.
  • Potential Conflicts:
    • Queue Workers: Running schedule:run-cronless alongside queue:work may cause race conditions. Solution: Use separate processes.
    • Timezone Issues: Ensure config/app.php timezone matches server timezone.
    • Database Locks: Heavy schedules may cause deadlocks. Solution: Test with DB::transactionLevel(0).

Sequencing

  1. Development:
    • Replace cron with schedule:run-cronless immediately for local testing.
  2. Testing:
    • Mock React\EventLoop\LoopInterface in PHPUnit for unit tests:
      $loop = Mockery::mock('React\EventLoop\LoopInterface');
      $loop->shouldReceive('addPeriodicTimer')->andReturn(true);
      $this->app->instance('reactphp', $loop);
      
  3. Staging:
    • Deploy with cronless in parallel to cron, then switch.
  4. Production:
    • Avoid. If used, phase out cron only after validating all schedules work.

Operational Impact

Maintenance

  • Pros:
    • No cron management: Eliminates server-specific cron syntax issues (e.g., * * * * * vs. 0 * * * *).
    • Portable: Works across environments without configuration changes.
  • Cons:
    • Process Management Overhead: Requires monitoring/restart logic (e.g., Kubernetes livenessProbe).
    • Dependency Updates: ReactPHP and Spatie packages must be kept updated.
    • Logging: Less standardized than cron’s cron.log. Solution: Extend CronlessSchedule to log to Laravel’s channels.

Support

  • Debugging:
    • Use php artisan schedule:list to verify tasks.
    • Check logs with tail -f storage/logs/laravel.log.
    • Common Issues:
      • Process crashes silently (add set_time_limit(0) if needed).
      • Time drift accumulates (use NTP to sync clocks).
  • Vendor Support:
    • Spatie provides GitHub issues and Slack community support.
    • No official SLA; community-driven.

Scaling

  • Horizontal Scaling:
    • Not supported. Cronless is single-process; multiple instances may duplicate work.
    • Workaround: Use Laravel Queues for distributed tasks instead.
  • Vertical Scaling:
    • Lightweight (~10MB memory usage). Monitor with memory_get_usage().
  • Performance:
    • No impact on HTTP requests (ReactPHP runs in background).
    • Heavy schedules may cause CPU spikes. Mitigation:
      • Offload to queues: Schedule::command('php artisan heavy:task')->onOneServer()->everyMinute().

Failure Modes

Failure Scenario Impact Mitigation
Process crashes Missed schedules Use PM2/Super
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
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