Interfaces
2 years ago
Job
2 years ago
Service
2 years ago
Task
2 years ago
Traits
2 years ago
AbstractDto.php
3 years ago
AbstractTaskDto.php
2 years ago
JobDataDto.php
2 years ago
StepsDto.php
2 years ago
TaskResponseDto.php
2 years ago
JobDataDto.php
531 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backup\Dto; |
| 4 | |
| 5 | use WPStaging\Framework\Queue\FinishedQueueException; |
| 6 | |
| 7 | use function WPStaging\functions\debug_log; |
| 8 | |
| 9 | class JobDataDto extends AbstractDto |
| 10 | { |
| 11 | /** @var string|int|null */ |
| 12 | protected $id; |
| 13 | |
| 14 | /** @var bool */ |
| 15 | protected $init; |
| 16 | |
| 17 | /** @var bool */ |
| 18 | protected $finished; |
| 19 | |
| 20 | /** @var bool */ |
| 21 | protected $statusCheck; |
| 22 | |
| 23 | /** @var string */ |
| 24 | protected $lastQueryInfoJSON; |
| 25 | |
| 26 | /** @var bool */ |
| 27 | protected $isSlowMySqlServer = false; |
| 28 | |
| 29 | /** @var double */ |
| 30 | protected $dbRequestTime = 0; |
| 31 | |
| 32 | /** @var int */ |
| 33 | protected $batchSize = 0; |
| 34 | |
| 35 | /** @var int */ |
| 36 | private $tableAverageRowLength = 0; |
| 37 | |
| 38 | /** @var string The name of the task we are checking the health */ |
| 39 | protected $taskHealthName = ''; |
| 40 | |
| 41 | /** @var int How many times this task failed in sequence */ |
| 42 | protected $taskHealthSequentialFailedRetries = 0; |
| 43 | |
| 44 | /** @var bool Whether the task has responded */ |
| 45 | protected $taskHealthResponded = false; |
| 46 | |
| 47 | /** @var bool Whether the task is currently retrying a request that failed */ |
| 48 | protected $taskHealthIsRetrying = false; |
| 49 | |
| 50 | /** @var int Where to set the Task queue offset */ |
| 51 | protected $queueOffset = 0; |
| 52 | |
| 53 | /** @var int Calculating the queue count is expensive, so we store it here as a metadata */ |
| 54 | protected $queueCount = 0; |
| 55 | |
| 56 | /** @var bool Whether this backup contains only a database */ |
| 57 | protected $databaseOnlyBackup = false; |
| 58 | |
| 59 | /** @var string The reason why a requirement fail, if it failed. */ |
| 60 | protected $requirementFailReason = ''; |
| 61 | |
| 62 | /** @var int Unix timestamp of when this job started. */ |
| 63 | protected $startTime; |
| 64 | |
| 65 | /** @var int Unix timestamp of when this job finished, if it finished at all. */ |
| 66 | protected $endTime; |
| 67 | |
| 68 | /** @var int How long this job took to run, in seconds. */ |
| 69 | protected $duration; |
| 70 | |
| 71 | /** @var bool Whether this job cleaned. */ |
| 72 | protected $cleaned; |
| 73 | |
| 74 | /** @var array list of tasks to be performed in this job */ |
| 75 | protected $taskQueue; |
| 76 | |
| 77 | /** @var int pointer|index to the current task in the queue */ |
| 78 | protected $currentTaskIndex; |
| 79 | |
| 80 | /** @var int how often a request is retried */ |
| 81 | protected $retries; |
| 82 | |
| 83 | /** @var array Data for the current task. */ |
| 84 | protected $currentTaskData = []; |
| 85 | |
| 86 | /** @var bool */ |
| 87 | protected $isWpCliRequest = false; |
| 88 | |
| 89 | /** |
| 90 | * @return string|int|null |
| 91 | */ |
| 92 | public function getId() |
| 93 | { |
| 94 | if (empty($this->id)) { |
| 95 | throw new \UnexpectedValueException('ID is not set'); |
| 96 | } |
| 97 | |
| 98 | return $this->id; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @param string|int|null $id |
| 103 | */ |
| 104 | public function setId($id) |
| 105 | { |
| 106 | $this->id = $id; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @return bool |
| 111 | */ |
| 112 | public function isInit() |
| 113 | { |
| 114 | return $this->init; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @param bool $init |
| 119 | */ |
| 120 | public function setInit($init) |
| 121 | { |
| 122 | $this->init = $init; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @return bool |
| 127 | */ |
| 128 | public function isFinished() |
| 129 | { |
| 130 | return $this->finished; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * @param bool $finished |
| 135 | */ |
| 136 | public function setFinished($finished) |
| 137 | { |
| 138 | $this->finished = $finished; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @return bool |
| 143 | */ |
| 144 | public function isStatusCheck() |
| 145 | { |
| 146 | return $this->statusCheck; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @param bool $statusCheck |
| 151 | */ |
| 152 | public function setStatusCheck($statusCheck) |
| 153 | { |
| 154 | $this->statusCheck = $statusCheck; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @return bool |
| 159 | */ |
| 160 | public function getIsSlowMySqlServer() |
| 161 | { |
| 162 | return $this->isSlowMySqlServer; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @param bool |
| 167 | * @return void |
| 168 | */ |
| 169 | public function setIsSlowMySqlServer($isSlowMySqlServer) |
| 170 | { |
| 171 | $this->isSlowMySqlServer = $isSlowMySqlServer; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * @return float|int |
| 176 | */ |
| 177 | public function getDbRequestTime() |
| 178 | { |
| 179 | return $this->dbRequestTime; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * @param float|int $dbRequestTime |
| 184 | */ |
| 185 | public function setDbRequestTime($dbRequestTime) |
| 186 | { |
| 187 | $this->dbRequestTime = $dbRequestTime; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * @return int |
| 192 | */ |
| 193 | public function getBatchSize() |
| 194 | { |
| 195 | return $this->batchSize; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * @param int $batchSize |
| 200 | */ |
| 201 | public function setBatchSize($batchSize) |
| 202 | { |
| 203 | $this->batchSize = $batchSize; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * @return string |
| 208 | */ |
| 209 | public function getLastQueryInfoJSON() |
| 210 | { |
| 211 | return $this->lastQueryInfoJSON; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * @param string $lastQueryInfoJSON |
| 216 | */ |
| 217 | public function setLastQueryInfoJSON($lastQueryInfoJSON) |
| 218 | { |
| 219 | if (is_array($lastQueryInfoJSON)) { |
| 220 | $lastQueryInfoJSON = json_encode($lastQueryInfoJSON); |
| 221 | debug_log('Trying to hydrate lastqueryinfoJSON with an array. String expected.'); |
| 222 | } |
| 223 | |
| 224 | $this->lastQueryInfoJSON = $lastQueryInfoJSON; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * @return int |
| 229 | */ |
| 230 | public function getTableAverageRowLength() |
| 231 | { |
| 232 | return $this->tableAverageRowLength; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * @param int $tableAverageRowLength |
| 237 | */ |
| 238 | public function setTableAverageRowLength($tableAverageRowLength) |
| 239 | { |
| 240 | $this->tableAverageRowLength = $tableAverageRowLength; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * @return string |
| 245 | */ |
| 246 | public function getTaskHealthName() |
| 247 | { |
| 248 | return $this->taskHealthName; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * @param string $taskHealthName |
| 253 | */ |
| 254 | public function setTaskHealthName($taskHealthName) |
| 255 | { |
| 256 | $this->taskHealthName = $taskHealthName; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * @return int |
| 261 | */ |
| 262 | public function getTaskHealthSequentialFailedRetries() |
| 263 | { |
| 264 | return $this->taskHealthSequentialFailedRetries; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * @param int $taskHealthSequentialFailedRetries |
| 269 | */ |
| 270 | public function setTaskHealthSequentialFailedRetries($taskHealthSequentialFailedRetries) |
| 271 | { |
| 272 | $this->taskHealthSequentialFailedRetries = $taskHealthSequentialFailedRetries; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * @return bool |
| 277 | */ |
| 278 | public function getTaskHealthResponded() |
| 279 | { |
| 280 | return $this->taskHealthResponded; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * @param bool $taskHealthResponded |
| 285 | */ |
| 286 | public function setTaskHealthResponded($taskHealthResponded) |
| 287 | { |
| 288 | $this->taskHealthResponded = $taskHealthResponded; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * @return bool |
| 293 | */ |
| 294 | public function getTaskHealthIsRetrying() |
| 295 | { |
| 296 | return $this->taskHealthIsRetrying; |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * @param bool $taskHealthIsRetrying |
| 301 | */ |
| 302 | public function setTaskHealthIsRetrying($taskHealthIsRetrying) |
| 303 | { |
| 304 | $this->taskHealthIsRetrying = $taskHealthIsRetrying; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * @return int |
| 309 | */ |
| 310 | public function getQueueOffset() |
| 311 | { |
| 312 | return (int)$this->queueOffset; |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * @param bool $queueOffset |
| 317 | */ |
| 318 | public function setQueueOffset($queueOffset) |
| 319 | { |
| 320 | $this->queueOffset = (int)$queueOffset; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * @return int |
| 325 | */ |
| 326 | public function getQueueCount() |
| 327 | { |
| 328 | return (int)$this->queueCount; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * @param int $queueCount |
| 333 | */ |
| 334 | public function setQueueCount($queueCount) |
| 335 | { |
| 336 | $this->queueCount = (int)$queueCount; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * @return bool |
| 341 | */ |
| 342 | public function getDatabaseOnlyBackup() |
| 343 | { |
| 344 | return (bool)$this->databaseOnlyBackup; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * @param bool $databaseOnlyBackup |
| 349 | */ |
| 350 | public function setDatabaseOnlyBackup($databaseOnlyBackup) |
| 351 | { |
| 352 | $this->databaseOnlyBackup = (bool)$databaseOnlyBackup; |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * @return string |
| 357 | */ |
| 358 | public function getRequirementFailReason() |
| 359 | { |
| 360 | return $this->requirementFailReason; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * @param string $requirementFailReason |
| 365 | */ |
| 366 | public function setRequirementFailReason($requirementFailReason) |
| 367 | { |
| 368 | $this->requirementFailReason = $requirementFailReason; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * @return int |
| 373 | */ |
| 374 | public function getStartTime() |
| 375 | { |
| 376 | return $this->startTime; |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * @param int $startTime |
| 381 | */ |
| 382 | public function setStartTime($startTime) |
| 383 | { |
| 384 | $this->startTime = $startTime; |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * @return int |
| 389 | */ |
| 390 | public function getEndTime() |
| 391 | { |
| 392 | return $this->endTime; |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * @param int $endTime |
| 397 | */ |
| 398 | public function setEndTime($endTime) |
| 399 | { |
| 400 | $this->endTime = $endTime; |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * This method expects the job to have finished successfully, otherwise it will return zero. |
| 405 | * |
| 406 | * @return int |
| 407 | */ |
| 408 | public function getDuration() |
| 409 | { |
| 410 | if (is_int($this->startTime) && is_int($this->endTime)) { |
| 411 | return $this->endTime - $this->startTime; |
| 412 | } |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * @param int $duration |
| 419 | */ |
| 420 | public function setDuration($duration) |
| 421 | { |
| 422 | $this->duration = $duration; |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * @return bool |
| 427 | */ |
| 428 | public function isCleaned() |
| 429 | { |
| 430 | return $this->cleaned; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * @param bool $cleaned |
| 435 | */ |
| 436 | public function setCleaned($cleaned = true) |
| 437 | { |
| 438 | $this->cleaned = $cleaned; |
| 439 | } |
| 440 | |
| 441 | /** @param int $index */ |
| 442 | public function setCurrentTaskIndex($index) |
| 443 | { |
| 444 | $this->currentTaskIndex = $index; |
| 445 | } |
| 446 | |
| 447 | /** @return int */ |
| 448 | public function getCurrentTaskIndex() |
| 449 | { |
| 450 | return $this->currentTaskIndex; |
| 451 | } |
| 452 | |
| 453 | /** @param array $queue */ |
| 454 | public function setTaskQueue($queue) |
| 455 | { |
| 456 | $this->taskQueue = $queue; |
| 457 | } |
| 458 | |
| 459 | /** @return array */ |
| 460 | public function getTaskQueue() |
| 461 | { |
| 462 | return $this->taskQueue; |
| 463 | } |
| 464 | |
| 465 | /** @return string */ |
| 466 | public function getCurrentTask() |
| 467 | { |
| 468 | if (empty($this->taskQueue[$this->currentTaskIndex])) { |
| 469 | $debugTaskQueue = print_r($this->taskQueue, true); |
| 470 | debug_log("getCurrenTask queue is empty $debugTaskQueue Current task index: $this->currentTaskIndex"); |
| 471 | return ''; |
| 472 | } |
| 473 | |
| 474 | return $this->taskQueue[$this->currentTaskIndex]; |
| 475 | } |
| 476 | |
| 477 | /** @throws FinishedQueueException */ |
| 478 | public function moveToNextTask() |
| 479 | { |
| 480 | if (count($this->taskQueue) === $this->currentTaskIndex + 1) { |
| 481 | throw new FinishedQueueException(); |
| 482 | } |
| 483 | |
| 484 | $this->currentTaskIndex++; |
| 485 | } |
| 486 | |
| 487 | /** @return int */ |
| 488 | public function getRetries() |
| 489 | { |
| 490 | return $this->retries; |
| 491 | } |
| 492 | |
| 493 | /** @param int $retries */ |
| 494 | public function setRetries($retries) |
| 495 | { |
| 496 | $this->retries = $retries; |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * @return array |
| 501 | */ |
| 502 | public function getCurrentTaskData(): array |
| 503 | { |
| 504 | return $this->currentTaskData; |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * @param array $currentTaskData |
| 509 | */ |
| 510 | public function setCurrentTaskData(array $currentTaskData) |
| 511 | { |
| 512 | $this->currentTaskData = $currentTaskData; |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * @return bool |
| 517 | */ |
| 518 | public function getIsWpCliRequest(): bool |
| 519 | { |
| 520 | return $this->isWpCliRequest; |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * @param bool $isWpCliRequest |
| 525 | */ |
| 526 | public function setIsWpCliRequest(bool $isWpCliRequest) |
| 527 | { |
| 528 | $this->isWpCliRequest = $isWpCliRequest; |
| 529 | } |
| 530 | } |
| 531 |