Dewan159
Dewan159

Reputation: 3084

Is there a way to check if Laravel schedule is running?

I am maintaining many copies of one application (built on Laravel) on different hosts, and I am looking for a way to check if Laravel Schedule is actually running for each. Does Laravel offer something for that?

Upvotes: 5

Views: 15601

Answers (2)

Rolf
Rolf

Reputation: 638

I had the same problem and fixed it by using an option in my commands:

class SomeCommand extends Command
{
    protected $signature = 'some:command {--other-option} {--schedule}';

    public function handle()
    {
        if ($this->option('schedule')) {
            // this command is triggered by the schedule
        }
    }
}

In your app/Console/Kernel.php:

class Kernel extends ConsoleKernel
{
    /**
     * @param \Illuminate\Console\Scheduling\Schedule $schedule
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command(SomeCommand::class, ['--schedule'])->hourly();
    }

    protected function commands()
    {
        $this->load(__DIR__ . '/Commands');
    }
}

Upvotes: 6

Travis Britz
Travis Britz

Reputation: 5552

No, there is nothing built into the framework that will tell you which hosts are running schedule:run. You will have to implement that yourself.

If you have a central system tracking the hosts, then you can make a command which sends a heartbeat ping to your central system, and add that command to the scheduler.

If you have some logic that depends on knowing whether its current host is running a scheduler, then you can make a scheduled command which writes the current time to a file on the local disk (writing to the cache also works if it's not shared between hosts, or the key is unique to the host). Then you simply check whether ($now - $last_write) > $schedule_interval is true.

Upvotes: 1

Related Questions