RestoreFileHandlers
2 years ago
Tasks
2 years ago
AbstractTask.php
2 years ago
BackupTask.php
3 years ago
FileBackupTask.php
2 years ago
FileRestoreTask.php
2 years ago
RestoreTask.php
2 years ago
AbstractTask.php
296 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backup\Task; |
| 4 | |
| 5 | use Exception; |
| 6 | use WPStaging\Framework\Exceptions\WPStagingException; |
| 7 | use WPStaging\Framework\Queue\SeekableQueueInterface; |
| 8 | use WPStaging\Framework\Traits\ResourceTrait; |
| 9 | use WPStaging\Backup\Dto\AbstractTaskDto; |
| 10 | use WPStaging\Backup\Dto\JobDataDto; |
| 11 | use WPStaging\Backup\Dto\StepsDto; |
| 12 | use WPStaging\Backup\Dto\TaskResponseDto; |
| 13 | use WPStaging\Backup\Job\AbstractJob; |
| 14 | use WPStaging\Backup\Task\Tasks\JobBackup\FilesystemScannerTask; |
| 15 | use WPStaging\Backup\Task\Tasks\JobRestore\ExtractFilesTask; |
| 16 | use WPStaging\Vendor\Psr\Log\LoggerInterface; |
| 17 | use WPStaging\Framework\Utils\Cache\Cache; |
| 18 | use WPStaging\Core\Utils\Logger; |
| 19 | use WPStaging\Core\WPStaging; |
| 20 | |
| 21 | abstract class AbstractTask |
| 22 | { |
| 23 | use ResourceTrait; |
| 24 | |
| 25 | /** @var Logger */ |
| 26 | protected $logger; |
| 27 | |
| 28 | /** @var Cache */ |
| 29 | protected $cache; |
| 30 | |
| 31 | /** @var bool */ |
| 32 | protected $prepared; |
| 33 | |
| 34 | // TODO RPoC |
| 35 | /** @var string|null */ |
| 36 | protected $jobName; |
| 37 | |
| 38 | /** @var int|null */ |
| 39 | protected $jobId; |
| 40 | |
| 41 | /** @var bool */ |
| 42 | protected $debug; |
| 43 | |
| 44 | /** @var StepsDto */ |
| 45 | protected $stepsDto; |
| 46 | |
| 47 | /** @var JobDataDto */ |
| 48 | protected $jobDataDto; |
| 49 | |
| 50 | /** @var AbstractJob */ |
| 51 | protected $job; |
| 52 | |
| 53 | /** @var AbstractTaskDto */ |
| 54 | protected $currentTaskDto; |
| 55 | |
| 56 | /** @var SeekableQueueInterface */ |
| 57 | protected $taskQueue; |
| 58 | |
| 59 | public function __construct(LoggerInterface $logger, Cache $cache, StepsDto $stepsDto, SeekableQueueInterface $taskQueue) |
| 60 | { |
| 61 | /** @var Logger logger */ |
| 62 | $this->logger = $logger; // @phpstan-ignore-line |
| 63 | $this->cache = $cache; |
| 64 | $this->stepsDto = $stepsDto; |
| 65 | $this->taskQueue = $taskQueue; |
| 66 | |
| 67 | if (method_exists($this, 'init')) { |
| 68 | $this->init(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @return TaskResponseDto |
| 74 | */ |
| 75 | abstract public function execute(); |
| 76 | |
| 77 | /** |
| 78 | * @example 'backup_site_restore_themes' |
| 79 | * @return string |
| 80 | */ |
| 81 | public static function getTaskName() |
| 82 | { |
| 83 | throw new WPStagingException('Any extending class MUST override the getTaskName method.'); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @example 'Restoring Themes From Backup' |
| 88 | * @return string |
| 89 | */ |
| 90 | public static function getTaskTitle() |
| 91 | { |
| 92 | throw new WPStagingException('Any extending class MUST override the getTaskTitle method.'); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @param AbstractJob $job |
| 97 | * @return void |
| 98 | */ |
| 99 | public function setJobContext(AbstractJob $job) |
| 100 | { |
| 101 | $this->cache->setLifetime(HOUR_IN_SECONDS); |
| 102 | $this->cache->setFilename('task_steps_' . static::getTaskName()); |
| 103 | |
| 104 | $this->stepsDto->hydrate($this->cache->get([ |
| 105 | 'current' => 0, |
| 106 | 'total' => 0, |
| 107 | ])); |
| 108 | |
| 109 | $this->job = $job; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @param JobDataDto $jobDataDto |
| 114 | * @return void |
| 115 | */ |
| 116 | public function setJobDataDto(JobDataDto $jobDataDto) |
| 117 | { |
| 118 | $this->jobDataDto = $jobDataDto; |
| 119 | $this->taskQueue->setup(static::getTaskName()); |
| 120 | $this->taskQueue->seek($this->jobDataDto->getQueueOffset()); |
| 121 | $this->setupCurrentTaskDto(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @var bool $incrementStep Whether to increment the step when generating a response or not. |
| 126 | * This might be false when you want to generate a response and still be |
| 127 | * able to retry the same step in the next request. |
| 128 | * |
| 129 | * @return TaskResponseDto |
| 130 | */ |
| 131 | public function generateResponse($incrementStep = true): TaskResponseDto |
| 132 | { |
| 133 | if ($incrementStep) { |
| 134 | $this->stepsDto->incrementCurrentStep(); |
| 135 | } |
| 136 | |
| 137 | // TODO Hydrate |
| 138 | $response = $this->getResponseDto(); |
| 139 | $response->setIsRunning(!$this->stepsDto->isFinished()); |
| 140 | $response->setPercentage($this->stepsDto->getPercentage()); |
| 141 | $response->setTotal($this->stepsDto->getTotal()); |
| 142 | $response->setStep($this->stepsDto->getCurrent()); |
| 143 | $response->setTask($this->getTaskName()); |
| 144 | $response->setStatusTitle(static::getTaskTitle()); |
| 145 | $response->setJobId($this->jobDataDto->getId()); |
| 146 | |
| 147 | /* |
| 148 | * If this backup contains only a database, let's not display log entries |
| 149 | * for file-related tasks, as they expose internal behavior of the backup |
| 150 | * feature that are not relevant to the user. |
| 151 | */ |
| 152 | if ($this->jobDataDto->getDatabaseOnlyBackup()) { |
| 153 | if ( |
| 154 | !$this instanceof FilesystemScannerTask |
| 155 | && !$this instanceof FileBackupTask |
| 156 | && !$this instanceof ExtractFilesTask |
| 157 | ) { |
| 158 | $response->addMessage($this->logger->getLastLogMsg()); |
| 159 | } |
| 160 | } else { |
| 161 | $response->addMessage($this->logger->getLastLogMsg()); |
| 162 | } |
| 163 | |
| 164 | $this->logger->setFileName(sprintf( |
| 165 | '%s__%s__%s', |
| 166 | $this->getJobName(), |
| 167 | date('Y_m_d__H'), |
| 168 | $this->getJobId() |
| 169 | )); |
| 170 | |
| 171 | if ($this->stepsDto->isFinished()) { |
| 172 | $this->taskQueue->seek(0); |
| 173 | $this->jobDataDto->setQueueOffset(0); |
| 174 | $response->setPercentage(0); |
| 175 | $this->jobDataDto->setCurrentTaskData([]); |
| 176 | $this->cache->delete(); |
| 177 | $this->jobDataDto->setCurrentTaskData([]); |
| 178 | } else { |
| 179 | $this->persistStepsDto(); |
| 180 | } |
| 181 | |
| 182 | $response = apply_filters('wpstg.task.response', $response); |
| 183 | |
| 184 | return $response; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Save StepsDto to disk. |
| 189 | * This happens automatically during the shutdown process, |
| 190 | * but it can also be called manually. |
| 191 | * @return void |
| 192 | */ |
| 193 | public function persistStepsDto() |
| 194 | { |
| 195 | $this->cache->save($this->stepsDto->toArray(), true); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * @return string|null |
| 200 | */ |
| 201 | public function getJobName() |
| 202 | { |
| 203 | return $this->jobName; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * @param string|null $jobName |
| 208 | */ |
| 209 | public function setJobName($jobName) |
| 210 | { |
| 211 | $this->jobName = $jobName; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * @return string|int|null |
| 216 | */ |
| 217 | public function getJobId() |
| 218 | { |
| 219 | return $this->jobId; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * @param string|int|null $jobId |
| 224 | */ |
| 225 | public function setJobId($jobId) |
| 226 | { |
| 227 | $this->jobId = $jobId; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * @param bool $debug |
| 232 | */ |
| 233 | public function setDebug($debug) |
| 234 | { |
| 235 | $this->debug = (bool)$debug; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * @return Logger |
| 240 | */ |
| 241 | public function getLogger(): Logger |
| 242 | { |
| 243 | return $this->logger; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * @return SeekableQueueInterface |
| 248 | */ |
| 249 | public function getQueue(): SeekableQueueInterface |
| 250 | { |
| 251 | return $this->taskQueue; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * @param AbstractTaskDto $taskDto |
| 256 | * @return void |
| 257 | */ |
| 258 | public function setCurrentTaskDto(AbstractTaskDto $taskDto) |
| 259 | { |
| 260 | $this->currentTaskDto = $taskDto; |
| 261 | $this->jobDataDto->setCurrentTaskData($taskDto->toArray()); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * @return TaskResponseDto |
| 266 | */ |
| 267 | protected function getResponseDto() |
| 268 | { |
| 269 | return new TaskResponseDto(); |
| 270 | } |
| 271 | |
| 272 | /** @return string */ |
| 273 | protected function getCurrentTaskType(): string |
| 274 | { |
| 275 | return ''; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * @return void |
| 280 | */ |
| 281 | protected function setupCurrentTaskDto() |
| 282 | { |
| 283 | $currentTaskType = $this->getCurrentTaskType(); |
| 284 | if (empty($currentTaskType) || !class_exists($currentTaskType)) { |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | try { |
| 289 | $currentTaskData = $this->jobDataDto->getCurrentTaskData(); |
| 290 | $this->currentTaskDto = WPStaging::make($currentTaskType); |
| 291 | $this->currentTaskDto->hydrateProperties($currentTaskData); |
| 292 | } catch (Exception $e) { |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 |