我如何在Laravel 5中以秒的方式使用调度程序 [英] How can i use scheduler in seconds manner in Laravel 5.#

查看:555
本文介绍了我如何在Laravel 5中以秒的方式使用调度程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以幼稚的方式安排一些任务,但是我发现很难每20秒安排一次任务.检查laravel文档.对于 laravel时间表,它几秒钟没有这种方法.虽然有 -> cron('* * * * * *')按照自定义的Cron时间表运行任务",但这也无法解决我的问题.

there i am trying to schedule some task in larave, but i find it hard to schedule my task in every 20 seconds. Checking laravel doc. for laravel schedular, it didn't have such method for seconds. While there is ->cron('* * * * * *') "Run the task on a custom Cron schedule", but it also couldn't solve my problem.

下面是我在kernal.php文件中用于调度的代码.

Below is my code in kernal.php file for scheduling.

protected function schedule(Schedule $schedule)
{ 
    // need to replace everyMinute() by some other method which can run it every 20 sec

    $schedule->call('path\to\controller@method)->everyMinute();

   // also tried cron() but didn't workout.
    $schedule->call('path\to\controller@method)->cron(*/20 * * * * *);
}    

谢谢.

推荐答案

没有任何一种方法可以为您提供开箱即用的功能,但是有一些与您处境相同的人的建议.

There's no method that will do this for you out of the box, however there are a few suggestions of people who have been in the same situation as you already.

->cron(*/20 * * * * *)实际上是说每20分钟运行一次,而不是每20秒运行一次.鉴于cron不会降低到一分钟的分辨率,这可能就是Laravel也不支持它的原因.

->cron(*/20 * * * * *) is actually saying run this every 20 minutes, not every 20 seconds. Given that cron doesn't go down to a sub-minute resolution this is probably why Laravel doesn't support it either.

如果确实需要每30秒运行一次,那么我将在此处结合使用这两种方法:按计划安排Laravel:每秒执行一次命令

If this really needs to run more than every 30 seconds then I would go for a combination of the two here: Laravel 5 schedule job every 30 seconds and Laravel schedular: execute a command every second

也就是说,您仍然应该每分钟调用一次命令,但是为了安全起见添加withoutOverlapping()方法.

That is to say, you should still call your command every minute, but add the withoutOverlapping() method just to be safe.

然后在命令中,您可以运行代码,休眠20秒钟,再次运行,休眠20秒钟,然后再次运行.显然,这是在您的代码几乎可以立即运行的前提下进行的. 例如,如果您的代码需要5秒钟才能运行,然后您休眠20秒钟然后再次运行,则您的代码实际上每25秒钟运行一次.

Then inside your command you can run your code, sleep for 20 seconds, run it again, sleep for 20 seconds, and then run it again. This obviously works on the premise that your code is almost instantaneous to run. For example, if your code takes 5 seconds to run and then you sleep for 20 seconds and then run it again - your code is actually running every 25 seconds.

我想您对这段代码要花多长时间有一个很好的了解.如果不是,请在运行artisan命令以获取想法时对其进行手动计时-这样的事情可能对date && php artisan your:command && date有帮助. 这将在您的shell中输出日期,运行命令,然后再次输出日期-这包括您可以用来查看运行多少秒的时间.

I imagine you have a fairly good idea of how long this code takes to run. If not, time it manually whilst running the artisan command to get an idea - something like this might help date && php artisan your:command && date. This will output the date in your shell, run the command then output the date again - this includes the time which you can use to see how many seconds it takes to run.

这将需要您将控制器动作重构为命令,或将sleep()代码添加到控制器动作.工匠命令的文档应该在这里有所帮助.

This will require you to either refactor the controller action out to a command or add the sleep() code to your controller action. The docs for artisan commands should help here.

我建议将其重构为命令,但这取决于您.

I would suggest refactoring this into a command, but it's up to you.

//Console command
public function handle()
{
    \App\Investment::calculate(); //takes 2 seconds to run
    sleep(18); //sleep for 18 seconds to ensure we are only running the command every 20 seconds
    \App\Investment::calculate(); //takes 2 seconds to run
    sleep(18);
    \App\Investment::calculate(); //takes 2 seconds to run
}


//The console scheduler - Kernel.php  
protected function schedule(Schedule $schedule)
{     
    $schedule->call('path\to\controller@method)->everyMinute()->withoutOverlapping();
}   

这篇关于我如何在Laravel 5中以秒的方式使用调度程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆