简体   繁体   中英

Laravel command every minute doesn't work

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.

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 Ymd H:i ? Or am I missing something?

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:

$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).

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.

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.

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.

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).

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:

$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')]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM