ApplyFiltersTrait.php
1 day ago
ArrayableTrait.php
1 day ago
BatchSizeCalculateTrait.php
1 day ago
BearerTokenTrait.php
1 day ago
BenchmarkTrait.php
1 day ago
BooleanTransientTrait.php
1 day ago
DatabaseSearchReplaceTrait.php
1 day ago
DbRowsGeneratorTrait.php
1 day ago
DebugLogTrait.php
1 day ago
DeveloperTimerTrait.php
1 day ago
EndOfLinePlaceholderTrait.php
1 day ago
EventLoggerTrait.php
1 day ago
FileScanToCacheTrait.php
1 day ago
FormatTrait.php
1 day ago
HttpRequestTrait.php
1 day ago
HydrateTrait.php
1 day ago
I18nTrait.php
1 day ago
IpResolverTrait.php
1 day ago
JobResponseTrait.php
1 day ago
MaintenanceTrait.php
7 months ago
MemoryExhaustTrait.php
1 day ago
MySQLRowsGeneratorTrait.php
1 day ago
NoticesTrait.php
1 day ago
PagesTrait.php
1 day ago
PropertyConstructor.php
1 day ago
RenameTmpDirectoryTrait.php
1 day ago
ResourceTrait.php
1 day ago
RestRequestTrait.php
1 day ago
RestoreFileExclusionTrait.php
1 day ago
SafeFileInfoTrait.php
1 day ago
SerializeTrait.php
1 day ago
SetTimeLimitTrait.php
1 day ago
SlashTrait.php
1 day ago
SqlCommentTrait.php
1 day ago
TablePrefixValidator.php
5 months ago
UrlTrait.php
1 day ago
ValueGetterTrait.php
1 day ago
WindowsOsTrait.php
1 day ago
ResourceTrait.php
455 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Traits; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | use WPStaging\Framework\Facades\Hooks; |
| 7 | use WPStaging\Framework\Job\Dto\JobDataDto; |
| 8 | |
| 9 | trait ResourceTrait |
| 10 | { |
| 11 | |
| 12 | protected $timeLimit; |
| 13 | |
| 14 | protected $resourceTraitSettings; |
| 15 | protected $executionTimeLimit; |
| 16 | protected $memoryLimit; |
| 17 | protected $scriptMemoryLimit; |
| 18 | |
| 19 | public static $defaultMaxExecutionTimeInSeconds = 30; |
| 20 | public static $executionTimeGapInSeconds = 5; |
| 21 | |
| 22 | |
| 23 | public static $backupRestoreMaxExecutionTimeInSeconds = 10; |
| 24 | |
| 25 | |
| 26 | public static $waitTaskMaxExecutionTimeInSeconds = 5; |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | public static $fileAppendMaxExecutionTimeInSeconds = 10; |
| 33 | |
| 34 | |
| 35 | protected $isUnitTest; |
| 36 | |
| 37 | |
| 38 | protected $allowResourceCheckOnUnitTests; |
| 39 | |
| 40 | |
| 41 | |
| 42 | protected $extractionLimit; |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | public function isThreshold() |
| 48 | { |
| 49 | if ($this->isUnitTest() && !Hooks::applyFilters('wpstg.tests.resources.allow_check', $this->allowResourceCheckOnUnitTests)) { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | $isMemoryLimit = $this->isMemoryLimit(); |
| 54 | $isTimeLimit = $this->isTimeLimit(); |
| 55 | |
| 56 | if (defined('WPSTG_DEBUG') && WPSTG_DEBUG) { |
| 57 | if ($isTimeLimit || $isMemoryLimit) { |
| 58 | \WPStaging\functions\debug_log('isThreshold: ' . wp_json_encode(['class' => __CLASS__, 'isTimeLimit' => $isTimeLimit, 'isMemoryLimit' => $isMemoryLimit], JSON_UNESCAPED_SLASHES)); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if ($isMemoryLimit) { |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | if ($isTimeLimit) { |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | public function isFileAppendThreshold() |
| 77 | { |
| 78 | if ($this->isUnitTest() && !Hooks::applyFilters('wpstg.tests.resources.allow_check', $this->allowResourceCheckOnUnitTests)) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | return $this->isMemoryLimit() || $this->isFileAppendTimeLimit(); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | public function isDatabaseRestoreThreshold() |
| 89 | { |
| 90 | return $this->isMemoryLimit() || $this->isDatabaseRestoreTimeLimit(); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | public function isWaitTaskThreshold() |
| 97 | { |
| 98 | return $this->isMemoryLimit() || $this->isWaitTaskTimeLimit(); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | public function isMaxExecutionThreshold() |
| 105 | { |
| 106 | return $this->isMemoryLimit() || $this->isMaxExecutionTimeoutLimit(); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | protected function isUnitTest() |
| 115 | { |
| 116 | if (isset($this->isUnitTest)) { |
| 117 | return $this->isUnitTest; |
| 118 | } |
| 119 | |
| 120 | $this->isUnitTest = defined('WPCEPT_ISOLATED_INSTALL'); |
| 121 | |
| 122 | return $this->isUnitTest; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | protected function getRunningTime() |
| 129 | { |
| 130 | return microtime(true) - WPStaging::$startTime; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | public function isMemoryLimit() |
| 137 | { |
| 138 | return $this->getScriptMemoryLimit() <= $this->getMemoryUsage(); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | public function isTimeLimit() |
| 145 | { |
| 146 | $timeLimit = $this->findExecutionTimeLimit(); |
| 147 | |
| 148 | if (isset($this->timeLimit)) { |
| 149 | $timeLimit = $this->timeLimit; |
| 150 | } |
| 151 | |
| 152 | return $this->getRunningTime() > $timeLimit; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | public function isDatabaseRestoreTimeLimit() |
| 159 | { |
| 160 | $timeLimit = (int)Hooks::applyFilters(JobDataDto::FILTER_RESOURCES_BACKUP_RESTORE_MAX_EXECUTION_TIME_IN_SECONDS, static::$backupRestoreMaxExecutionTimeInSeconds); |
| 161 | return $this->getRunningTime() > $timeLimit; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | public function isFileAppendTimeLimit(): bool |
| 168 | { |
| 169 | $timeLimit = (int)Hooks::applyFilters(JobDataDto::FILTER_RESOURCES_FILE_APPEND_TIME_LIMIT, static::$fileAppendMaxExecutionTimeInSeconds); |
| 170 | return $this->getRunningTime() > $timeLimit; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | public function isWaitTaskTimeLimit(): bool |
| 177 | { |
| 178 | $phpTimeLimit = $this->findExecutionTimeLimit(); |
| 179 | $timeLimit = min($phpTimeLimit, self::$waitTaskMaxExecutionTimeInSeconds); |
| 180 | |
| 181 | return $this->getRunningTime() > $timeLimit; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | public function isMaxExecutionTimeoutLimit() |
| 188 | { |
| 189 | return $this->getRunningTime() > $this->findExecutionTimeLimit(true); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | public function findExecutionTimeLimit($useMaxTimeout = false) |
| 202 | { |
| 203 | |
| 204 | if (isset($this->executionTimeLimit)) { |
| 205 | return $this->executionTimeLimit; |
| 206 | } |
| 207 | |
| 208 | $phpMaxExecutionTime = $this->getPhpMaxExecutionTime(); |
| 209 | $cpuBoundMaxExecutionTime = $this->getCpuBoundMaxExecutionTime(); |
| 210 | |
| 211 | |
| 212 | if ($useMaxTimeout) { |
| 213 | $this->executionTimeLimit = max(min($phpMaxExecutionTime - static::$executionTimeGapInSeconds, $phpMaxExecutionTime * 0.8), 10); |
| 214 | |
| 215 | |
| 216 | $this->executionTimeLimit = (int)Hooks::applyFilters('wpstg.tests.databaseRenameTaskExecutionTime', $this->executionTimeLimit); |
| 217 | |
| 218 | return $this->executionTimeLimit; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | if (!$cpuBoundMaxExecutionTime || $cpuBoundMaxExecutionTime > static::$defaultMaxExecutionTimeInSeconds) { |
| 223 | $cpuBoundMaxExecutionTime = static::$defaultMaxExecutionTimeInSeconds; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | if ($phpMaxExecutionTime > 0) { |
| 228 | $cpuBoundMaxExecutionTime = min($phpMaxExecutionTime, $cpuBoundMaxExecutionTime); |
| 229 | } |
| 230 | |
| 231 | |
| 232 | $this->executionTimeLimit = max(min($cpuBoundMaxExecutionTime - static::$executionTimeGapInSeconds, 30), 10); |
| 233 | |
| 234 | |
| 235 | |
| 236 | $this->executionTimeLimit = (int)Hooks::applyFilters(JobDataDto::FILTER_RESOURCES_EXECUTION_TIME_LIMIT, $this->executionTimeLimit); |
| 237 | |
| 238 | |
| 239 | if ((bool)Hooks::applyFilters(JobDataDto::FILTER_RESOURCES_IGNORE_TIME_LIMIT, false)) { |
| 240 | $this->executionTimeLimit = PHP_INT_MAX; |
| 241 | } |
| 242 | |
| 243 | return $this->executionTimeLimit; |
| 244 | } |
| 245 | |
| 246 | public function isWithinMemoryExtractionLimit(int $fileSize): bool |
| 247 | { |
| 248 | return $fileSize <= $this->getInMemoryExtractionLimit(); |
| 249 | } |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | public function getInMemoryExtractionLimit(): int |
| 262 | { |
| 263 | |
| 264 | if ($this->extractionLimit !== null) { |
| 265 | return $this->extractionLimit; |
| 266 | } |
| 267 | |
| 268 | $memoryLimit = $this->getMaxMemoryLimit(); |
| 269 | $limit = $this->calculateInMemoryExtractionLimit($memoryLimit); |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | $this->extractionLimit = (int)Hooks::applyFilters(JobDataDto::FILTER_BACKUP_INMEMORY_EXTRACTION_LIMIT, $limit, $memoryLimit); |
| 277 | |
| 278 | |
| 279 | $this->extractionLimit = max(524288, min($this->extractionLimit, (16 * MB_IN_BYTES))); |
| 280 | |
| 281 | return $this->extractionLimit; |
| 282 | } |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | protected function getMemoryUsage($realUsage = true) |
| 290 | { |
| 291 | return memory_get_usage($realUsage); |
| 292 | } |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | protected function getMemoryPeakUsage($realUsage = true) |
| 300 | { |
| 301 | return memory_get_peak_usage($realUsage); |
| 302 | } |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | public function getTimeLimit() |
| 308 | { |
| 309 | if (!isset($this->timeLimit)) { |
| 310 | $this->timeLimit = $this->findExecutionTimeLimit(); |
| 311 | } |
| 312 | |
| 313 | return $this->timeLimit; |
| 314 | } |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | public function setTimeLimit($timeLimit) |
| 320 | { |
| 321 | $this->timeLimit = $timeLimit; |
| 322 | } |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | public function resourceCheckOnUnitTests($isAllowed) |
| 328 | { |
| 329 | $this->allowResourceCheckOnUnitTests = $isAllowed; |
| 330 | } |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | protected function getMaxMemoryLimit() |
| 338 | { |
| 339 | |
| 340 | if (isset($this->memoryLimit)) { |
| 341 | return $this->memoryLimit; |
| 342 | } |
| 343 | |
| 344 | $memoryLimit = wp_convert_hr_to_bytes(ini_get('memory_limit')); |
| 345 | |
| 346 | |
| 347 | if ($memoryLimit == -1 || $memoryLimit < 0) { |
| 348 | $memoryLimit = 256 * MB_IN_BYTES; |
| 349 | } |
| 350 | |
| 351 | |
| 352 | $this->memoryLimit = Hooks::applyFilters(JobDataDto::FILTER_RESOURCES_MEMORY_LIMIT, $memoryLimit); |
| 353 | |
| 354 | |
| 355 | if (!is_int($this->memoryLimit) || $this->memoryLimit < (64 * MB_IN_BYTES)) { |
| 356 | $this->memoryLimit = 64 * MB_IN_BYTES; |
| 357 | } |
| 358 | |
| 359 | |
| 360 | $this->memoryLimit = (min($this->memoryLimit, 256 * MB_IN_BYTES)); |
| 361 | |
| 362 | |
| 363 | if ((bool)Hooks::applyFilters(JobDataDto::FILTER_RESOURCES_IGNORE_MEMORY_LIMIT, false)) { |
| 364 | $this->memoryLimit = PHP_INT_MAX; |
| 365 | } |
| 366 | |
| 367 | return $this->memoryLimit; |
| 368 | } |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | protected function getScriptMemoryLimit() |
| 376 | { |
| 377 | |
| 378 | $this->scriptMemoryLimit = Hooks::applyFilters('wpstg.tests.resources.script_memory_limit', $this->scriptMemoryLimit); |
| 379 | |
| 380 | |
| 381 | if (isset($this->scriptMemoryLimit)) { |
| 382 | return $this->scriptMemoryLimit; |
| 383 | } |
| 384 | |
| 385 | |
| 386 | return $this->scriptMemoryLimit = $this->getMaxMemoryLimit() * 0.8; |
| 387 | } |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | protected function getCpuBoundMaxExecutionTime($cpuLoadSetting = null) |
| 400 | { |
| 401 | |
| 402 | if (!isset($this->resourceTraitSettings)) { |
| 403 | $this->resourceTraitSettings = json_decode(json_encode(get_option('wpstg_settings', []))); |
| 404 | } |
| 405 | |
| 406 | if ($cpuLoadSetting === null) { |
| 407 | $cpuLoadSetting = isset($this->resourceTraitSettings->cpuLoad) ? $this->resourceTraitSettings->cpuLoad : 'medium'; |
| 408 | } |
| 409 | |
| 410 | $execution_gap = static::$executionTimeGapInSeconds; |
| 411 | switch ($cpuLoadSetting) { |
| 412 | case 'low': |
| 413 | $cpuBoundMaxExecutionTime = 10 + $execution_gap; |
| 414 | break; |
| 415 | case 'medium': |
| 416 | default: |
| 417 | $cpuBoundMaxExecutionTime = 20 + $execution_gap; |
| 418 | break; |
| 419 | case 'high': |
| 420 | $cpuBoundMaxExecutionTime = 25 + $execution_gap; |
| 421 | break; |
| 422 | } |
| 423 | |
| 424 | return $cpuBoundMaxExecutionTime; |
| 425 | } |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | private function getPhpMaxExecutionTime() |
| 434 | { |
| 435 | return (int)ini_get('max_execution_time'); |
| 436 | } |
| 437 | |
| 438 | private function calculateInMemoryExtractionLimit(int $memoryLimit): int |
| 439 | { |
| 440 | if ($memoryLimit <= (256 * MB_IN_BYTES)) { |
| 441 | return MB_IN_BYTES; |
| 442 | } |
| 443 | |
| 444 | if ($memoryLimit <= (512 * MB_IN_BYTES)) { |
| 445 | return 2 * MB_IN_BYTES; |
| 446 | } |
| 447 | |
| 448 | if ($memoryLimit <= (1024 * MB_IN_BYTES)) { |
| 449 | return 4 * MB_IN_BYTES; |
| 450 | } |
| 451 | |
| 452 | return 8 * MB_IN_BYTES; |
| 453 | } |
| 454 | } |
| 455 |