platformsh-templates / laravel

Laravel template for Platform.sh.
23 stars 98 forks source link

Crons are not being run due to the queue:work blocking it #33

Closed matthiaz closed 1 year ago

matthiaz commented 3 years ago

Intro

crons:
    # Run Laravel's scheduler every 5 minutes, which is often as crons can run.
    scheduler:
        spec: '*/5 * * * *'
        cmd: 'echo "testing"; php artisan schedule:run'

app/Console/Kernel.php has a simple:

$schedule->call(function () {
            echo "This is a test every 5 minutes";
        })->everyFiveMinutes();

Expected result

Log result when you happen to deploy it on minute 00 05 10 15 20 25 30 35 40 45 50 55

testing
[2021-08-30 13:30:38.741147] Launching command 'php artisan schedule:run'.
Running scheduled command: Callback
This is a test every 5 minute

Actual failed result However, if you deploy it on any other minute: (or we do a maintenance and move it... )


[2021-08-30 13:36:33.361473] Launching command 'echo "testing"; php artisan schedule:run'.

testing
No scheduled commands are ready to run.

Notice the distinct lack of running the actual scheduled command. This is because Laravel currently assumes that we run a cron every minute, and it therefore assumes that it will encounter 00 05 10 15 ... at some point. (which it doesn't)

Proposal to solve

Make a service provider that overrides the CronExpression and injects our own, that can deal with the shift of minutes.

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class PlatformShServiceProvider extends ServiceProvider
{
    /**
     * All of the container bindings that should be registered.
     *
     * @var array
     */
    public $bindings = [
        CronExpression::class => PlatformShCronExpression::class,
    ];
chadwcarlson commented 2 years ago

From Slack

  schedule-worker:
    size: S
    commands:
      start: |
        php artisan schedule:work
stefandoorn commented 2 years ago

Unfortunately workers only work starting from the Medium plan, which is too much for many projects. The alternative for smaller projects it to use a native cronjob instead of the Laravel scheduler. Might be good to add this in the docs and / or comment inside the yaml template.