gpt4 book ai didi

laravel-5.2 - 可以使用 Laravel 每 X 秒运行一次任务吗?

转载 作者:行者123 更新时间:2023-12-01 11:27:16 25 4
gpt4 key购买 nike

大家好,我已经阅读了 Laravel 文档调度的章节,但是我没有找到太多信息。我正在尝试自定义任务的执行以使其每 30 秒运行一次。有解决办法吗?对不起我的英语,祝你有美好的一天。

最佳答案

默认情况下,CRON 任务的安排时间最短为 1 分钟,但您的问题有解决方法。

在控制台内核中你应该这样做:

/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('your_command:run', ['--delay'=> 0])->everyMinute();
$schedule->command('your_command:run', ['--delay'=> 30])->everyMinute();
}

在您的 Command 类中,您应该定义可以采用延迟参数的 $signature 变量。

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'your_command:run {--delay= : Number of seconds to delay command}';

在handle方法中,你应该读取这个参数的值并使用内置的sleep()函数让这个任务休眠特定的秒数。

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// code
sleep(intval($this->option('delay')));
}

此解决方案将每 30 秒运行一次您的任务,您可以将不同延迟的任务数相乘。在这种情况下,您需要在 Kernel 类中编辑 schedule 方法。

关于laravel-5.2 - 可以使用 Laravel 每 X 秒运行一次任务吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36354181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com