ISGArchiveDelegate.php
3 years ago
SGBGArchive.php
3 years ago
SGBGArchiveCdr.php
3 years ago
SGBGArchiveHelper.php
3 years ago
SGBGCache.php
3 years ago
SGBGCacheableFile.php
3 years ago
SGBGDirectoryTreeFile.php
3 years ago
SGBGFile.php
3 years ago
SGBGFileHelper.php
3 years ago
SGBGJsonFile.php
3 years ago
SGBGLog.php
3 years ago
SGBGReloader.php
3 years ago
SGBGStateFile.php
3 years ago
SGBGTask.php
3 years ago
SGBGStateFile.php
586 lines
| 1 | <?php |
| 2 | /* |
| 3 | @ class StateFile |
| 4 | @ version 1.0.1 |
| 5 | @ updated 24/01/2021 |
| 6 | */ |
| 7 | |
| 8 | require_once(__DIR__.'/SGBGJsonFile.php'); |
| 9 | require_once(__DIR__.'/SGBGCacheableFile.php'); |
| 10 | |
| 11 | class SGBGStateFile extends SGBGCacheableFile |
| 12 | { |
| 13 | use SGBGJsonFile; |
| 14 | |
| 15 | const STATUS_READY = 0; |
| 16 | const STATUS_STARTED = 1; |
| 17 | const STATUS_BUSY = 2; |
| 18 | const STATUS_RESUME = 3; |
| 19 | const STATUS_DONE = 4; |
| 20 | const STATUS_ERROR = 5; |
| 21 | |
| 22 | private $count = 0; |
| 23 | private $offset = 0; |
| 24 | private $status = 0; |
| 25 | private $data = []; |
| 26 | private $startTs = 0; |
| 27 | private $updateTs = 0; |
| 28 | private $action = null; |
| 29 | private $inprogress = false; |
| 30 | private $tablesToBackup = ''; |
| 31 | private $activeDirectory = ''; |
| 32 | private $currentUploadChunksCount = 0; |
| 33 | private $totalUploadChunksCount; |
| 34 | private $chunkSize = 0; |
| 35 | private $progress = 0; |
| 36 | private $storageType; |
| 37 | private $pendingStorageUploads = array(); |
| 38 | private $token; |
| 39 | private $type; |
| 40 | private $backedUpTables; |
| 41 | private $uploadId = 0; |
| 42 | private $actionId = 0; |
| 43 | private $parts = array(); |
| 44 | private $backupFileName= ''; |
| 45 | private $progressCursor; |
| 46 | private $numberOfEntries; |
| 47 | private $restoreMode; |
| 48 | |
| 49 | /** |
| 50 | * |
| 51 | * Constructor. |
| 52 | * |
| 53 | * @param string $path absolute file path |
| 54 | * @return null |
| 55 | */ |
| 56 | public function __construct($path) |
| 57 | { |
| 58 | parent::__construct($path); |
| 59 | |
| 60 | $this->status = SGBGStateFile::STATUS_READY; |
| 61 | $this->startTs = time(); |
| 62 | $this->updateTs = time(); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | /** |
| 67 | * |
| 68 | * Get count. |
| 69 | * |
| 70 | * @return int the count |
| 71 | */ |
| 72 | public function getCount() |
| 73 | { |
| 74 | return $this->count; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * |
| 79 | * Set count. |
| 80 | * |
| 81 | * @param int $count the count |
| 82 | * @return null |
| 83 | */ |
| 84 | public function setCount($count) |
| 85 | { |
| 86 | $this->count = $count; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * |
| 91 | * Get count. |
| 92 | * |
| 93 | * @return int the count |
| 94 | */ |
| 95 | public function getAction() |
| 96 | { |
| 97 | return $this->action; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * |
| 102 | * Set count. $this->archive->setLogEnabled($logEnabled); |
| 103 | * |
| 104 | * @param string $action the action |
| 105 | * @return null |
| 106 | */ |
| 107 | public function setAction( $action ) |
| 108 | { |
| 109 | $this->action = $action; |
| 110 | } |
| 111 | |
| 112 | public function getInprogress() |
| 113 | { |
| 114 | return $this->inprogress; |
| 115 | } |
| 116 | |
| 117 | public function setInprogress( $value ) |
| 118 | { |
| 119 | $this->inprogress = $value; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * |
| 124 | * Get offest. |
| 125 | * |
| 126 | * @return int the offset |
| 127 | */ |
| 128 | public function getOffset() |
| 129 | { |
| 130 | return $this->offset; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * |
| 135 | * Set offset. |
| 136 | * |
| 137 | * @param int $offest the offset |
| 138 | * @return null |
| 139 | */ |
| 140 | public function setOffset($offset) |
| 141 | { |
| 142 | $this->offset = max($offset, 0); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * |
| 147 | * Get status. |
| 148 | * |
| 149 | * @return int the status |
| 150 | */ |
| 151 | public function getStatus() |
| 152 | { |
| 153 | return $this->status; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * |
| 158 | * Set status. |
| 159 | * |
| 160 | * @param int $status the status |
| 161 | * @return null |
| 162 | */ |
| 163 | public function setStatus($status) |
| 164 | { |
| 165 | $this->status = $status; |
| 166 | } |
| 167 | |
| 168 | public function getTablesToBackup() |
| 169 | { |
| 170 | return $this->tablesToBackup; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | public function setTablesToBackup($tablesToBackup) |
| 175 | { |
| 176 | $this->tablesToBackup = $tablesToBackup; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * |
| 181 | * Get data. |
| 182 | * If provided key is null, return all data. |
| 183 | * |
| 184 | * @param string $key the key |
| 185 | * @return mixed the data related to the key |
| 186 | */ |
| 187 | public function getData($key = null) |
| 188 | { |
| 189 | if (empty($key)) { |
| 190 | return $this->data; |
| 191 | } |
| 192 | |
| 193 | if (isset($this->data[$key])) { |
| 194 | return $this->data[$key]; |
| 195 | } |
| 196 | |
| 197 | return null; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * |
| 202 | * Set data. |
| 203 | * |
| 204 | * @param string $key the key |
| 205 | * @param mixed $data the data |
| 206 | * @return null |
| 207 | */ |
| 208 | public function setData($key, $data) |
| 209 | { |
| 210 | if (empty($key)) { |
| 211 | $this->data = $data; |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | $this->data[$key] = $data; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * |
| 220 | * Get start timestamp. |
| 221 | * |
| 222 | * @return int the starting timestamp |
| 223 | */ |
| 224 | public function getStartTs() |
| 225 | { |
| 226 | return $this->startTs; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * |
| 231 | * Set start timestamp. |
| 232 | * |
| 233 | * @param int $startTs the starting timestamp |
| 234 | * @return null |
| 235 | */ |
| 236 | public function setStartTs($startTs) |
| 237 | { |
| 238 | $this->startTs = $startTs; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * |
| 243 | * Get update timestamp. |
| 244 | * |
| 245 | * @return int the update timestamp |
| 246 | */ |
| 247 | public function getUpdateTs() |
| 248 | { |
| 249 | return $this->updateTs; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * |
| 254 | * Set update timestamp. |
| 255 | * |
| 256 | * @param int $updateTs the update timestamp |
| 257 | * @return null |
| 258 | */ |
| 259 | public function setUpdateTs($updateTs) |
| 260 | { |
| 261 | $this->updateTs = $updateTs; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * |
| 266 | * Initialize state object from array. |
| 267 | * |
| 268 | * @param array $arr array containing state data |
| 269 | * @return bool false if any data is missing; otherwise true |
| 270 | */ |
| 271 | |
| 272 | public function getActiveDirectory() |
| 273 | { |
| 274 | return $this->activeDirectory; |
| 275 | } |
| 276 | |
| 277 | public function setActiveDirectory($activeDirectory) |
| 278 | { |
| 279 | $this->activeDirectory = $activeDirectory; |
| 280 | } |
| 281 | |
| 282 | public function getChunkSize() |
| 283 | { |
| 284 | return $this->chunkSize; |
| 285 | } |
| 286 | |
| 287 | public function setChunkSize($chunkSize) |
| 288 | { |
| 289 | $this->chunkSize = $chunkSize; |
| 290 | } |
| 291 | |
| 292 | public function getProgress() |
| 293 | { |
| 294 | return $this->progress; |
| 295 | } |
| 296 | |
| 297 | public function setProgress($progress) |
| 298 | { |
| 299 | $this->progress = $progress; |
| 300 | } |
| 301 | |
| 302 | public function getStorageType() |
| 303 | { |
| 304 | return $this->storageType; |
| 305 | } |
| 306 | |
| 307 | public function setStorageType($storageType) |
| 308 | { |
| 309 | $this->storageType = $storageType; |
| 310 | } |
| 311 | |
| 312 | public function getType() |
| 313 | { |
| 314 | return $this->type; |
| 315 | } |
| 316 | |
| 317 | public function setType($type) |
| 318 | { |
| 319 | $this->type = $type; |
| 320 | } |
| 321 | |
| 322 | public function getNumberOfEntries() |
| 323 | { |
| 324 | return $this->numberOfEntries; |
| 325 | } |
| 326 | |
| 327 | public function setNumberOfEntries($numberOfEntries) |
| 328 | { |
| 329 | $this->numberOfEntries = $numberOfEntries; |
| 330 | } |
| 331 | |
| 332 | public function getProgressCursor() |
| 333 | { |
| 334 | return $this->progressCursor; |
| 335 | } |
| 336 | |
| 337 | public function setProgressCursor($progressCursor) |
| 338 | { |
| 339 | $this->progressCursor = $progressCursor; |
| 340 | } |
| 341 | |
| 342 | public function getBackedUpTables() |
| 343 | { |
| 344 | return $this->backedUpTables; |
| 345 | } |
| 346 | |
| 347 | public function setBackedUpTables($backedUpTables) |
| 348 | { |
| 349 | $this->backedUpTables = $backedUpTables; |
| 350 | } |
| 351 | |
| 352 | public function getPendingStorageUploads() |
| 353 | { |
| 354 | return $this->pendingStorageUploads; |
| 355 | } |
| 356 | |
| 357 | public function setPendingStorageUploads($pendingStorageUploads) |
| 358 | { |
| 359 | $this->pendingStorageUploads = $pendingStorageUploads; |
| 360 | } |
| 361 | |
| 362 | public function getBackupFileName() |
| 363 | { |
| 364 | return $this->backupFileName; |
| 365 | } |
| 366 | |
| 367 | public function setBackupFileName($backupFileName) |
| 368 | { |
| 369 | $this->backupFileName = $backupFileName; |
| 370 | } |
| 371 | |
| 372 | public function setCurrentUploadChunksCount($currentUploadChunksCount) |
| 373 | { |
| 374 | $this->currentUploadChunksCount = $currentUploadChunksCount; |
| 375 | } |
| 376 | |
| 377 | public function getCurrentUploadChunksCount() |
| 378 | { |
| 379 | return $this->currentUploadChunksCount; |
| 380 | } |
| 381 | |
| 382 | |
| 383 | public function setTotalUploadChunksCount($totalUploadChunksCount) |
| 384 | { |
| 385 | $this->totalUploadChunksCount = $totalUploadChunksCount; |
| 386 | } |
| 387 | |
| 388 | public function getTotalUploadChunksCount() |
| 389 | { |
| 390 | return $this->totalUploadChunksCount; |
| 391 | } |
| 392 | |
| 393 | public function setToken($token) |
| 394 | { |
| 395 | $this->token = $token; |
| 396 | } |
| 397 | |
| 398 | public function getToken() |
| 399 | { |
| 400 | return $this->token; |
| 401 | } |
| 402 | |
| 403 | |
| 404 | |
| 405 | public function setUploadId($id) |
| 406 | { |
| 407 | $this->uploadId = $id; |
| 408 | } |
| 409 | |
| 410 | public function getUploadId() |
| 411 | { |
| 412 | return $this->uploadId; |
| 413 | } |
| 414 | |
| 415 | public function setActionId($id) |
| 416 | { |
| 417 | $this->actionId = $id; |
| 418 | } |
| 419 | |
| 420 | public function getActionId() |
| 421 | { |
| 422 | return $this->actionId; |
| 423 | } |
| 424 | |
| 425 | public function setParts($parts) |
| 426 | { |
| 427 | $this->parts = $parts; |
| 428 | } |
| 429 | |
| 430 | public function getParts() |
| 431 | { |
| 432 | return $this->parts; |
| 433 | } |
| 434 | |
| 435 | public function getRestoreMode() |
| 436 | { |
| 437 | return $this->restoreMode; |
| 438 | } |
| 439 | |
| 440 | public function setRestoreMode($restoreMode) |
| 441 | { |
| 442 | $this->restoreMode = $restoreMode; |
| 443 | } |
| 444 | |
| 445 | |
| 446 | public function initFromArray($arr) |
| 447 | { |
| 448 | if (!isset($arr['count']) || |
| 449 | !isset($arr['offset']) || |
| 450 | !isset($arr['status']) || |
| 451 | !isset($arr['data']) || |
| 452 | !isset($arr['startTs']) || |
| 453 | !isset($arr['actionId']) || |
| 454 | !isset($arr['uploadId']) || |
| 455 | !isset($arr['progress']) || |
| 456 | !isset($arr['activeDirectory']) || |
| 457 | !isset($arr['currentUploadChunksCount']) || |
| 458 | !isset($arr['parts']) || |
| 459 | !isset($arr['updateTs']) |
| 460 | ) { |
| 461 | error_log(json_encode($arr)); |
| 462 | return false; |
| 463 | } |
| 464 | |
| 465 | $this->setCount($arr['count']); |
| 466 | $this->setOffset($arr['offset']); |
| 467 | $this->setStatus($arr['status']); |
| 468 | $this->setData(null, $arr['data']); |
| 469 | $this->setStartTs($arr['startTs']); |
| 470 | $this->setUpdateTs($arr['updateTs']); |
| 471 | $this->setUploadId($arr['uploadId']); |
| 472 | $this->setProgress($arr['progress']); |
| 473 | $this->setAction($arr['action']); |
| 474 | $this->setType($arr['type']); |
| 475 | $this->setNumberOfEntries($arr['numberOfEntries']); |
| 476 | $this->setProgressCursor($arr['progressCursor']); |
| 477 | $this->setBackedUpTables($arr['backedUpTables']); |
| 478 | $this->setActionId($arr['actionId']); |
| 479 | $this->setParts($arr['parts']); |
| 480 | $this->setChunkSize($arr['chunkSize']); |
| 481 | $this->setActiveDirectory($arr['activeDirectory']); |
| 482 | $this->setPendingStorageUploads($arr['pendingStorageUploads']); |
| 483 | $this->setCurrentUploadChunksCount($arr['currentUploadChunksCount']); |
| 484 | $this->setTotalUploadChunksCount($arr['totalUploadChunksCount']); |
| 485 | $this->setRestoreMode($arr['restoreMode']); |
| 486 | return true; |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * |
| 491 | * Convert state to array. |
| 492 | * |
| 493 | * @return array array containing state data |
| 494 | */ |
| 495 | public function toArray() |
| 496 | { |
| 497 | return array( |
| 498 | 'count' => $this->getCount(), |
| 499 | 'offset' => $this->getOffset(), |
| 500 | 'status' => $this->getStatus(), |
| 501 | 'data' => $this->getData(), |
| 502 | 'startTs' => $this->getStartTs(), |
| 503 | 'updateTs' => $this->getUpdateTs(), |
| 504 | 'action' => $this->getAction(), |
| 505 | 'type' => $this->getType(), |
| 506 | 'backedUpTables' => $this->getBackedUpTables(), |
| 507 | 'actionId' => $this->getActionId(), |
| 508 | 'numberOfEntries' => $this->getNumberOfEntries(), |
| 509 | 'progressCursor' => $this->getProgressCursor(), |
| 510 | 'uploadId' => $this->getUploadId(), |
| 511 | 'progress' => $this->getProgress(), |
| 512 | 'parts' => $this->getParts(), |
| 513 | 'chunkSize' => $this->getChunkSize(), |
| 514 | 'activeDirectory' => $this->getActiveDirectory(), |
| 515 | 'currentUploadChunksCount' => $this->getCurrentUploadChunksCount(), |
| 516 | 'totalUploadChunksCount' => $this->getTotalUploadChunksCount(), |
| 517 | 'pendingStorageUploads' => $this->getPendingStorageUploads(), |
| 518 | 'restoreMode' => $this->getRestoreMode() |
| 519 | ); |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * |
| 524 | * Override parent callback function to open and close file immediately after writing to file. |
| 525 | * We don't keep the file open, since next writing may come very late. |
| 526 | * |
| 527 | * @param string $data data to write to file |
| 528 | * @return null |
| 529 | */ |
| 530 | public function writeToFile($data) |
| 531 | { |
| 532 | $this->open('w'); |
| 533 | |
| 534 | parent::writeToFile($data); |
| 535 | |
| 536 | $this->close(); |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * |
| 541 | * Load state file. |
| 542 | * Decode the JSON file and store all local variables. |
| 543 | * |
| 544 | * @return null |
| 545 | */ |
| 546 | public function load() |
| 547 | { |
| 548 | if (!$this->exists()) { |
| 549 | return; |
| 550 | } |
| 551 | |
| 552 | $this->open('r'); |
| 553 | |
| 554 | //trait JsonFile method called here |
| 555 | $contents = $this->getDecodedContents(); |
| 556 | |
| 557 | if (!empty($contents)) { |
| 558 | $this->initFromArray($contents); |
| 559 | } |
| 560 | |
| 561 | $this->close(); |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * |
| 566 | * Save state file. |
| 567 | * Encode the data and store in the file. |
| 568 | * |
| 569 | * @param bool $forceFlush whether to flush buffer at the end |
| 570 | * @return null |
| 571 | */ |
| 572 | public function save($forceFlush = false) |
| 573 | { |
| 574 | $this->getCache()->clear(); |
| 575 | |
| 576 | $this->setUpdateTs(time()); |
| 577 | |
| 578 | //trait JsonFile method called here |
| 579 | $this->encodeAndSetContents($this->toArray()); |
| 580 | |
| 581 | if ($forceFlush) { |
| 582 | $this->getCache()->flush(); |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 |