0

Laravel Task Scheduling has options like, everyMinute() which will run a command every minute. I want to know what is the time it actually starts executing. Is it when I run the server or any specific second of the minute?
I am trying to run a function depending on the time difference. Here's a pseudocode

Run myCustomFunction() if diffInMin(now, customTime) <= 1

I thought it will run 1 time but it ran twice every time.

Rifat Bin Reza
  • 2,601
  • 2
  • 14
  • 29
  • 1
    The scheduler runs via a cron task every minute so `everyMinute` executes when that happens. It's up to the OS to determine when that is. It's usually pretty accurate on the 0 second mark of that minute (on the OSs I tired it on) but may be off depending on server load – apokryfos Apr 29 '21 at 06:31
  • I logged the task handler and it shows it executes mostly at :03/:04 second of the minute. Not so accurate as I was running on my local machine. But I think you're right about it running at the beginning of the minute – Rifat Bin Reza Apr 29 '21 at 06:41

1 Answers1

1

The scheduler usually runs every minute right around the zero secound mark based on the server's current time as @apokryfos mentioned.

Assuming the customTime is a fixed DateTime, what makes you think the code you wrote will only run once?

  • When now() === customTime the diffInMin() would be zero so the condition diffInMin(now, customTime) <= 1 will evaluate to true.
  • The next minute, the diffInMin() would be 1, so the condition diffInMin(now, customTime) <= 1 will still evaluate to true.
P. K. Tharindu
  • 2,565
  • 3
  • 17
  • 34
  • Thanks for pointing the minute different mistake. Could you tell me a better way to make sure the function runs once? – Rifat Bin Reza Apr 29 '21 at 06:52
  • Can't really tell without knowing the full context. But since you said it is a command, adding something like `$schedule->command('name')->everyMinute();` in the `App\Console\Kernel` should be enough. – P. K. Tharindu Apr 29 '21 at 07:01