我正在使用Laravel计划来运行cron作业。
在我的crontab中,我添加了(使用正确的项目路径):
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
现在我的App \ Console \ Kernel.php看起来像:
protected function schedule(Schedule $schedule)
{
$schedule->command('investments:calculate_interests')->everyMinute();
$schedule->call('\App\Http\Controllers\UsersController@testEvent')->everyMinute();
}
像这样使用它我每分钟都成功地运行工作。但我怎么能改变它来运行它们每10或20或30秒?
我用这段代码解决了它:
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);
}
}
并在cron表中添加此命令。
* * * * * /usr/local/php56/bin/php56 /home/hamshahr/domains/hamshahrapp.com/project/artisan taxis:beFreeTaxis 1>> /dev/null 2>&1
它不像Alexey Mezenin建议的那样工作,但他给了我一个想法,在哪里测试一些解决方案。
您可以添加不带计时器的循环
protected function schedule(Schedule $schedule)
但是直接进入命令/事件或其他你要循环的东西。
例如,我正在运行一个命令,所以在命令中我添加了:
public function handle()
{
\App\Investment::calculate();
sleep(30);
\App\Investment::calculate();
}
现在我的命令每30秒就会运行一次。