Job
2 years ago
Service
2 years ago
Task
2 years ago
Traits
3 years ago
AbstractDto.php
3 years ago
AbstractTaskDto.php
2 years ago
JobDataDto.php
2 years ago
StepsDto.php
3 years ago
TaskResponseDto.php
2 years ago
JobDataDto.php
512 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 | /** |
| 87 | * @return string|int|null |
| 88 | */ |
| 89 | public function getId() |
| 90 | { |
| 91 | if (empty($this->id)) { |
| 92 | throw new \UnexpectedValueException('ID is not set'); |
| 93 | } |
| 94 | |
| 95 | return $this->id; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @param string|int|null $id |
| 100 | */ |
| 101 | public function setId($id) |
| 102 | { |
| 103 | $this->id = $id; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @return bool |
| 108 | */ |
| 109 | public function isInit() |
| 110 | { |
| 111 | return $this->init; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @param bool $init |
| 116 | */ |
| 117 | public function setInit($init) |
| 118 | { |
| 119 | $this->init = $init; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @return bool |
| 124 | */ |
| 125 | public function isFinished() |
| 126 | { |
| 127 | return $this->finished; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @param bool $finished |
| 132 | */ |
| 133 | public function setFinished($finished) |
| 134 | { |
| 135 | $this->finished = $finished; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @return bool |
| 140 | */ |
| 141 | public function isStatusCheck() |
| 142 | { |
| 143 | return $this->statusCheck; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * @param bool $statusCheck |
| 148 | */ |
| 149 | public function setStatusCheck($statusCheck) |
| 150 | { |
| 151 | $this->statusCheck = $statusCheck; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @return bool |
| 156 | */ |
| 157 | public function getIsSlowMySqlServer() |
| 158 | { |
| 159 | return $this->isSlowMySqlServer; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * @param bool |
| 164 | * @return void |
| 165 | */ |
| 166 | public function setIsSlowMySqlServer($isSlowMySqlServer) |
| 167 | { |
| 168 | $this->isSlowMySqlServer = $isSlowMySqlServer; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @return float|int |
| 173 | */ |
| 174 | public function getDbRequestTime() |
| 175 | { |
| 176 | return $this->dbRequestTime; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * @param float|int $dbRequestTime |
| 181 | */ |
| 182 | public function setDbRequestTime($dbRequestTime) |
| 183 | { |
| 184 | $this->dbRequestTime = $dbRequestTime; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * @return int |
| 189 | */ |
| 190 | public function getBatchSize() |
| 191 | { |
| 192 | return $this->batchSize; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * @param int $batchSize |
| 197 | */ |
| 198 | public function setBatchSize($batchSize) |
| 199 | { |
| 200 | $this->batchSize = $batchSize; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * @return string |
| 205 | */ |
| 206 | public function getLastQueryInfoJSON() |
| 207 | { |
| 208 | return $this->lastQueryInfoJSON; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * @param string $lastQueryInfoJSON |
| 213 | */ |
| 214 | public function setLastQueryInfoJSON($lastQueryInfoJSON) |
| 215 | { |
| 216 | if (is_array($lastQueryInfoJSON)) { |
| 217 | $lastQueryInfoJSON = json_encode($lastQueryInfoJSON); |
| 218 | debug_log('Trying to hydrate lastqueryinfoJSON with an array. String expected.'); |
| 219 | } |
| 220 | |
| 221 | $this->lastQueryInfoJSON = $lastQueryInfoJSON; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * @return int |
| 226 | */ |
| 227 | public function getTableAverageRowLength() |
| 228 | { |
| 229 | return $this->tableAverageRowLength; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * @param int $tableAverageRowLength |
| 234 | */ |
| 235 | public function setTableAverageRowLength($tableAverageRowLength) |
| 236 | { |
| 237 | $this->tableAverageRowLength = $tableAverageRowLength; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * @return string |
| 242 | */ |
| 243 | public function getTaskHealthName() |
| 244 | { |
| 245 | return $this->taskHealthName; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * @param string $taskHealthName |
| 250 | */ |
| 251 | public function setTaskHealthName($taskHealthName) |
| 252 | { |
| 253 | $this->taskHealthName = $taskHealthName; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * @return int |
| 258 | */ |
| 259 | public function getTaskHealthSequentialFailedRetries() |
| 260 | { |
| 261 | return $this->taskHealthSequentialFailedRetries; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * @param int $taskHealthSequentialFailedRetries |
| 266 | */ |
| 267 | public function setTaskHealthSequentialFailedRetries($taskHealthSequentialFailedRetries) |
| 268 | { |
| 269 | $this->taskHealthSequentialFailedRetries = $taskHealthSequentialFailedRetries; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * @return bool |
| 274 | */ |
| 275 | public function getTaskHealthResponded() |
| 276 | { |
| 277 | return $this->taskHealthResponded; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * @param bool $taskHealthResponded |
| 282 | */ |
| 283 | public function setTaskHealthResponded($taskHealthResponded) |
| 284 | { |
| 285 | $this->taskHealthResponded = $taskHealthResponded; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * @return bool |
| 290 | */ |
| 291 | public function getTaskHealthIsRetrying() |
| 292 | { |
| 293 | return $this->taskHealthIsRetrying; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * @param bool $taskHealthIsRetrying |
| 298 | */ |
| 299 | public function setTaskHealthIsRetrying($taskHealthIsRetrying) |
| 300 | { |
| 301 | $this->taskHealthIsRetrying = $taskHealthIsRetrying; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * @return int |
| 306 | */ |
| 307 | public function getQueueOffset() |
| 308 | { |
| 309 | return (int)$this->queueOffset; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * @param bool $queueOffset |
| 314 | */ |
| 315 | public function setQueueOffset($queueOffset) |
| 316 | { |
| 317 | $this->queueOffset = (int)$queueOffset; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * @return int |
| 322 | */ |
| 323 | public function getQueueCount() |
| 324 | { |
| 325 | return (int)$this->queueCount; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * @param int $queueCount |
| 330 | */ |
| 331 | public function setQueueCount($queueCount) |
| 332 | { |
| 333 | $this->queueCount = (int)$queueCount; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * @return bool |
| 338 | */ |
| 339 | public function getDatabaseOnlyBackup() |
| 340 | { |
| 341 | return (bool)$this->databaseOnlyBackup; |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * @param bool $databaseOnlyBackup |
| 346 | */ |
| 347 | public function setDatabaseOnlyBackup($databaseOnlyBackup) |
| 348 | { |
| 349 | $this->databaseOnlyBackup = (bool)$databaseOnlyBackup; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * @return string |
| 354 | */ |
| 355 | public function getRequirementFailReason() |
| 356 | { |
| 357 | return $this->requirementFailReason; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * @param string $requirementFailReason |
| 362 | */ |
| 363 | public function setRequirementFailReason($requirementFailReason) |
| 364 | { |
| 365 | $this->requirementFailReason = $requirementFailReason; |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * @return int |
| 370 | */ |
| 371 | public function getStartTime() |
| 372 | { |
| 373 | return $this->startTime; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * @param int $startTime |
| 378 | */ |
| 379 | public function setStartTime($startTime) |
| 380 | { |
| 381 | $this->startTime = $startTime; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * @return int |
| 386 | */ |
| 387 | public function getEndTime() |
| 388 | { |
| 389 | return $this->endTime; |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * @param int $endTime |
| 394 | */ |
| 395 | public function setEndTime($endTime) |
| 396 | { |
| 397 | $this->endTime = $endTime; |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * This method expects the job to have finished successfully, otherwise it will return zero. |
| 402 | * |
| 403 | * @return int |
| 404 | */ |
| 405 | public function getDuration() |
| 406 | { |
| 407 | if (is_int($this->startTime) && is_int($this->endTime)) { |
| 408 | return $this->endTime - $this->startTime; |
| 409 | } |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * @param int $duration |
| 416 | */ |
| 417 | public function setDuration($duration) |
| 418 | { |
| 419 | $this->duration = $duration; |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * @return bool |
| 424 | */ |
| 425 | public function isCleaned() |
| 426 | { |
| 427 | return $this->cleaned; |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * @param bool $cleaned |
| 432 | */ |
| 433 | public function setCleaned($cleaned = true) |
| 434 | { |
| 435 | $this->cleaned = $cleaned; |
| 436 | } |
| 437 | |
| 438 | /** @param int $index */ |
| 439 | public function setCurrentTaskIndex($index) |
| 440 | { |
| 441 | $this->currentTaskIndex = $index; |
| 442 | } |
| 443 | |
| 444 | /** @return int */ |
| 445 | public function getCurrentTaskIndex() |
| 446 | { |
| 447 | return $this->currentTaskIndex; |
| 448 | } |
| 449 | |
| 450 | /** @param array $queue */ |
| 451 | public function setTaskQueue($queue) |
| 452 | { |
| 453 | $this->taskQueue = $queue; |
| 454 | } |
| 455 | |
| 456 | /** @return array */ |
| 457 | public function getTaskQueue() |
| 458 | { |
| 459 | return $this->taskQueue; |
| 460 | } |
| 461 | |
| 462 | /** @return string */ |
| 463 | public function getCurrentTask() |
| 464 | { |
| 465 | if (empty($this->taskQueue[$this->currentTaskIndex])) { |
| 466 | $debugTaskQueue = print_r($this->taskQueue, true); |
| 467 | debug_log("getCurrenTask queue is empty $debugTaskQueue Current task index: $this->currentTaskIndex"); |
| 468 | return ''; |
| 469 | } |
| 470 | |
| 471 | return $this->taskQueue[$this->currentTaskIndex]; |
| 472 | } |
| 473 | |
| 474 | /** @throws FinishedQueueException */ |
| 475 | public function moveToNextTask() |
| 476 | { |
| 477 | if (count($this->taskQueue) === $this->currentTaskIndex + 1) { |
| 478 | throw new FinishedQueueException(); |
| 479 | } |
| 480 | |
| 481 | $this->currentTaskIndex++; |
| 482 | } |
| 483 | |
| 484 | /** @return int */ |
| 485 | public function getRetries() |
| 486 | { |
| 487 | return $this->retries; |
| 488 | } |
| 489 | |
| 490 | /** @param int $retries */ |
| 491 | public function setRetries($retries) |
| 492 | { |
| 493 | $this->retries = $retries; |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * @return array |
| 498 | */ |
| 499 | public function getCurrentTaskData(): array |
| 500 | { |
| 501 | return $this->currentTaskData; |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * @param array $currentTaskData |
| 506 | */ |
| 507 | public function setCurrentTaskData(array $currentTaskData) |
| 508 | { |
| 509 | $this->currentTaskData = $currentTaskData; |
| 510 | } |
| 511 | } |
| 512 |