CronScriptReadinessCheck.php 4.7 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Setup\Model;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\Setup\Model\Cron\ReadinessCheck;

/**
 * This class is used by Readiness check page to verify if Setup cron command
 * and Updater cron script are running properly.
 * This includes verifying file permission in Updater Cron and db privileges in Setup Cron.
 * It also verifies Cron time interval configuration.
 * This class only verifies the status files created by both Cron jobs. No actual checking logic is done in this class.
 */
class CronScriptReadinessCheck
{
    /**
     * Setup type
     */
    const SETUP = 'setup';

    /**
     * Updater type
     */
    const UPDATER = 'updater';

    /**
     * Basename to Updater status file
     */
    const UPDATER_CRON_JOB_STATS_FILE = '.update_cronjob_status';

    /**
     * Key in Updater status file
     */
    const UPDATER_KEY_FILE_PERMISSIONS_VERIFIED = 'file_permissions_verified';

    /**
     * Error message for dependant checks
     */
    const OTHER_CHECKS_WILL_FAIL_MSG =
        '<br/>Other checks will fail as a result (PHP version, PHP settings, and PHP extensions)';

    /**
     * @var Filesystem
     */
    private $filesystem;

    /**
     * Constructor
     *
     * @param Filesystem $filesystem
     */
    public function __construct(Filesystem $filesystem)
    {
        $this->filesystem = $filesystem;
    }

    /**
     * Check Setup Cron job status file
     *
     * @return array
     */
    public function checkSetup()
    {
        return $this->checkJson(self::SETUP);
    }

    /**
     * Check Updater Cron job status file
     *
     * @return array
     */
    public function checkUpdater()
    {
        return $this->checkJson(self::UPDATER);
    }

    /**
     * Check JSON file created by Setup cron command and Updater cron script
     *
     * @param string $type
     * @return array
     */
    private function checkJson($type)
    {
        $read = $this->filesystem->getDirectoryRead(DirectoryList::VAR_DIR);
        try {
            switch ($type) {
                case self::SETUP:
                    $key = ReadinessCheck::KEY_DB_WRITE_PERMISSION_VERIFIED;
                    $jsonData = json_decode($read->readFile(ReadinessCheck::SETUP_CRON_JOB_STATUS_FILE), true);
                    break;
                case self::UPDATER:
                    $key = self::UPDATER_KEY_FILE_PERMISSIONS_VERIFIED;
                    $jsonData = json_decode($read->readFile(self::UPDATER_CRON_JOB_STATS_FILE), true);
                    break;
                default:
                    return ['success' => false, 'error' => 'Internal Error'];
            }
        } catch (\Magento\Framework\Exception\FileSystemException $e) {
            $error = 'Cron job has not been configured yet';
            if ($type == self::SETUP) {
                $error .= self::OTHER_CHECKS_WILL_FAIL_MSG;
            }
            return [
                'success' => false,
                'error' => $error
            ];
        }

        if (isset($jsonData[ReadinessCheck::KEY_READINESS_CHECKS])
            && isset($jsonData[ReadinessCheck::KEY_READINESS_CHECKS][$key])
        ) {
            if ($jsonData[ReadinessCheck::KEY_READINESS_CHECKS][$key]) {
                return $this->checkCronTime($jsonData);
            }
            return ['success' => false, 'error' => $jsonData[ReadinessCheck::KEY_READINESS_CHECKS]['error']];
        }
        $error = 'Cron job has not been configured yet';
        if ($type == self::SETUP) {
            $error .= self::OTHER_CHECKS_WILL_FAIL_MSG;
        }
        return [
            'success' => false,
            'error' => $error
        ];
    }

    /**
     * Check if Cron Job time interval is within acceptable range
     *
     * @param array $jsonData
     * @return array
     */
    private function checkCronTime(array $jsonData)
    {
        if (isset($jsonData[ReadinessCheck::KEY_CURRENT_TIMESTAMP])
            && isset($jsonData[ReadinessCheck::KEY_LAST_TIMESTAMP])
        ) {
            $timeDifference = $jsonData[ReadinessCheck::KEY_CURRENT_TIMESTAMP] -
                $jsonData[ReadinessCheck::KEY_LAST_TIMESTAMP];
            if ($timeDifference < 90) {
                return ['success' => true];
            }
            return [
                'success' => true,
                'notice' => 'We recommend you schedule cron to run every 1 minute'
            ];
        }
        return [
            'success' => true,
            'notice' => 'Unable to determine cron time interval. ' .
                'We recommend you schedule cron to run every 1 minute'
        ];
    }
}