Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel schedule fire only once on initialization

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.

like image 725
Jan Ciołek Avatar asked Jan 22 '18 11:01

Jan Ciołek


People also ask

How do I fire a scheduled event in Laravel?

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.

How to manage cron entries in Laravel?

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.

What is the use of update and finish command in Laravel?

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.

What is Laravel before and after callbacks?

Laravel executes any before-callbacks, sends the command to the OS, and finally executes any after-callbacks.


2 Answers

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
like image 64
Sapnesh Naik Avatar answered Oct 03 '22 00:10

Sapnesh Naik


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.

like image 33
Alexey Mezenin Avatar answered Oct 02 '22 23:10

Alexey Mezenin