Skip to content
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

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

vedanshujain
Copy link
Contributor

@vedanshujain vedanshujain commented Jun 20, 2022

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

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.
Comment on lines +67 to +71
$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 );
Copy link
Member

@barryhughes barryhughes Jun 20, 2022

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 );
Copy link
Member

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() ) {
Copy link
Member

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scheduling just one recurring action makes more than one
2 participants