Hello,
I would like my Laravel application to execute a function present in one of my controllers at certain times of the day, before that I wanted to do a test.
So in: app\Console\Kernel.php I did:
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Http\Controllers\LuccaController;
class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule.
*/
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
$luccaController = new LuccaController();
$luccaController->testSchedule();
})->everyTwoSeconds();
}
/**
* Register the commands for the application.
*/
protected function commands(): void
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
And here is the function in my controller :
public function testSchedule()
{
error_log("Function has been called correctly !");
}
It works well but for a few seconds after a while it stops by itself:
myproject> php artisan schedule:run
2024-02-26 11:13:43 Running [Callback] Function has been called correctly !
.................................................................................................. 2ms DONE
2024-02-26 11:13:45 Running [Callback] Function has been called correctly !
.................................................................................................. 0ms DONE
2024-02-26 11:13:47 Running [Callback] Function has been called correctly !
.................................................................................................. 0ms DONE
2024-02-26 11:13:49 Running [Callback] Function has been called correctly !
.................................................................................................. 1ms DONE
2024-02-26 11:13:51 Running [Callback] Function has been called correctly !
.................................................................................................. 1ms DONE
2024-02-26 11:13:53 Running [Callback] Function has been called correctly !
.................................................................................................. 0ms DONE
2024-02-26 11:13:55 Running [Callback] Function has been called correctly !
.................................................................................................. 0ms DONE
2024-02-26 11:13:57 Running [Callback] Function has been called correctly !
.................................................................................................. 1ms DONE
2024-02-26 11:13:59 Running [Callback] Function has been called correctly !
.................................................................................................. 1ms DONE
And it stopped here, I have to re run it.
It's the same with everyMinute(); but it only run once.
Do you have any idea why ? Thx !
.. just don't manually instantiate a controller