Skip to content
Merged
Show file tree
Hide file tree
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
109 changes: 60 additions & 49 deletions includes/class-syndication-site-auto-retry.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,78 +55,89 @@ public function handle_pull_failure_event( $site_id = 0, $failed_attempts = 0 )
}

// Only proceed if we have a valid site id
if ( 0 !== $site_id ) {
if ( 0 === $site_id ) {
return;
}

// Fetch the site post.
$site = get_post( $site_id );

// Fetch the site post
$site = get_post( $site_id );
// Validate the site post exists and is the correct post type.
if ( ! $site instanceof WP_Post || 'syn_site' !== $site->post_type ) {
return;
}

// Fetch the site url
$site_url = get_post_meta( $site->ID, 'syn_feed_url', true );
// Fetch the site url.
$site_url = get_post_meta( $site->ID, 'syn_feed_url', true );

// Fetch the number of times we've tried to auto-retry
$site_auto_retry_count = (int) get_post_meta( $site_id, 'syn_failed_auto_retry_attempts', true );
// Validate the site has a valid-looking syndication URL.
if ( empty( $site_url ) || false === filter_var( $site_url, FILTER_VALIDATE_URL ) ) {
return;
}

// Only proceed if we haven't hit the pull attempt ceiling
if ( $failed_attempts < $max_pull_attempts ) {
// Fetch the number of times we've tried to auto-retry.
$site_auto_retry_count = (int) get_post_meta( $site_id, 'syn_failed_auto_retry_attempts', true );

// Allow the default auto retry to be filtered
// By default, only auto retry 3 times
$auto_retry_limit = apply_filters( 'pull_syndication_failure_auto_retry_limit', 3 );
// Only proceed if we haven't hit the pull attempt ceiling.
if ( $failed_attempts < $max_pull_attempts ) {

// Store the current time for repeated use below
$time_now = time();
// Allow the default auto retry to be filtered.
// By default, only auto retry 3 times.
$auto_retry_limit = apply_filters( 'pull_syndication_failure_auto_retry_limit', 3 );

// Create a string time to be sent to the logger
// Add 1 so our log items appear to occur a second later
// and hence order better in the log viewer
// without this, sometimes when the pull occurs quickly
// these log items appear to occur at the same time as the failure
$log_time = date( 'Y-m-d H:i:s', $time_now + 1 );
// Store the current time for repeated use below.
$time_now = time();

// Are we still below the auto retry limit?
if ( $site_auto_retry_count < $auto_retry_limit ) {
// Create a string time to be sent to the logger.
// Add 1 so our log items appear to occur a second later
// and hence order better in the log viewer.
// Without this, sometimes when the pull occurs quickly
// these log items appear to occur at the same time as the failure.
$log_time = gmdate( 'Y-m-d H:i:s', $time_now + 1 );

// Yes we are..
// Are we still below the auto retry limit?
if ( $site_auto_retry_count < $auto_retry_limit ) {

// Run in one minute by default
$auto_retry_interval = apply_filters( 'syndication_failure_auto_retry_interval', $time_now + MINUTE_IN_SECONDS );
// Yes we are..

Syndication_Logger::log_post_info( $site->ID, $status = 'start_auto_retry', $message = sprintf( __( 'Connection retry %d of %d to %s in %s..', 'push-syndication' ), $site_auto_retry_count + 1, $auto_retry_limit, $site_url, human_time_diff( $time_now, $auto_retry_interval ) ), $log_time, $extra = array() );
// Run in one minute by default.
$auto_retry_interval = apply_filters( 'syndication_failure_auto_retry_interval', $time_now + MINUTE_IN_SECONDS );

// Schedule a pull retry for one minute in the future
wp_schedule_single_event(
$auto_retry_interval, // retry in X time
'syn_pull_content', // fire the syndication_auto_retry hook
array( array( $site ) ) // the site which failed to pull
);
Syndication_Logger::log_post_info( $site->ID, $status = 'start_auto_retry', $message = sprintf( __( 'Connection retry %d of %d to %s in %s..', 'push-syndication' ), $site_auto_retry_count + 1, $auto_retry_limit, $site_url, human_time_diff( $time_now, $auto_retry_interval ) ), $log_time, $extra = array() );

// Increment our auto retry counter
$site_auto_retry_count++;
// Schedule a pull retry for one minute in the future.
wp_schedule_single_event(
$auto_retry_interval, // retry in X time
'syn_pull_content', // fire the syndication_auto_retry hook
array( array( $site ) ) // the site which failed to pull
);

// And update the post meta auto retry count
update_post_meta( $site->ID, 'syn_failed_auto_retry_attempts', $site_auto_retry_count );
// Increment our auto retry counter.
$site_auto_retry_count++;

} else {
// And update the post meta auto retry count.
update_post_meta( $site->ID, 'syn_failed_auto_retry_attempts', $site_auto_retry_count );

// Auto Retry limit met
// Let's cleanup after ourselves
$cleanup = true ;
}
} else {

// Retry attempt limit met
// The site has been disabled, let's cleanup after ourselves
// Auto Retry limit met.
// Let's cleanup after ourselves.
$cleanup = true;
}
} else {

// Should we cleanup after ourselves?
if ( $cleanup ) {
// Retry attempt limit met.
// The site has been disabled, let's cleanup after ourselves.
$cleanup = true;
}

// Remove the auto retry if there was one
delete_post_meta( $site->ID, 'syn_failed_auto_retry_attempts' );
// Should we cleanup after ourselves?
if ( $cleanup ) {

Syndication_Logger::log_post_error( $site->ID, $status = 'end_auto_retry', $message = sprintf( __( 'Failed %d times to reconnect to %s', 'push-syndication' ), $site_auto_retry_count, $site_url ), $log_time, $extra = array() );
}
// Remove the auto retry if there was one.
delete_post_meta( $site->ID, 'syn_failed_auto_retry_attempts' );

Syndication_Logger::log_post_error( $site->ID, $status = 'end_auto_retry', $message = sprintf( __( 'Failed %d times to reconnect to %s', 'push-syndication' ), $site_auto_retry_count, $site_url ), $log_time, $extra = array() );
}
}

Expand Down
Loading