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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions classes/abstracts/ActionScheduler_Abstract_QueueRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,26 @@ public function process_action( $action_id, $context = '' ) {
$this->store->log_execution( $action_id );
$action->execute();
do_action( 'action_scheduler_after_execute', $action_id, $action, $context );
$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 );
Comment on lines +67 to +71
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_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).

do_action( 'action_scheduler_failed_execution', $action_id, $e, $context );
} else {
do_action( 'action_scheduler_failed_validation', $action_id, $e, $context );
}
}
}

/**
* Helper method to schedule next instance of an instance if there is a schedule.
*
* @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?

$this->schedule_next_instance( $action, $action_id );
}
Expand Down