-
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
Schedule next action before current one is mark completed/failed. #830
base: trunk
Are you sure you want to change the base?
Conversation
This prevents race condition that may happen if we are trying to schedule same action, and the check `as_has_scheduled_action` runs after action is marked complete but before next one is scheduled.
$this->maybe_schedule_next_instance( $action, $action_id ); | ||
$this->store->mark_complete( $action_id ); | ||
} catch ( Exception $e ) { | ||
if ( $valid_action ) { | ||
$this->maybe_schedule_next_instance( $action, $action_id ); |
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.
Annotated this block with comments at the end of each line:
$this->maybe_schedule_next_instance( $action, $action_id ); # 1. Next instance is scheduled
$this->store->mark_complete( $action_id ); # 2. Exception can be thrown by this method
} catch ( Exception $e ) { # 3. Execution enters the catch block
if ( $valid_action ) { # 4. Action still 'valid'
$this->maybe_schedule_next_instance( $action, $action_id ); # 5. We schedule another instance
↑ This chain of events seems unlikely, but still technically possible and could result in a duplicate.
$this->store->mark_complete( $action_id ); | ||
} catch ( Exception $e ) { | ||
if ( $valid_action ) { | ||
$this->maybe_schedule_next_instance( $action, $action_id ); | ||
$this->store->mark_failure( $action_id ); |
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.
Thinking of PR #806, it may be better to wait until the action has been marked as a failure before we call ::maybe_schedule_next_instance()
(though, if this is merged first we can also update that other PR accordingly).
* @param ActionScheduler_Action $action Action object. | ||
* @param int $action_id Action ID. | ||
*/ | ||
private function maybe_schedule_next_instance( $action, $action_id ) { | ||
if ( isset( $action ) && is_a( $action, 'ActionScheduler_Action' ) && $action->get_schedule()->is_recurring() ) { |
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.
Does testing if $action
is set still make sense, now this is in its own method?
This prevents race conditions that may happen if we are trying to schedule the same action, and the check
as_has_scheduled_action
runs after the action is marked complete but before the next one is scheduled.Fixes #698