Tasks
1 week ago
BackupTask.php
1 year ago
FileBackupTask.php
6 months ago
FileRestoreTask.php
1 year ago
RestoreTask.php
1 year ago
FileBackupTask.php
95 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Base class for file backup tasks that archive WordPress files into backups |
| 5 | * |
| 6 | * Manages the coordination between file discovery and archiving, handling large files |
| 7 | * across multiple requests while tracking progress and managing graceful shutdowns. |
| 8 | */ |
| 9 | |
| 10 | namespace WPStaging\Backup\Task; |
| 11 | |
| 12 | use WPStaging\Backup\Service\FileBackupService; |
| 13 | use WPStaging\Backup\Service\FileBackupServiceProvider; |
| 14 | use WPStaging\Backup\Task\BackupTask; |
| 15 | use WPStaging\Framework\Job\Dto\StepsDto; |
| 16 | use WPStaging\Framework\Job\Dto\TaskResponseDto; |
| 17 | use WPStaging\Framework\Queue\SeekableQueueInterface; |
| 18 | use WPStaging\Framework\Utils\Cache\Cache; |
| 19 | use WPStaging\Vendor\Psr\Log\LoggerInterface; |
| 20 | |
| 21 | abstract class FileBackupTask extends BackupTask |
| 22 | { |
| 23 | /** |
| 24 | * Whether if the file backup task gracefully shuts down |
| 25 | */ |
| 26 | const TRANSIENT_GRACEFUL_SHUTDOWN = 'wpstg_file_backup_task'; |
| 27 | |
| 28 | /** @var FileBackupService */ |
| 29 | protected $fileBackupService; |
| 30 | |
| 31 | public function __construct(FileBackupServiceProvider $fileBackupServiceProvider, LoggerInterface $logger, Cache $cache, StepsDto $stepsDto, SeekableQueueInterface $taskQueue) |
| 32 | { |
| 33 | parent::__construct($logger, $cache, $stepsDto, $taskQueue); |
| 34 | $this->fileBackupService = $fileBackupServiceProvider->getService(); // @phpstan-ignore-line |
| 35 | } |
| 36 | |
| 37 | public static function getTaskName(): string |
| 38 | { |
| 39 | return 'backup_file_task'; |
| 40 | } |
| 41 | |
| 42 | public static function getTaskTitle(): string |
| 43 | { |
| 44 | return 'Adding Files to Backup'; |
| 45 | } |
| 46 | |
| 47 | public function execute(): TaskResponseDto |
| 48 | { |
| 49 | $this->prepareFileBackupTask(); |
| 50 | set_transient(self::TRANSIENT_GRACEFUL_SHUTDOWN, '1', 60); |
| 51 | $this->fileBackupService->setupArchiver($this->getFileIdentifier(), $this->isOtherWpRootFilesTask()); |
| 52 | $this->fileBackupService->execute(); |
| 53 | |
| 54 | delete_transient(self::TRANSIENT_GRACEFUL_SHUTDOWN); |
| 55 | return $this->generateResponse(false); |
| 56 | } |
| 57 | |
| 58 | /** @return string */ |
| 59 | abstract protected function getFileIdentifier(): string; |
| 60 | |
| 61 | /** |
| 62 | * @return void |
| 63 | */ |
| 64 | private function prepareFileBackupTask() |
| 65 | { |
| 66 | $this->fileBackupService->inject($this, $this->taskQueue, $this->logger, $this->jobDataDto, $this->stepsDto); |
| 67 | if ($this->stepsDto->getTotal() > 0) { |
| 68 | $this->checkIfLastRequestGracefulShutdown(); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | $this->stepsDto->setTotal($this->jobDataDto->getDiscoveredFilesByCategory($this->getFileIdentifier())); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @return bool |
| 77 | */ |
| 78 | protected function isOtherWpRootFilesTask(): bool |
| 79 | { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | protected function checkIfLastRequestGracefulShutdown() |
| 84 | { |
| 85 | $transient = get_transient(self::TRANSIENT_GRACEFUL_SHUTDOWN); |
| 86 | // empty that mean it was graceful shutdown |
| 87 | if (empty($transient)) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | $this->logger->debug('Resuming file backup task after a non-graceful shutdown.'); |
| 92 | $this->fileBackupService->setIsGracefulShutdown(false); |
| 93 | } |
| 94 | } |
| 95 |