-
Notifications
You must be signed in to change notification settings - Fork 384
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
Create background events for URL validation #5515
Conversation
Plugin builds for f64e4ab are ready 🛎️!
|
…ation-cron * origin/develop: Run composer update Ensure alphabetical order for patches in composer.json Reorder composer.json fields Remove obsolete amp_is_canonical() examples Add more markdown formatting and restore linebreak Wrap HTML tag in backticks Add variable to docs for `amp_dev_mode_enabled` filter Fix parsing of docblocks that contain code snippets
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@johnwatkins0 I tried turning off DevTools to see the SingleScheduledBackgroundTask
in action. However, I found that SavePostValidationEvent::should_schedule_event()
would never get called. The reason appears to be the logic in is_needed
:
public static function is_needed() {
return is_admin() || wp_doing_cron();
}
When making an edit to a post in the block editor, neither is_admin()
nor wp_doing_cron()
are true for the REST API request. This means the save_post
action is never added.
Should the CronBasedBackgroundTask
abstract class not be made Conditional
?
That's the simple solution. Updated in a53afa5 |
/** | ||
* Callback for the cron action. | ||
* | ||
* @param mixed[] ...$args Unused callback arguments. | ||
*/ | ||
public function process( ...$args ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | ||
$urls = $this->scannable_url_provider->get_urls(); | ||
$sleep_time = $this->get_sleep_time(); | ||
|
||
foreach ( $urls as $url ) { | ||
$this->url_validation_provider->get_url_validation( $url['url'], $url['type'], true ); | ||
if ( $sleep_time ) { | ||
sleep( $sleep_time ); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can address this in a subsequent PR, but I have an idea for how this could be modified to not make use of sleep()
. Instead of there being one single event that scans all of the URLs, we can instead register a separate event for each for each URL, and offset the time()
for each event by 10 minute intervals (for example). In this way only one URL would be validated during any given cron request, and there would be no need for sleep()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #5750.
I was testing
After deactivating, they remained. I found there were a few problems:
I've addressed these points in f424735. Now when deactivating the plugin, no AMP hooks remain:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the mammoth effort here. This is going to go a long way to help site owners monitor their sites for AMP validation issues.
Summary
Fixes #1756. Some related discussion also took place in #5228
Main changes
This PR does the following:
hourlydaily cron task that validates a set of URLs. The URLs are the default set returned from theScannableURLProvider
class. This set will always include most recent posts for all AMP-enabled post types plus a standard set of others such as the homepage and a search result.save_post
to validate the saved post's URL when the user has dev tools off. When the user has dev tools on, the existing behavior (URL is validated synchronously on save) is unchanged.Additional changes
BackgroundTaskDeactivator
I also took the suggestion from this to-do comment in the
CronBasedBackgroundTask
class:I created the new class,
BackgroundTaskDeactivator
which is injected into the background task classes and used to queue up registered background tasks to be cleared when the plugin is deactivated.CronBasedBackgroundTask refactor
CronBasedBackgroundTask
was previously being extended for a couple of different recurring cron tasks. I have createdRecurringBackgroundTask
along withSingleScheduledBackgroundTask
to account for the two different scheduling approaches we need for background. These new classes are both abstract, inheriting fromCronBasedBackgroundTask
, andCronBasedBackroundTask
is no longer directly extended by any non-abstract class.Remove locking
I removed the locking functions from
URLValidationProvider
. I introduced these in another recent PR, but we subsequently determined they're not needed for now at least.Checklist