Cleaners
5 years ago
Exceptions
5 years ago
Cancel.php
3 years ago
CancelUpdate.php
4 years ago
Cloning.php
3 years ago
CloningProcess.php
3 years ago
Data.php
3 years ago
Database.php
3 years ago
Delete.php
3 years ago
Directories.php
2 years ago
Files.php
2 years ago
Finish.php
2 years ago
Job.php
2 years ago
JobExecutable.php
3 years ago
Logs.php
3 years ago
PreserveDataFirstStep.php
3 years ago
PreserveDataSecondStep.php
3 years ago
ProcessLock.php
3 years ago
Scan.php
2 years ago
SearchReplace.php
3 years ago
TotalStepsAreNumberOfTables.php
5 years ago
Updating.php
3 years ago
Verify.php
3 years ago
Job.php
370 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backend\Modules\Jobs; |
| 4 | |
| 5 | use DateInterval; |
| 6 | use DateTime; |
| 7 | use Exception; |
| 8 | use stdClass; |
| 9 | use WPStaging\Core\DTO\Settings; |
| 10 | use WPStaging\Core\Utils\Cache; |
| 11 | use WPStaging\Core\Utils\Logger; |
| 12 | use WPStaging\Core\WPStaging; |
| 13 | use WPStaging\Framework\Database\ExcludedTables; |
| 14 | use WPStaging\Framework\Interfaces\ShutdownableInterface; |
| 15 | use WPStaging\Framework\Traits\ResourceTrait; |
| 16 | use WPStaging\Framework\Utils\Math; |
| 17 | use WPStaging\Backend\Modules\SystemInfo; |
| 18 | use WPStaging\Framework\Database\WpDbInfo; |
| 19 | use WPStaging\Framework\Security\UniqueIdentifier; |
| 20 | |
| 21 | /** |
| 22 | * Class Job |
| 23 | * @package WPStaging\Backend\Modules\Jobs |
| 24 | */ |
| 25 | abstract class Job implements ShutdownableInterface |
| 26 | { |
| 27 | use ResourceTrait; |
| 28 | |
| 29 | /** |
| 30 | * @var Cache |
| 31 | */ |
| 32 | protected $cache; |
| 33 | |
| 34 | /** |
| 35 | * @var Logger |
| 36 | */ |
| 37 | protected $logger; |
| 38 | |
| 39 | /** |
| 40 | * @var object |
| 41 | */ |
| 42 | protected $options; |
| 43 | |
| 44 | /** |
| 45 | * @var object |
| 46 | */ |
| 47 | protected $settings; |
| 48 | |
| 49 | /** |
| 50 | * Multisite home domain without scheme |
| 51 | * @var string |
| 52 | */ |
| 53 | protected $baseUrl; |
| 54 | |
| 55 | /** @var ExcludedTables */ |
| 56 | protected $excludedTableService; |
| 57 | |
| 58 | /** @var UniqueIdentifier */ |
| 59 | protected $identifier; |
| 60 | |
| 61 | /** @var Math */ |
| 62 | protected $utilsMath; |
| 63 | |
| 64 | /** @var SystemInfo */ |
| 65 | protected $systemInfo; |
| 66 | |
| 67 | /** |
| 68 | * Job constructor. |
| 69 | * @throws Exception |
| 70 | */ |
| 71 | public function __construct() |
| 72 | { |
| 73 | $this->utilsMath = new Math(); |
| 74 | |
| 75 | $this->excludedTableService = new ExcludedTables(); |
| 76 | |
| 77 | // Services |
| 78 | $this->cache = new Cache(-1, WPStaging::getContentDir()); |
| 79 | //$this->logger = WPStaging::make(Logger::class); |
| 80 | $this->logger = WPStaging::getInstance()->get("logger"); |
| 81 | $this->systemInfo = WPStaging::make(SystemInfo::class); |
| 82 | $this->identifier = WPStaging::make(UniqueIdentifier::class); |
| 83 | |
| 84 | // Settings and Options |
| 85 | $this->options = $this->cache->get("clone_options"); |
| 86 | $this->settings = (object)((new Settings())->setDefault()); |
| 87 | |
| 88 | if (!$this->options) { |
| 89 | $this->options = new stdClass(); |
| 90 | } |
| 91 | |
| 92 | if (isset($this->options->existingClones) && is_object($this->options->existingClones)) { |
| 93 | $this->options->existingClones = json_decode(json_encode($this->options->existingClones), true); |
| 94 | } |
| 95 | |
| 96 | if (method_exists($this, "initialize")) { |
| 97 | $this->initialize(); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @todo can be removed? |
| 103 | * @return void |
| 104 | */ |
| 105 | public function onWpShutdown() |
| 106 | { |
| 107 | // do nothing |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @param null|array|object $options |
| 112 | * @return bool |
| 113 | * @throws Exception |
| 114 | */ |
| 115 | public function saveOptions($options = null) |
| 116 | { |
| 117 | // Get default options |
| 118 | if ($options === null) { |
| 119 | $options = $this->options; |
| 120 | } |
| 121 | |
| 122 | $now = new DateTime(); |
| 123 | $options->expiresAt = $now->add(new DateInterval('P1D'))->format('Y-m-d H:i:s'); |
| 124 | |
| 125 | if (!property_exists($options, 'jobIdentifier')) { |
| 126 | $options->jobIdentifier = rand(0, 2147483647); // 32 bits int max |
| 127 | } |
| 128 | |
| 129 | // Ensure that it is an object |
| 130 | $options = json_decode(json_encode($options)); |
| 131 | |
| 132 | return $this->cache->save('clone_options', $options); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @return object |
| 137 | */ |
| 138 | public function getOptions() |
| 139 | { |
| 140 | return $this->options; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Get current time in seconds |
| 145 | * @return float |
| 146 | */ |
| 147 | protected function time() |
| 148 | { |
| 149 | $time = microtime(); |
| 150 | $time = explode(' ', $time); |
| 151 | $time = (float)$time[1] + (float)$time[0]; |
| 152 | return $time; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * @return bool |
| 157 | */ |
| 158 | public function isOverThreshold() |
| 159 | { |
| 160 | // Check if the memory is over threshold |
| 161 | $usedMemory = $this->getMemoryPeakUsage(); |
| 162 | $maxMemoryLimit = $this->getMaxMemoryLimit(); |
| 163 | $scriptMemoryLimit = $this->getScriptMemoryLimit(); |
| 164 | |
| 165 | $this->debugLog( |
| 166 | sprintf( |
| 167 | "Used Memory: %s Max Memory Limit: %s Max Script Memory Limit: %s", |
| 168 | size_format($usedMemory), |
| 169 | size_format($maxMemoryLimit), |
| 170 | size_format($scriptMemoryLimit) |
| 171 | ), |
| 172 | Logger::TYPE_DEBUG |
| 173 | ); |
| 174 | |
| 175 | if ($this->isMemoryLimit()) { |
| 176 | $this->log( |
| 177 | sprintf( |
| 178 | "Used Memory: %s Memory Limit: %s Max Script memory limit: %s", |
| 179 | size_format($usedMemory), |
| 180 | size_format($maxMemoryLimit), |
| 181 | size_format($scriptMemoryLimit) |
| 182 | ), |
| 183 | Logger::TYPE_ERROR |
| 184 | ); |
| 185 | |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | // Check if execution time is over threshold |
| 190 | if ($this->isTimeLimit()) { |
| 191 | $this->debugLog( |
| 192 | sprintf( |
| 193 | "RESET TIME: current time: %s, Start Time: %d, exec time limit: %s", |
| 194 | $this->getRunningTime(), |
| 195 | WPStaging::$startTime, |
| 196 | $this->findExecutionTimeLimit() |
| 197 | ) |
| 198 | ); |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * @param string $msg |
| 207 | * @param string $type |
| 208 | */ |
| 209 | public function log($msg, $type = Logger::TYPE_INFO) |
| 210 | { |
| 211 | if ($this->logger === null) { |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | $this->logger->setFileName($this->getLogFilename()); |
| 216 | |
| 217 | $this->logger->add($msg, $type); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * @return string |
| 222 | */ |
| 223 | private function getLogFilename() |
| 224 | { |
| 225 | $uniqueId = $this->identifier->getIdentifier(); |
| 226 | // If job is not cloning i.e. updating, resetting, pushing |
| 227 | if (!empty($this->options->mainJob) && $this->options->mainJob !== 'cloning') { |
| 228 | return $this->options->mainJob . '_' . $uniqueId . '_' . date('Y-m-d', time()); |
| 229 | } |
| 230 | |
| 231 | // If job is cloning |
| 232 | if (!empty($this->options->clone) && !empty($this->options->mainJob)) { |
| 233 | return $this->options->mainJob . '_' . $uniqueId . '_' . $this->options->clone . '_' . date('Y-m-d', time()); |
| 234 | } |
| 235 | |
| 236 | if (empty($this->options->clone) && !empty($this->options->mainJob)) { |
| 237 | return $this->options->mainJob . '_' . $uniqueId . '_unknown_clone_' . date('Y-m-d', time()); |
| 238 | } |
| 239 | |
| 240 | if (!empty($this->options->clone) && empty($this->options->mainJob)) { |
| 241 | return 'unknown_job_' . $uniqueId . '_' . $this->options->clone . '_' . date('Y-m-d', time()); |
| 242 | } |
| 243 | |
| 244 | return 'unknown_job_' . $uniqueId . '_' . date('Y-m-d', time()); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * @param string $msg |
| 249 | * @param string $type |
| 250 | */ |
| 251 | public function debugLog($msg, $type = Logger::TYPE_INFO) |
| 252 | { |
| 253 | $this->logger->setFileName($this->getLogFilename()); |
| 254 | |
| 255 | if (isset($this->settings->debugMode)) { |
| 256 | $this->logger->add($msg, $type); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Throw an error message via json and stop further execution |
| 262 | * @param string $message |
| 263 | */ |
| 264 | public function returnException($message = '') |
| 265 | { |
| 266 | wp_die( |
| 267 | json_encode( |
| 268 | [ |
| 269 | 'job' => isset($this->options->currentJob) ? $this->options->currentJob : '', |
| 270 | 'status' => false, |
| 271 | 'message' => $message, |
| 272 | 'error' => true |
| 273 | ] |
| 274 | ) |
| 275 | ); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Is job running |
| 280 | * @return bool |
| 281 | */ |
| 282 | protected function isRunning() |
| 283 | { |
| 284 | if (!isset($this->options) || !isset($this->options->isRunning) || !isset($this->options->expiresAt)) { |
| 285 | return false; |
| 286 | } |
| 287 | |
| 288 | try { |
| 289 | $now = new DateTime(); |
| 290 | $expiresAt = new DateTime($this->options->expiresAt); |
| 291 | return $this->options->isRunning === true && $now < $expiresAt; |
| 292 | } catch (Exception $e) { |
| 293 | } |
| 294 | |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | protected function isPro() |
| 299 | { |
| 300 | return defined('WPSTGPRO_VERSION'); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * @return bool |
| 305 | */ |
| 306 | protected function isMultisiteAndPro() |
| 307 | { |
| 308 | return $this->isPro() && is_multisite(); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * @return bool |
| 313 | */ |
| 314 | public function isNetworkClone() |
| 315 | { |
| 316 | if (!isset($this->options->networkClone)) { |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | return $this->isMultisiteAndPro() && $this->options->networkClone; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Should exclude wp-config file during clone update |
| 325 | * |
| 326 | * @return bool |
| 327 | */ |
| 328 | public function excludeWpConfigDuringUpdate() |
| 329 | { |
| 330 | return $this->options->mainJob === Updating::NORMAL_UPDATE; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Check if external database is used |
| 335 | * @return bool |
| 336 | */ |
| 337 | protected function isExternalDatabase() |
| 338 | { |
| 339 | return !(empty($this->options->databaseUser) && empty($this->options->databasePassword)); |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * @return bool |
| 344 | */ |
| 345 | protected function isStagingDatabaseSameAsProductionDatabase() |
| 346 | { |
| 347 | if (empty($this->options->databaseUser) || empty($this->options->databaseServer) || empty($this->options->databaseDatabase)) { |
| 348 | return true; |
| 349 | } |
| 350 | |
| 351 | if ($this->options->databaseServer === DB_HOST && $this->options->databaseDatabase === DB_NAME) { |
| 352 | return true; |
| 353 | } |
| 354 | |
| 355 | $productionDb = WPStaging::make('wpdb'); |
| 356 | $productionDbInfo = new WpDbInfo($productionDb); |
| 357 | $productionServer = $productionDbInfo->getServer(); |
| 358 | |
| 359 | $stagingDb = new \wpdb($this->options->databaseUser, str_replace("\\\\", "\\", $this->options->databasePassword), $this->options->databaseDatabase, $this->options->databaseServer); |
| 360 | $stagingDbInfo = new WpDbInfo($stagingDb); |
| 361 | $stagingServer = $stagingDbInfo->getServer(); |
| 362 | |
| 363 | if ($productionServer === $stagingServer && $this->options->databaseDatabase === DB_NAME) { |
| 364 | return true; |
| 365 | } |
| 366 | |
| 367 | return false; |
| 368 | } |
| 369 | } |
| 370 |