拉拉威尔命令每一分钟都不起作用

时间:2022-05-02 02:29:13

Edit: @Bogdan, the script only executed once, which was exactly one hour before the next day would start, which is odd, also because the start time of both appointments on that day were different. Both appointments, however, were set for the next day.

编辑:@Bogdan,脚本只执行了一次,刚好在第二天开始的前一小时,这很奇怪,因为那天两个约会的开始时间都不一样。不过,这两项任命都定于第二天进行。

So basically my script didn't check for upcoming appointments in the next hour, but simply for appointments that were set for tomorrow, and send an email one hour before the day those appointments would start. Confusing. Also, wouldn't the seconds matter if the time was checked in the format Y-m-d H:i? Or am I missing something?

所以基本上我的脚本并没有检查下一个小时的约会,只是简单地检查一下明天的约会,然后在约会开始的前一个小时发一封邮件。让人困惑。而且,如果以Y-m-d:i的格式检查时间,秒不重要吗?还是我漏掉了什么?

OP:

人事处:

I have a few commands set up using the laravel cronjob and scheduler. I have got all commands that I need working, except for one. I have an appointments database where I store a timestamp for when an appointment is due. Using a command, I want to send an e-mail one hour before the appointment is due. For that, I use the following command:

我使用laravel cronjob和调度器设置了一些命令。我有我需要的所有命令,除了一个。我有一个约会数据库,我在其中存储约会到期时的时间戳。使用命令,我想在预约到期前一个小时发一封电子邮件。为此,我使用以下命令:

$schedule->command('appointment:hourly')->everyMinute();

So it runs every minute. In my command I have the following:

所以它每分钟都在运行。我的命令如下:

$date = Carbon::now()->addHours('1')->format('Y-m-d H:i');

$appointments = Appointment::latest('start')->date($date)->get();

So it sets the date to now(), then it adds an hour and checks that value in the database. My e-mails never get send (the e-mail script works since I am using that same script for my other commands).

因此,它将日期设置为now(),然后添加一个小时,并在数据库中检查该值。我的电子邮件永远不会被发送(电子邮件脚本可以工作,因为我对其他命令使用相同的脚本)。

I have tried the following as well:

我也尝试过以下方法:

$date = Carbon::now()->addHours('1')->format('Y-m-d H:i:s');

Still nothing. My dates are stored in the database like so for example:

还是什么都没有。我的日期存储在数据库中,例如:

2016-01-25 09:00:00

So wether or not it also checks to match the seconds, it should work. When I check my $date in my view, it does return 2016-01-25 09:00 for example.

所以无论它是否检查匹配秒,它应该是有效的。当我在视图中查看$date时,它会返回2016-01-25 09:00。

Any pointers will be glady appreciated.

任何建议都会受到欢迎。

Btw, my date() function. It just checks the value against the start column:

顺便说一句,我的日期()函数。它只是对照开始列检查值:

public function scopeDate($query, $date)
{
    if($date)
    {
        return $query->whereDate('start', '=', $date);
    }
    else {
        return false;
    }
}

PS. One thing I should mention. Last night at exactly 11:00 PM, I got two e-mails from the above command, saying an appointment was set to start in an hour from now. Those were appointments that were set to start for today, but they were not set to start at 00:00. So I guess with the above command, it doesn't check for appointments for today. I got confused and haven't been able to figure this out just yet.

还有一件事我要提一下。昨天晚上11点整,我收到了两封来自上面命令的电子邮件,说约会将在一个小时后开始。这些是原定于今天开始的约会,但他们不会在00:00开始。所以我猜上面的命令,它不会检查今天的预约。我很困惑,还没弄明白。

1 个解决方案

#1


1  

There are two things to consider here:

这里有两件事需要考虑:

1. Make sure your have the correct timezone set in your config/app.php file, as that might be the cause of the appointment notification email being sent at the wrong time.

1。确保在配置/应用程序中设置了正确的时区。php文件,因为这可能是在错误的时间发送约会通知邮件的原因。

2. From the time the cron job runs until the Carbon::now() statement is executed, the time might not be exactly what you expect it to be (this is not a certainty and is something that can be affected by what your code does and/or the server load at the time of the execution). So the time might be 09:00:01 instead of 09:00:00, in which case your condition would not be true anymore (this being another reason why it worked sporadically).

2。从cron作业运行直到碳::现在()语句执行,时间可能不是你希望它是什么(这不是确定的,是可以受到什么代码和/或服务器加载的时候执行)。所以时间可能是09:00:01而不是09:00:00,在这种情况下你的情况将不再是真的(这是它偶尔起作用的另一个原因)。

To make sure the time difference doesn't affect the condition you should check if the time is one hour or less from the current time, this would of course require you to set some sort of marker that lets you know if the notification was sent. Also since whereDate only compares the date part of the date and time string, there is the need to handle checking the time as well. Since Eloquent doesn't offer any methods for that, you can use the hour and minute MySQL function in conjunction with whereRaw like so:

为了确保时差不影响您应该检查时间是否比当前时间短一小时或更短的条件,这当然需要您设置某种标记,让您知道是否发送了通知。同样,因为where只比较日期和时间字符串的日期部分,所以也需要处理检查时间。由于口才没有提供任何方法,所以你可以用一个小时和分钟的MySQL函数和这样的地方连接:

$date = Carbon::now()->addHours('1');

$query->where('notified', false)
      ->whereDate('start', '=', $date->format('Y-m-d'))
      ->whereRaw('hour(start) <= ?', [$date->format('H')])
      ->whereRaw('minute(start) <= ?', [$date->format('i')]);

#1


1  

There are two things to consider here:

这里有两件事需要考虑:

1. Make sure your have the correct timezone set in your config/app.php file, as that might be the cause of the appointment notification email being sent at the wrong time.

1。确保在配置/应用程序中设置了正确的时区。php文件,因为这可能是在错误的时间发送约会通知邮件的原因。

2. From the time the cron job runs until the Carbon::now() statement is executed, the time might not be exactly what you expect it to be (this is not a certainty and is something that can be affected by what your code does and/or the server load at the time of the execution). So the time might be 09:00:01 instead of 09:00:00, in which case your condition would not be true anymore (this being another reason why it worked sporadically).

2。从cron作业运行直到碳::现在()语句执行,时间可能不是你希望它是什么(这不是确定的,是可以受到什么代码和/或服务器加载的时候执行)。所以时间可能是09:00:01而不是09:00:00,在这种情况下你的情况将不再是真的(这是它偶尔起作用的另一个原因)。

To make sure the time difference doesn't affect the condition you should check if the time is one hour or less from the current time, this would of course require you to set some sort of marker that lets you know if the notification was sent. Also since whereDate only compares the date part of the date and time string, there is the need to handle checking the time as well. Since Eloquent doesn't offer any methods for that, you can use the hour and minute MySQL function in conjunction with whereRaw like so:

为了确保时差不影响您应该检查时间是否比当前时间短一小时或更短的条件,这当然需要您设置某种标记,让您知道是否发送了通知。同样,因为where只比较日期和时间字符串的日期部分,所以也需要处理检查时间。由于口才没有提供任何方法,所以你可以用一个小时和分钟的MySQL函数和这样的地方连接:

$date = Carbon::now()->addHours('1');

$query->where('notified', false)
      ->whereDate('start', '=', $date->format('Y-m-d'))
      ->whereRaw('hour(start) <= ?', [$date->format('H')])
      ->whereRaw('minute(start) <= ?', [$date->format('i')]);