Laravel Artisan:"schedule:run"如何工作? [英] Laravel Artisan: How does `schedule:run` work?

查看:258
本文介绍了Laravel Artisan:"schedule:run"如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个虚拟的Command作业,其 handle()函数如下:

 公共函数handle(){$ this-> line('==================');$ this-> line('Running my job at'.Carbon :: now());$ this-> line('结束我在'的工作.Carbon :: now());} 

如您所见,它实际上什么也没做,只是将几行信息返回到标准输出.

现在,在我的 App \ Console \ Kernel 类中,我已经设置了以下时间表:

 受保护的功能计划(计划$ schedule){时间表->命令('cbh:dummyCommand')->每一分钟()->appendOutputTo(storage_path().'/logs/laravel_output.log');} 

现在,从命令行运行 php artisan schedule:run .我的 laravel_output.log 文件中的输出为

  ==================在2018-02-08开始工作在2018-02-08结束我的工作 

到目前为止,一切都很好.看来我的日程安排正在运行.但是,如果我在同一分钟内再次运行该命令,则日志文件现在显示为:

  ==================在2018-02-08开始工作在2018-02-08结束我的工作==================在2018-02-08开始工作在2018-02-08 11:01:51结束我的工作 

换句话说,日程安排似乎比每分钟运行的频率更高,在我看来,这违反了我在日程安排中定义的规则.

更令人困惑的是,我可以将时间表更改为每5分钟而不是每分钟运行一次:

 受保护的功能计划(计划$ schedule){时间表->命令('cbh:dummyCommand')->everyFiveMinutes()->appendOutputTo(storage_path().'/logs/laravel_output.log');} 

然后运行 php artisan schedule:run ,然后得到以下输出

没有计划的命令可以运行.

我可以等待任意多的时间(即超过5分钟),但日志文件仍然没有输出.

使用Windows Task Scheduler计划命令时,我观察到完全相同的行为(是的,我的开发环境是Windows 7框,是的,这等效于Windows的cron-job).

问题

那是怎么回事? artisan schedule:run 命令如何确定要在计划中执行的命令正在等待"?我曾想过会有某种日志文件记录"Command X处于1个小时的时间表,最后一次运行在09:00的事实,所以不要在10:00之前再次执行它",但我找不到此类日志的踪迹.

有人可以给我一个线索吗?

谢谢!

解决方案

我知道,回答您自己的问题并不很酷.无论如何,让我们想象一下这是我的时间表:

 受保护的功能计划(计划$ schedule){时间表->命令('cbh:dummyCommand')->everyFiveMinutes()->appendOutputTo('/my/logs/laravel_output.log');} 

我发现此代码未将您的作业设置为每5分钟运行一次.如果命令在5分钟之前运行,也不会阻止该命令再次运行.

一种更好的思考方式是,该代码将命名命令设置为在当前时间的分钟数字为 0 5 ".换句话说,如果我在 11:04 上运行命令行参数: php artisan schedule:run ,则响应为:

 #没有计划的命令可以运行. 

但是,如果我在 11:00 11:05 上运行相同的命令,则会得到:

 #运行计划的命令:php artisan cbh:dummyCommand>>/my/logs/laravel_output.log 2>& 1 

最后,我在日志文件中输出了结果.

当我的 everyFiveMinutes()计划基于我的任务计划程序每2分钟运行一次的事实,每10分钟在文件中创建一个日志时,我发现了上述问题.

I have a dummy Command job set up, whose handle() function is as follows:

public function handle()
{
    $this->line('==================');
    $this->line('Running my job at ' . Carbon::now());
    $this->line('Ending my job at ' . Carbon::now());
}

As you see, it doesn't actually do anything but return a few lines of info to the standard output.

Now, in my App\Console\Kernel class, I have set up the following schedule:

protected function schedule(Schedule $schedule)
{
    $schedule
        -> command('cbh:dummyCommand')
        -> everyMinute()
        -> appendOutputTo (storage_path().'/logs/laravel_output.log');
}

Now, from the command-line I run php artisan schedule:run. The output in my laravel_output.log file reads

==================
Running my job at 2018-02-08 11:01:33
Ending my job at 2018-02-08 11:01:33

So far so good. It seems that my schedule is running. However, if I run the command again within the same minute, my logfile now reads:

==================
Running my job at 2018-02-08 11:01:33
Ending my job at 2018-02-08 11:01:33
==================
Running my job at 2018-02-08 11:01:51
Ending my job at 2018-02-08 11:01:51

In other words, the schedule appears to be running more frequently than every minute, which appears to me to break the rules I defined in my schedule.

What's more confusing is that I can change the schedule to run every 5 minutes instead of every minute:

protected function schedule(Schedule $schedule)
{
    $schedule
        -> command('cbh:dummyCommand')
        -> everyFiveMinutes()
        -> appendOutputTo (storage_path().'/logs/laravel_output.log');
}

then run php artisan schedule:run, then I get the following output

No scheduled commands are ready to run.

I can wait as long as you like (i.e. more than 5 minutes) and still I get no output to my log file.

I observe exactly the same behaviour when I schedule my command with Windows Task Scheduler (yes, my development environment is a Windows 7 box, and yes, this is the Windows equivalent of a cron-job).

The Question

So what's going on? How does the artisan schedule:run command figure out which commands are "waiting" on the schedule to be executed? I had imagined that there would be some kind of log-file to record the fact that "Command X is on a 1-hour schedule and last ran at 09:00, so don't execute it again before 10:00", but I have been able to find no trace of such a log.

Can someone give me a clue?

Thanks!!

解决方案

Not cool to answer your own question, I know. Anyhow, let's imagine this is my schedule:

protected function schedule(Schedule $schedule)
{
    $schedule
        -> command('cbh:dummyCommand')
        -> everyFiveMinutes()
        -> appendOutputTo ('/my/logs/laravel_output.log');
}

What I've discovered is that this code doesn't set your job to run every 5 minutes. Nor does it prevent the command running again if it was run less than 5-minutes ago.

A better way to think about it is that this code sets the named command "to be runnable every time the minute-figure of the current time is 0 or 5". In other words, if I run the command-line argument: php artisan schedule:run at 11:04, then the response is:

# No scheduled commands are ready to run.

But if I run the same command at 11:00 or 11:05, then we get:

# Running scheduled command: php artisan cbh:dummyCommand >> /my/logs/laravel_output.log 2>&1

And I end up with output in my log-file.

I discovered the above when my everyFiveMinutes() schedule was creating a log in my file every 10 minutes based on the fact that my task-scheduler was running every 2 minutes.

这篇关于Laravel Artisan:"schedule:run"如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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