Cleaners
5 years ago
Exceptions
5 years ago
Cancel.php
2 years ago
CancelUpdate.php
2 years ago
Cloning.php
2 years ago
CloningProcess.php
2 years ago
Data.php
2 years ago
Database.php
2 years ago
Delete.php
2 years ago
Directories.php
2 years ago
Files.php
2 years ago
Finish.php
2 years ago
Job.php
2 years ago
JobExecutable.php
2 years ago
Logs.php
3 years ago
PreserveDataFirstStep.php
3 years ago
PreserveDataSecondStep.php
3 years ago
ProcessLock.php
2 years ago
Scan.php
2 years ago
SearchReplace.php
2 years ago
TotalStepsAreNumberOfTables.php
5 years ago
Updating.php
2 years ago
JobExecutable.php
145 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backend\Modules\Jobs; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | use WPStaging\Framework\Traits\MemoryExhaustTrait; |
| 7 | use WPStaging\Framework\Utils\Strings; |
| 8 | use WPStaging\Framework\Utils\ServerVars; |
| 9 | |
| 10 | // No Direct Access |
| 11 | if (!defined("WPINC")) { |
| 12 | die; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Class JobExecutable |
| 17 | * I'm sorry for such mess, we need to support PHP 5.3 |
| 18 | * @package WPStaging\Backend\Modules\Jobs |
| 19 | */ |
| 20 | abstract class JobExecutable extends Job |
| 21 | { |
| 22 | use MemoryExhaustTrait; |
| 23 | |
| 24 | /** |
| 25 | * @var Strings |
| 26 | */ |
| 27 | protected $strUtil; |
| 28 | |
| 29 | /** |
| 30 | * @var array |
| 31 | */ |
| 32 | protected $response = [ |
| 33 | "status" => false, |
| 34 | "percentage" => 0, |
| 35 | "total" => 0, |
| 36 | "step" => 0, |
| 37 | "last_msg" => '', |
| 38 | ]; |
| 39 | |
| 40 | /** |
| 41 | * JobExecutable constructor. |
| 42 | */ |
| 43 | public function __construct() |
| 44 | { |
| 45 | parent::__construct(); |
| 46 | |
| 47 | // Calculate total steps |
| 48 | $this->calculateTotalSteps(); |
| 49 | |
| 50 | // Set server settings for jobs (Do not set this globally as this will affect other plugins and even wp core then as well. Helpscout Ticket #9061) |
| 51 | // Set whether a client disconnect should abort script execution |
| 52 | @ignore_user_abort(true); |
| 53 | |
| 54 | // Set maximum execution time |
| 55 | WPStaging::make(ServerVars::class)->setTimeLimit(0); |
| 56 | |
| 57 | // Set maximum time in seconds a script is allowed to parse input data |
| 58 | @ini_set('max_input_time', '-1'); |
| 59 | |
| 60 | // Set maximum backtracking steps |
| 61 | @ini_set('pcre.backtrack_limit', PHP_INT_MAX); |
| 62 | |
| 63 | $this->strUtil = WPStaging::make(Strings::class); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Prepare Response Array |
| 68 | * @param bool $status false if not finished |
| 69 | * @param bool $incrementCurrentStep |
| 70 | * @return array |
| 71 | */ |
| 72 | protected function prepareResponse($status = false, $incrementCurrentStep = true) |
| 73 | { |
| 74 | if ($incrementCurrentStep) { |
| 75 | $this->options->currentStep++; |
| 76 | } |
| 77 | |
| 78 | $percentage = 0; |
| 79 | if (isset($this->options->currentStep) && isset($this->options->totalSteps) && $this->options->totalSteps > 0) { |
| 80 | $percentage = round(($this->options->currentStep / $this->options->totalSteps) * 100); |
| 81 | $percentage = ($percentage > 100) ? 100 : $percentage; |
| 82 | } |
| 83 | |
| 84 | $this->removeMemoryExhaustErrorTmpFile(); |
| 85 | return $this->response = [ |
| 86 | "status" => $status, |
| 87 | "percentage" => $percentage, |
| 88 | "total" => $this->options->totalSteps, |
| 89 | "step" => $this->options->currentStep, |
| 90 | "job" => $this->options->currentJob, |
| 91 | "last_msg" => $this->logger->getLastLogMsg(), |
| 92 | "job_done" => $status |
| 93 | ]; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Start Module |
| 98 | * @return object |
| 99 | */ |
| 100 | public function start() |
| 101 | { |
| 102 | // Execute steps |
| 103 | $this->run(); |
| 104 | |
| 105 | // Save option, progress |
| 106 | $this->saveOptions(); |
| 107 | |
| 108 | return (object) $this->response; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Run Steps |
| 113 | */ |
| 114 | protected function run() |
| 115 | { |
| 116 | // Execute steps |
| 117 | for ($i = 0; $i < $this->options->totalSteps; $i++) { |
| 118 | // Job is finished or over threshold limits was hit |
| 119 | if (!$this->execute()) { |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | // Return after every step to create lower batches |
| 124 | // This also gets a smoother progress bar and to a less consumptive php cpu load |
| 125 | // This decrease performance tremendous but also lowers memory consumption |
| 126 | if ($this->settings->cpuLoad === 'low') { |
| 127 | return (object) $this->response; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Calculate Total Steps in This Job and Assign It to $this->options->totalSteps |
| 134 | * @return void |
| 135 | */ |
| 136 | abstract protected function calculateTotalSteps(); |
| 137 | |
| 138 | /** |
| 139 | * Execute the Current Step |
| 140 | * Returns false when over threshold limits are hit or when the job is done, true otherwise |
| 141 | * @return bool |
| 142 | */ |
| 143 | abstract protected function execute(); |
| 144 | } |
| 145 |