-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[10.x] Sub-minute Scheduling #47279
[10.x] Sub-minute Scheduling #47279
Conversation
This would be an awesome addition. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Look good to me.
I'm coding like crazy and I see this PR. "Wow!" I think, "Sub-minute task scheduling, just what I need!". I'm impressed with the cleanliness and organization of the code, it's clear it's a great contribution. However, as a software engineer, a few potential improvements and alternative solution suggestions come to mind. Look, when tasks are repeated very frequently, this system can cause some performance issues. Running a task every second, especially for heavy-duty operations, can require high processing power. So here's my advice to my friends who will use this feature: monitor the load of your tasks and the overall performance of your system carefully. To make your operations faster and more efficient, consider making your operations parallel using Laravel queues. Here's an example:
Keep coding, you're doing great! 🖖 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool
"interrupt" has a bit of a special meaning when referring to timers and scheduling, it means requesting attention from the processor to some other element of your code (most often an interrupt handler). Maybe Nevertheless this probably needs (some cumbersome-to-write) tests where this subscheduling is tested. As you noted there will be task-skipping when a (synchronous) task takes longer than the interval, which could even be beneficial (no overlaps) depending on the command you're running. Just for reference, I have a fairly simple implementation for a single command I need to run every 5 seconds using a wrapped command that takes care of the loop: https://gist.github.com/bert-w/89a1bd46787c2404764bfd56f8a23fd1 (think twice before you decide you need to run code that often) |
I have a use-case in mind for this where I was just going to dispatch n delayed queued jobs every minute, but this would be nicer! |
There is a similar package: https://github.com/spatie/laravel-short-schedule |
Would this allow sub-second scheduling? |
That would be cool to have. |
dd4ed4a
to
4e4358e
Compare
* Pass queue from Mailable to SendQueuedMailable job Fixes issue where the unserialized job has the wrong queue. * Pass connection from Mailable to SendQueuedMailable job * Fix property mismatches in SendQueuedMailable * order --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
…into sub-minute-scheduling
This means |
Thats awfully specific. What prohibits you from using every 10 seconds or every 15 seconds? I do agree the |
I agree but don't see any reason why it's missing |
Wanted to try this feature locally, and I realized |
I don't think that's necessary. As far as I understand from the PR description the |
Yeah, I think that's fine. You just have to wait a minute before sub-minute tasks actually run, it's ok. But |
Not necessary? If you run |
#47720 improves |
A better use case would be with queues. |
That so valuable 😉 |
protected function repeatEvery($seconds) | ||
{ | ||
if (60 % $seconds !== 0) { | ||
throw new InvalidArgumentException("The seconds [$seconds] are not evenly divisible by 60."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this say "60 is not evenly divisible by the seconds [$seconds]" or some other wording.
The current wording implies that seconds needs to be divisible by 60 but that's not what the code does...?
Only way to set task every 3 seconds. $event = Schedule::command(Indicators::class);
$event->repeatSeconds = 3;
$event->everyMinute(); |
This PR introduces the ability to schedule tasks with a sub-minute frequencies.
Example
The new frequency options are:
everySecond
everyTwoSeconds
everyFiveSeconds
everyTenSeconds
everyFifteenSeconds
everyTwentySeconds
everyThirtySeconds
How it works
The
schedule:run
Artisan command has been modified to continue running until the end of the minute when the current minute has events with sub-minute frequency.Caveats
If a condition (e.g. using theSolved!when
method) prevents a sub-minute task from running at the beginning of a minute, it won't be run at all during that minute.schedule:run
command won't know this occurred and will continue processing tasks. Theschedule:interrupt
command can be used in these scenarios to ensure no further jobs are processed for the current minute, but will include those configured to run in maintenance mode.Interrupting the execution (e.g. when deploying new code)
The
schedule:run
command is typically called by your system's cron daemon. When deploying new code, you may need to interrupt the currently-running command. This can be done with theschedule:interrupt
command:This works similarly to the
queue:restart
command, which uses the cache to pass a signal value to the running process that will be checked on the next loop. The schedule will continue when your cron daemon next callsschedule:run
.I went with "interrupt" over "restart" or "stop" because neither felt quite right with the way the command will be stopped and then potentially started again in the next minute.