PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.0.0
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.0.0
4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Backup / Task / AbstractTask.php
wp-staging / Backup / Task Last commit date
RestoreFileHandlers 3 years ago Tasks 2 years ago AbstractTask.php 2 years ago BackupTask.php 3 years ago FileBackupTask.php 2 years ago FileRestoreTask.php 3 years ago RestoreTask.php 3 years ago
AbstractTask.php
235 lines
1 <?php
2
3 namespace WPStaging\Backup\Task;
4
5 use WPStaging\Framework\Exceptions\WPStagingException;
6 use WPStaging\Framework\Queue\SeekableQueueInterface;
7 use WPStaging\Framework\Traits\ResourceTrait;
8 use WPStaging\Backup\Dto\JobDataDto;
9 use WPStaging\Backup\Dto\StepsDto;
10 use WPStaging\Backup\Dto\TaskResponseDto;
11 use WPStaging\Backup\Job\AbstractJob;
12 use WPStaging\Backup\Task\Tasks\JobBackup\FilesystemScannerTask;
13 use WPStaging\Backup\Task\Tasks\JobRestore\ExtractFilesTask;
14 use WPStaging\Vendor\Psr\Log\LoggerInterface;
15 use WPStaging\Framework\Utils\Cache\Cache;
16 use WPStaging\Core\Utils\Logger;
17
18 abstract class AbstractTask
19 {
20 use ResourceTrait;
21
22 /** @var Logger */
23 protected $logger;
24
25 /** @var Cache */
26 protected $cache;
27
28 /** @var bool */
29 protected $prepared;
30
31 // TODO RPoC
32 /** @var string|null */
33 protected $jobName;
34
35 /** @var int|null */
36 protected $jobId;
37
38 /** @var bool */
39 protected $debug;
40
41 /** @var StepsDto */
42 protected $stepsDto;
43
44 /** @var JobDataDto */
45 protected $jobDataDto;
46
47 /** @var AbstractJob */
48 protected $job;
49
50 /** @var SeekableQueueInterface */
51 protected $taskQueue;
52
53 public function __construct(LoggerInterface $logger, Cache $cache, StepsDto $stepsDto, SeekableQueueInterface $taskQueue)
54 {
55 /** @var Logger logger */
56 $this->logger = $logger; // @phpstan-ignore-line
57 $this->cache = $cache;
58 $this->stepsDto = $stepsDto;
59 $this->taskQueue = $taskQueue;
60
61 if (method_exists($this, 'init')) {
62 $this->init();
63 }
64 }
65
66 /**
67 * @return \WPStaging\Backup\Dto\TaskResponseDto
68 */
69 abstract public function execute();
70
71 /**
72 * @example 'backup_site_restore_themes'
73 * @return string
74 */
75 public static function getTaskName()
76 {
77 throw new WPStagingException('Any extending class MUST override the getTaskName method.');
78 }
79
80 /**
81 * @example 'Restoring Themes From Backup'
82 * @return string
83 */
84 public static function getTaskTitle()
85 {
86 throw new WPStagingException('Any extending class MUST override the getTaskTitle method.');
87 }
88
89 public function setJobContext(AbstractJob $job)
90 {
91 $this->cache->setLifetime(HOUR_IN_SECONDS);
92 $this->cache->setFilename('task_steps_' . static::getTaskName());
93
94 $this->stepsDto->hydrate($this->cache->get([
95 'current' => 0,
96 'total' => 0,
97 ]));
98
99 $this->job = $job;
100 }
101
102 public function setJobDataDto(JobDataDto $jobDataDto)
103 {
104 $this->jobDataDto = $jobDataDto;
105 $this->taskQueue->setup(static::getTaskName());
106 $this->taskQueue->seek($this->jobDataDto->getQueueOffset());
107 }
108
109 /**
110 * @var bool $incrementStep Whether to increment the step when generating a response or not.
111 * This might be false when you want to generate a response and still be
112 * able to retry the same step in the next request.
113 *
114 * @return \WPStaging\Backup\Dto\TaskResponseDto
115 */
116 public function generateResponse($incrementStep = true)
117 {
118 if ($incrementStep) {
119 $this->stepsDto->incrementCurrentStep();
120 }
121
122 // TODO Hydrate
123 $response = $this->getResponseDto();
124 $response->setIsRunning($this->stepsDto->isFinished());
125 $response->setPercentage($this->stepsDto->getPercentage());
126 $response->setTotal($this->stepsDto->getTotal());
127 $response->setStep($this->stepsDto->getCurrent());
128 $response->setTask($this->getTaskName());
129 $response->setStatusTitle(static::getTaskTitle());
130 $response->setJobId($this->jobDataDto->getId());
131
132 /*
133 * If this backup contains only a database, let's not display log entries
134 * for file-related tasks, as they expose internal behavior of the backup
135 * feature that are not relevant to the user.
136 */
137 if ($this->jobDataDto->getDatabaseOnlyBackup()) {
138 if (
139 !$this instanceof FilesystemScannerTask
140 && !$this instanceof FileBackupTask
141 && !$this instanceof ExtractFilesTask
142 ) {
143 $response->addMessage($this->logger->getLastLogMsg());
144 }
145 } else {
146 $response->addMessage($this->logger->getLastLogMsg());
147 }
148
149 $this->logger->setFileName(sprintf(
150 '%s__%s__%s',
151 $this->getJobName(),
152 date('Y_m_d__H'),
153 $this->getJobId()
154 ));
155
156 if ($this->stepsDto->isFinished()) {
157 $this->taskQueue->seek(0);
158 $this->jobDataDto->setQueueOffset(0);
159 $response->setPercentage(0);
160 $this->cache->delete();
161 } else {
162 $this->persistStepsDto();
163 }
164
165 $response = apply_filters('wpstg.task.response', $response);
166
167 return $response;
168 }
169
170 /**
171 * Save StepsDto to disk.
172 * This happens automatically during the shutdown process,
173 * but it can also be called manually.
174 */
175 public function persistStepsDto()
176 {
177 $this->cache->save($this->stepsDto->toArray(), true);
178 }
179
180 /**
181 * @return string|null
182 */
183 public function getJobName()
184 {
185 return $this->jobName;
186 }
187
188 /**
189 * @param string|null $jobName
190 */
191 public function setJobName($jobName)
192 {
193 $this->jobName = $jobName;
194 }
195
196 /**
197 * @return string|int|null
198 */
199 public function getJobId()
200 {
201 return $this->jobId;
202 }
203
204 /**
205 * @param string|int|null $jobId
206 */
207 public function setJobId($jobId)
208 {
209 $this->jobId = $jobId;
210 }
211
212 /**
213 * @param bool $debug
214 */
215 public function setDebug($debug)
216 {
217 $this->debug = (bool)$debug;
218 }
219
220 protected function getResponseDto()
221 {
222 return new TaskResponseDto();
223 }
224
225 public function getLogger()
226 {
227 return $this->logger;
228 }
229
230 public function getQueue()
231 {
232 return $this->taskQueue;
233 }
234 }
235