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

Improve the control over rocket_rucss_pending_jobs_cron action #4960

Closed
4 tasks
piotrbak opened this issue Apr 22, 2022 · 1 comment · Fixed by #4965
Closed
4 tasks

Improve the control over rocket_rucss_pending_jobs_cron action #4960

piotrbak opened this issue Apr 22, 2022 · 1 comment · Fixed by #4965
Assignees
Labels
effort: [M] 3-5 days of estimated development time module: remove unused css priority: high Issues which should be resolved as quickly as possible type: enhancement Improvements that slightly enhance existing functionality and are fast to implement
Milestone

Comments

@piotrbak
Copy link
Contributor

Describe the bug
Currently, we improved the behaviour described in the #4940 based on the woocommerce/action-scheduler#698 suggestions. However, we can improve it even further by adding a health check cron.

Expected behavior
We'll schedule a permanent action/cron that will check the number of pending or in-progress rocket_rucss_pending_jobs_cron actions:

  1. 0 actions - we need to schedule it
  2. 1 action - we're just fine, no further action needed
  3. 1+ actions - we need to reduce it to 1

Backlog Grooming (for WP Media dev team use only)

  • Reproduce the problem
  • Identify the root cause
  • Scope a solution
  • Estimate the effort
@piotrbak piotrbak added type: enhancement Improvements that slightly enhance existing functionality and are fast to implement priority: high Issues which should be resolved as quickly as possible needs: grooming module: remove unused css labels Apr 22, 2022
@piotrbak piotrbak added this to the 3.11.1 milestone Apr 22, 2022
@engahmeds3ed
Copy link
Contributor

Grooming

Reproduce the problem
Sometimes I can see it using the following code:

add_action( 'rocket_rucss_pending_jobs_cron', function (){
    throw new Exception( 'Hiiii' );
} );

add_action( 'action_scheduler_failed_execution', function (){
	wp_remote_get( 'https://wp-rocket.me/?nowprocket' );
	wp_remote_get( 'https://rocketlabsqa.ovh/?nowprocket' );
} );

and then refresh on admin UI multiple times till reaching two rows in AS tools page for our hook rocket_rucss_pending_jobs_cron in pending status.

Identify the root cause

We are not 100% sure about that but as mentioned in the Action Scheduler's issue here: woocommerce/action-scheduler#698 the issue is in this method:

/**
* Process an individual action.
*
* @param int $action_id The action ID to process.
* @param string $context Optional identifer for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron'
* Generally, this should be capitalised and not localised as it's a proper noun.
*/
public function process_action( $action_id, $context = '' ) {
try {
$valid_action = false;
do_action( 'action_scheduler_before_execute', $action_id, $context );
if ( ActionScheduler_Store::STATUS_PENDING !== $this->store->get_status( $action_id ) ) {
do_action( 'action_scheduler_execution_ignored', $action_id, $context );
return;
}
$valid_action = true;
do_action( 'action_scheduler_begin_execute', $action_id, $context );
$action = $this->store->fetch_action( $action_id );
$this->store->log_execution( $action_id );
$action->execute();
do_action( 'action_scheduler_after_execute', $action_id, $action, $context );
$this->store->mark_complete( $action_id );
} catch ( Exception $e ) {
if ( $valid_action ) {
$this->store->mark_failure( $action_id );
do_action( 'action_scheduler_failed_execution', $action_id, $e, $context );
} else {
do_action( 'action_scheduler_failed_validation', $action_id, $e, $context );
}
}
if ( isset( $action ) && is_a( $action, 'ActionScheduler_Action' ) && $action->get_schedule()->is_recurring() ) {
$this->schedule_next_instance( $action, $action_id );
}
}

so in some cases before scheduling the new action, our main hook to check if our hook is scheduled or not here:

if ( $this->is_scheduled( $hook, $args ) ) {
return '';
}

is_scheduled here returns false in those cases.

Scope a solution

As mentioned in the issue itself we need to get the number of pending and in-progress actions and if it's more than 1, we should cancel the only pending actions.
This can be added here: https://github.com/wp-media/wp-rocket/blob/e219e903953a9dc6c7077de15f9e85d5191eb011/inc/Engine/Optimization/RUCSS/Admin/Subscriber.php#L185-L184

OR here:

and I recommend the second location so if not is_scheduled we can use the method search from the same class to get pending or in-progress statuses' actions and if this count is more than 1, we can use the method cancel_all and pass the hook to it, this method will cancel all scheduled actions for this hook and group, and then it'll be created with the next refresh in admin.

Estimate the effort
[S] to [M]

@engahmeds3ed engahmeds3ed added effort: [M] 3-5 days of estimated development time and removed needs: grooming labels Apr 25, 2022
@CrochetFeve0251 CrochetFeve0251 self-assigned this Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort: [M] 3-5 days of estimated development time module: remove unused css priority: high Issues which should be resolved as quickly as possible type: enhancement Improvements that slightly enhance existing functionality and are fast to implement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants