-
Notifications
You must be signed in to change notification settings - Fork 2
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
Avoid Cavalcade lookups on front-end requests #702
Comments
Suggestion from @kovshenin
|
This is also a good opportunity to improve our scheduled tasks docs, specifically suggest ways how to add single and recurring tasks, how and when to check By default in altis/cloud we can short-circuit the core https check event, the privacy events, etc. that we know are most likely to already be scheduled. We can also try and move them to |
Implementing this filter from application code as described here doesn't seem to have any effect on whether Cavalcade queries are executed on the frontend, for me. In fact, returning either It seems like possibly Cavalcade runs so early, that I am not able to set filters from an mu-plugin which actually run during the cavalcade job lookup process? |
My assumption was incorrect, but returning /**
* Skip cavalcade scheduling on high-traffic frontend pages.
*
* @param null|false|object $pre Value to return instead. Default null to continue retrieving the event.
* @param string $hook Action hook of the event.
* @param array $args Array containing each separate argument to pass to the hook's callback function.
* @return null|false|object Returning a non-null value will short-circuit the normal process.
*/
function mytheme_skip_cavalcade_scheduled_event_on_high_traffic_pages( $pre, $hook, $args ) {
if ( is_admin() ) {
// Do not alter processing of events within the admin.
return $pre;
}
// Most of the events we want to skip run before any globals are set, so
// it is not straightforward to skip these only on is_single() or is_home().
// Skip relevant jobs on all frontend pages, for now.
$is_skippable_hook = in_array(
$hook,
[
'wp_https_detection',
'extendable-aggregator-sync-cron',
'wp_site_health_scheduled_check',
'wp_privacy_delete_old_export_files',
],
true
);
if ( $is_skippable_hook ) {
$fake_scheduled_event = new stdClass();
// Perpetually one hour in the future to defer any queries.
$fake_scheduled_event->timestamp = time() + HOUR_IN_SECONDS;
return $fake_scheduled_event;
}
return $pre;
}
add_filter( 'pre_get_scheduled_event', 'mytheme_skip_cavalcade_scheduled_event_on_high_traffic_pages', 10, 3 );
|
Can we just return false? I this is the only way would you mind updating the PR #713 ? |
@roborourke if we return |
If
Returning false will also short-circuit the filter because there is an explicit check for null: |
@kovshenin See above—it short-circuits the filter, but then the plugin treats that as an opportunity to "fix" the situation by setting the missing job. That results in more queries, which is not the goal. |
Shortcircuiting is the point though? Cavalcade should be respecting any previous modifications to the value on that filter. |
Sorry I should have been a bit more clear. Short-circuit with a false at the end tells Cavalcade that the none of the jobs exist, which is what we don't want, as demonstrated by @kadamwhite's screenshot. We only want to short-circuit for the jobs that we know, i.e. |
@kovshenin The change I made which resulted in additional queries was to return if ( $is_skippable_hook ) {
return false;
} I wasn't short-circuiting at the end, only for the jobs that we know. However, returning if ( ! wp_next_scheduled( 'wp_site_health_scheduled_check' ) && ! wp_installing() ) {
wp_schedule_event( time() + DAY_IN_SECONDS, 'weekly', 'wp_site_health_scheduled_check' );
} Returning |
Ah, apologies, I thought @roborourke's comment was referring to that last |
Per this issue on the Cavalcade repo when db latency is a factor the common pattern of checking for and scheduling cron jobs is often done on
init
. WP core does this as well many 3rd party plugins etc...In theory we could avoid this altogether using the preflight filter to limit these checks to the admin only, provided they are added during the
init
hook:Events will still be scheduled but on admin requests only.
This would cause issues if plugins are adding events on the frontend using the
init
hook based on user interactions and$_GET
or$_POST
parameters, although that would be far more likely dealt with not on theinit
hook.This approach seems safe to me, especially if we also check that
$_POST
is empty. Would be great to get some additional input and feedback though.The text was updated successfully, but these errors were encountered: