Laravel 5 schedule job every 30 seconds

我们两清 提交于 2019-12-01 13:57:28

I solved it with this code:

public function handle()
{
    $count = 0;
    while ($count < 59) {
        $startTime =  Carbon::now();

        $this->travelsRequestsDriversRepository->beFreeRequestAfterTime();

        $endTime = Carbon::now();
        $totalDuration = $endTime->diffInSeconds($startTime);
        if($totalDuration > 0) {
            $count +=  $totalDuration;
        }
        else {
            $count++;
        }
        sleep(1);
    }


}

and in cron table add command of this.

* * * * * /usr/local/php56/bin/php56 /home/hamshahr/domains/hamshahrapp.com/project/artisan taxis:beFreeTaxis 1>> /dev/null 2>&1

It's not working like Alexey Mezenin suggested, but he gave me an idea where to test some solutions.

You can add loop with timer not to

protected function schedule(Schedule $schedule)

but direct into the command / event or other what you want to loop.

For example, I was running a command, so inside the command I added:

public function handle()
{
        \App\Investment::calculate();
        sleep(30);
        \App\Investment::calculate();
}

And now my command works every 30 seconds.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!