-
Notifications
You must be signed in to change notification settings - Fork 117
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
Scheduling just one recurring action makes more than one #698
Comments
Thanks for flagging this, @Nemi5150.
Yes, that seems like a real possibility. For example, simplifying a little, when an action is picked up for processing things shake out as follows:
So there is a gap between those last two points (the final status update, and the scheduling of the next individual action) and if, concurrently, a call to Probably, we need to update our docs at the very least to provide a more robust way of handling this. Possibly, we also need some changes in Action Scheduler itself to handle this sort of scenario more gracefully. |
I was able to duplicate this issue as well, with an aside, I've created a batch script that I was running concurrently(4 concurrent processes) to maybe try and simulate race conditions as: action_scheduler_loop.sh
In my screenshots, the duplicate appeared at 18:01:21. I've used this code for every minute(60 seconds) schedule:
Looking at this code line, that seems the one responsible for creating the duplicate, would it be worth changing the scheduled action store status to failed/complete only after the call to schedule the next event was sent? |
Hi!
Possibly. I think that would reduce the potential for this problem to occur, but I also suspect the 'core' race condition is the one that arises directly from the snippet found here. It's pretty aggressive in that it fires every request during Altering our recommended approach in the docs may therefore be the safer and better approach (short of a larger change that revises how recurring actions work and are stored). |
Hello there Happens to me as well in the same manner |
We have a further fix in the works, though efforts have been paused for some time while we prioritized other work. That said, in case you were not already aware of this, it has been possible to declare actions as 'unique' since our 3.6.0 release which may help to reduce instances of recurring event duplication. For instance, you can now replace this: if ( ! as_has_scheduled_action( 'foo' ) ) {
as_schedule_recurring_action( time(), HOUR_IN_SECONDS, 'foo' );
} With: as_schedule_recurring_action( time(), HOUR_IN_SECONDS, 'foo', [], '', true ); Or, using some PHP 8.x syntax: as_schedule_recurring_action( time(), HOUR_IN_SECONDS, 'foo', unique: true ); |
Thanks barryhughes! |
Yep, it's not a complete solution: it (hopefully) solves one part of the problem, but there is still a race condition in the logic that schedules the replacement action (which is what the PR I linked to tries to solve). |
I will be happy to test it, if that's of any help I might be utilizing an unorthodox code, but the 'unique' parameter doesn't help in my case:
I actually think to automatically unhook the actions every now and then via CRON to handle this specific issue. |
Thanks for the offer! We may reach out once we're ready for some help. I suspect you are seeing the problem more frequently, because you're processing things more frequently. We'll try to update the PR I referenced earlier and move this task to completion, but I can't really offer a timeline as we have quite a lot of work on our hands at present. |
For the meanwhile, went for a custom mechanism for my specific needs. Maybe it will be of interest:
|
For what it is worth, I recently implemented this and it seems to have solved my problem of duplicate entries. |
On the Usage page there is an example of how to "schedule a function to run at midnight if it is not already scheduled". See here
https://actionscheduler.org/usage/
I have used this example to create an action that runs every minute, and only one gets scheduled. However, every once in a while I check on the scheduled actions and there are two of those running. My guess is that there must be a race condition two requests are running at the same time and end up scheduling two upcoming actions. They then stay in perpetuity.
What is the best way to keep this from happening?
The text was updated successfully, but these errors were encountered: