From 7014c9d9fdf2669ac901f944ef9e4048fae50707 Mon Sep 17 00:00:00 2001 From: Yahnis Elsts Date: Sun, 3 Nov 2019 12:33:09 +0200 Subject: [PATCH] Randomly offset the update check schedule within $checkPeriod This helps spread out update requests over all hours of the day and may prevent the update server from being hit with daily traffic spikes. See #323 for discussion. --- Puc/v4p8/Scheduler.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Puc/v4p8/Scheduler.php b/Puc/v4p8/Scheduler.php index 246a22f..1b89cf2 100644 --- a/Puc/v4p8/Scheduler.php +++ b/Puc/v4p8/Scheduler.php @@ -51,7 +51,14 @@ if ( !class_exists('Puc_v4p8_Scheduler', false) ): } if ( !wp_next_scheduled($this->cronHook) && !defined('WP_INSTALLING') ) { - wp_schedule_event(time(), $scheduleName, $this->cronHook); + //Randomly offset the schedule to help prevent update server traffic spikes. Without this + //most checks may happen during times of day when people are most likely to install new plugins. + $firstCheckTime = time() - rand(0, max($this->checkPeriod * 3600 - 15 * 60, 1)); + $firstCheckTime = apply_filters( + $this->updateChecker->getUniqueName('first_check_time'), + $firstCheckTime + ); + wp_schedule_event($firstCheckTime, $scheduleName, $this->cronHook); } add_action($this->cronHook, array($this, 'maybeCheckForUpdates'));