From 9684b97659d03bffc4e7526f643853aa8363b543 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 9 Dec 2015 21:13:29 -0500 Subject: [PATCH] CRM-17637 - Add poor man's cron fallback for VersionCheck --- CRM/Utils/Check/Env.php | 1 + CRM/Utils/VersionCheck.php | 40 ++++++++++++++++---- tests/phpunit/CRM/Utils/versionCheckTest.php | 4 +- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/CRM/Utils/Check/Env.php b/CRM/Utils/Check/Env.php index 9e2d829882..89883fe6ed 100644 --- a/CRM/Utils/Check/Env.php +++ b/CRM/Utils/Check/Env.php @@ -289,6 +289,7 @@ class CRM_Utils_Check_Env { public function checkVersion() { $messages = array(); $vc = new CRM_Utils_VersionCheck(); + $vc->initialize(); // Show a notice if the version_check job is disabled if (empty($vc->cronJob['is_active'])) { diff --git a/CRM/Utils/VersionCheck.php b/CRM/Utils/VersionCheck.php index 443151420d..fd6ee3ce2b 100644 --- a/CRM/Utils/VersionCheck.php +++ b/CRM/Utils/VersionCheck.php @@ -33,8 +33,9 @@ class CRM_Utils_VersionCheck { const PINGBACK_URL = 'http://latest.civicrm.org/stable.php?format=json', - // relative to $config->uploadDir - CACHEFILE_NAME = '[civicrm.files]/persist/version-info-cache.json'; + CACHEFILE_NAME = '[civicrm.files]/persist/version-info-cache.json', + // after this length of time we fall back on poor-man's cron (7+ days) + CACHEFILE_EXPIRE = 605000; /** * The version of the current (local) installation @@ -87,12 +88,36 @@ class CRM_Utils_VersionCheck { public function __construct() { $this->localVersion = CRM_Utils_System::version(); $this->localMajorVersion = $this->getMajorVersion($this->localVersion); + $this->cacheFile = Civi::paths()->getPath(self::CACHEFILE_NAME); + } + /** + * Self-populates version info + */ + public function initialize() { $this->getJob(); // Populate remote $versionInfo from cache file - $this->cacheFile = Civi::paths()->getPath(self::CACHEFILE_NAME); $this->isInfoAvailable = $this->readCacheFile(); + + // Poor-man's cron fallback if scheduled job is enabled but has failed to run + $expiryTime = time() - self::CACHEFILE_EXPIRE; + if (!empty($this->cronJob['is_active']) && + (!$this->isInfoAvailable || filemtime($this->cacheFile) < $expiryTime) + ) { + $this->fetch(); + } + } + + /** + * Sets $versionInfo + * + * @param $info + */ + public function setVersionInfo($info) { + $this->versionInfo = (array) $info; + // Sort version info in ascending order for easier comparisons + ksort($this->versionInfo, SORT_NUMERIC); } /** @@ -362,9 +387,10 @@ class CRM_Utils_VersionCheck { // If we couldn't fetch or parse the data $versionInfo will be NULL // Otherwise it will be an array and we'll cache it. // Note the array may be empty e.g. in the case of a pre-alpha with no releases - if ($versionInfo !== NULL) { + $this->isInfoAvailable = $versionInfo !== NULL; + if ($this->isInfoAvailable) { $this->writeCacheFile($rawJson); - $this->versionInfo = $versionInfo; + $this->setVersionInfo($versionInfo); } } @@ -373,9 +399,7 @@ class CRM_Utils_VersionCheck { */ private function readCacheFile() { if (file_exists($this->cacheFile)) { - $this->versionInfo = (array) json_decode(file_get_contents($this->cacheFile), TRUE); - // Sort version info in ascending order for easier comparisons - ksort($this->versionInfo, SORT_NUMERIC); + $this->setVersionInfo(json_decode(file_get_contents($this->cacheFile), TRUE)); return TRUE; } return FALSE; diff --git a/tests/phpunit/CRM/Utils/versionCheckTest.php b/tests/phpunit/CRM/Utils/versionCheckTest.php index a85cd2e04f..8222125803 100644 --- a/tests/phpunit/CRM/Utils/versionCheckTest.php +++ b/tests/phpunit/CRM/Utils/versionCheckTest.php @@ -112,7 +112,7 @@ class CRM_Utils_versionCheckTest extends CiviUnitTestCase { // These values are set by the constructor but for testing we override them $vc->localVersion = $localVersion; $vc->localMajorVersion = $vc->getMajorVersion($localVersion); - $vc->versionInfo = $versionInfo; + $vc->setVersionInfo($versionInfo); $available = $vc->isNewerVersionAvailable(); $this->assertEquals($available['version'], $expectedResult); } @@ -158,7 +158,7 @@ class CRM_Utils_versionCheckTest extends CiviUnitTestCase { // These values are set by the constructor but for testing we override them $vc->localVersion = $localVersion; $vc->localMajorVersion = $vc->getMajorVersion($localVersion); - $vc->versionInfo = $versionInfo; + $vc->setVersionInfo($versionInfo); $available = $vc->isNewerVersionAvailable(); $this->assertEquals($available['upgrade'], $expectedResult); } -- 2.25.1