Remove a laravel sheduling task

Viewed 2828

I use laravel's command and task scheduling to check shared online 3D printers's status. I use below code to start the scheduling task to check the device status every 10 minutes. But I want to remove the scheduling task when the printer is off line.

$schedule->command(DeviceCheckCommand::class, ['--force'])->everyTenMinutes();

I've read the laravel doc Task Scheduling entirely, but I cannot find any info about how to remove a scheduling task.

1 Answers

You better save printer's status in a file or database and check for the status before executing another command:

Inside DeviceCheckCommand.php:

public function handle()
{
    // Your code

    if ($printerStatus == 'Offline') {
        Storage::put('PrinterIsOffline.info', 'Yes');
    } else {
       Storage::delete('PrinterIsOffline.info');
    }
}

Inside Kernel.php:

protected function schedule(Schedule $schedule)
{
    if (!Storage::exists('PrinterIsOffline.info')) {
        $schedule->command(DeviceCheckCommand::class, ['--force'])->everyTenMinutes();
    }
}
Related