When i use php artisan schedule:run
command the shuffleQuoteOfTheDay
method is evaluated once and then it does not fire it every minute.
Kernel@schedule method
protected function schedule(Schedule $schedule)
{
$schedule->call('App\Services\MotivationalQuoteService@shuffleQuoteOfTheDay')->everyMinute();
}
MotivationalQuoteService@shuffleQuoteOfTheDay method
public function shuffleQuoteOfTheDay(){
$currentQuoteOfADay = $this->getQuoteOfTheDay();
$randomQuote = MotivationalQuote::where('quote_of_the_day',false)->inRandomOrder()->first();
$currentQuoteOfADay->update(['quote_of_the_day' => false]);
$randomQuote->update(['quote_of_the_day' => true]);
return $randomQuote;
}
It does work once but not every minute, i have created jobs table.Can someone help me with this?Thank you in advance.
When it's time to fire a scheduled event, Laravel's schedule manager calls the run () method on the Illuminate\Console\Scheduling\Event object representing that event, this happens inside the Illuminate\Console\Scheduling\ScheduleRunCommand.
If you do not know how to add Cron entries to your server, consider using a service such as Laravel Forge which can manage the Cron entries for you: This Cron will call the Laravel command scheduler every minute. When the schedule:run command is executed, Laravel will evaluate your scheduled tasks and runs the tasks that are due.
This command will cause a sequence of two commands to run one after another but in the background, so after your update:coupons command finishes a schedule:finish command will run given the Mutex of the current event, using this ID Laravel locates the event and runs its after-callbacks.
Laravel executes any before-callbacks, sends the command to the OS, and finally executes any after-callbacks.
See Task Scheduling:
Here is the only Cron entry you need to add to your server:
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
This Cron will call the Laravel command scheduler every minute. Then, Laravel evaluates your scheduled tasks and runs the tasks that are due.
Laravel's task scheduler does not stay in memory, it needs to be run every minute. It will then check which tasks need to be run in that minute and run them. When you run the task scheduler using PHP it just runs once, it needs cron to run it every minute.
If your dev machine is a linux machine/homestead then you could test this lcoally.
To add a cron job:
$ crontab -e
then add * * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
to the crontab file and save it.
Do not forget to start/reload cron service.
On ubuntu:
$ sudo service cron reload
You need to setup cron
so it could run the php artisan schedule:run
command every minute.
From the docs:
When using the scheduler, you only need to add the following Cron entry to your server. If you do not know how to add Cron entries to your server, consider using a service such as Laravel Forge which can manage the Cron entries for you:
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
This Cron will call the Laravel command scheduler every minute. When the schedule:run command is executed, Laravel will evaluate your scheduled tasks and runs the tasks that are due.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With