Skip to content

Commit 06cc119

Browse files
authored
Merge pull request #186 from Automattic/fix/101-cron-validation-after-pull-failure
2 parents e55d365 + 23323fa commit 06cc119

File tree

2 files changed

+403
-49
lines changed

2 files changed

+403
-49
lines changed

includes/class-syndication-site-auto-retry.php

Lines changed: 60 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -55,78 +55,89 @@ public function handle_pull_failure_event( $site_id = 0, $failed_attempts = 0 )
5555
}
5656

5757
// Only proceed if we have a valid site id
58-
if ( 0 !== $site_id ) {
58+
if ( 0 === $site_id ) {
59+
return;
60+
}
61+
62+
// Fetch the site post.
63+
$site = get_post( $site_id );
5964

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

63-
// Fetch the site url
64-
$site_url = get_post_meta( $site->ID, 'syn_feed_url', true );
70+
// Fetch the site url.
71+
$site_url = get_post_meta( $site->ID, 'syn_feed_url', true );
6572

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

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

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

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

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

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

89-
// Yes we are..
98+
// Are we still below the auto retry limit?
99+
if ( $site_auto_retry_count < $auto_retry_limit ) {
90100

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

94-
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() );
103+
// Run in one minute by default.
104+
$auto_retry_interval = apply_filters( 'syndication_failure_auto_retry_interval', $time_now + MINUTE_IN_SECONDS );
95105

96-
// Schedule a pull retry for one minute in the future
97-
wp_schedule_single_event(
98-
$auto_retry_interval, // retry in X time
99-
'syn_pull_content', // fire the syndication_auto_retry hook
100-
array( array( $site ) ) // the site which failed to pull
101-
);
106+
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() );
102107

103-
// Increment our auto retry counter
104-
$site_auto_retry_count++;
108+
// Schedule a pull retry for one minute in the future.
109+
wp_schedule_single_event(
110+
$auto_retry_interval, // retry in X time
111+
'syn_pull_content', // fire the syndication_auto_retry hook
112+
array( array( $site ) ) // the site which failed to pull
113+
);
105114

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

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

111-
// Auto Retry limit met
112-
// Let's cleanup after ourselves
113-
$cleanup = true ;
114-
}
115121
} else {
116122

117-
// Retry attempt limit met
118-
// The site has been disabled, let's cleanup after ourselves
123+
// Auto Retry limit met.
124+
// Let's cleanup after ourselves.
119125
$cleanup = true;
120126
}
127+
} else {
121128

122-
// Should we cleanup after ourselves?
123-
if ( $cleanup ) {
129+
// Retry attempt limit met.
130+
// The site has been disabled, let's cleanup after ourselves.
131+
$cleanup = true;
132+
}
124133

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

128-
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() );
129-
}
137+
// Remove the auto retry if there was one.
138+
delete_post_meta( $site->ID, 'syn_failed_auto_retry_attempts' );
139+
140+
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() );
130141
}
131142
}
132143

0 commit comments

Comments
 (0)