.htaccess
1 year ago
Automation.php
10 months ago
General.php
3 months ago
Integrations.php
4 months ago
Logging.php
4 months ago
Maintenance.php
4 months ago
Notifications.php
4 months ago
Performance.php
3 months ago
Restore.php
1 year ago
Security.php
1 day ago
Settings.php
4 months ago
Updates.php
4 months ago
index.html
1 year ago
web.config
1 year ago
Maintenance.php
151 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JetBackup\Settings; |
| 4 | |
| 5 | use JetBackup\BackupJob\BackupJob; |
| 6 | use JetBackup\Exception\DBException; |
| 7 | use JetBackup\Exception\FieldsValidationException; |
| 8 | use JetBackup\Exception\IOException; |
| 9 | use JetBackup\Exception\JBException; |
| 10 | use SleekDB\Exceptions\InvalidArgumentException; |
| 11 | |
| 12 | if (!defined( '__JETBACKUP__')) die('Direct access is not allowed'); |
| 13 | |
| 14 | class Maintenance extends Settings { |
| 15 | |
| 16 | const SECTION = 'maintenance'; |
| 17 | |
| 18 | const MAINTENANCE_QUEUE_HOURS_TTL = 'MAINTENANCE_QUEUE_HOURS_TTL'; |
| 19 | const MAINTENANCE_QUEUE_ALERTS_TTL = 'MAINTENANCE_QUEUE_ALERTS_TTL'; |
| 20 | const MAINTENANCE_DOWNLOAD_ITEMS_TTL = 'MAINTENANCE_DOWNLOAD_ITEMS_TTL'; |
| 21 | const MAINTENANCE_DOWNLOAD_LIMIT = 'MAINTENANCE_DOWNLOAD_LIMIT'; |
| 22 | |
| 23 | const CONFIG_EXPORT_ROTATE = 'CONFIG_EXPORT_ROTATE'; |
| 24 | |
| 25 | private BackupJob $_backup; |
| 26 | |
| 27 | /** |
| 28 | * @throws DBException |
| 29 | * @throws IOException |
| 30 | * @throws JBException |
| 31 | * @throws \SleekDB\Exceptions\IOException |
| 32 | * @throws InvalidArgumentException |
| 33 | */ |
| 34 | public function __construct() { |
| 35 | parent::__construct(self::SECTION); |
| 36 | $this->_backup = BackupJob::getDefaultConfigJob(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @return int |
| 41 | */ |
| 42 | public function getQueueItemsTTL():int { return (int) $this->get(self::MAINTENANCE_QUEUE_HOURS_TTL, 24); } |
| 43 | public function getDownloadItemsTTL():int { return (int) $this->get(self::MAINTENANCE_DOWNLOAD_ITEMS_TTL, 72); } |
| 44 | public function getDownloadLimit():int { return (int) $this->get(self::MAINTENANCE_DOWNLOAD_LIMIT, 5); } |
| 45 | |
| 46 | /** |
| 47 | * @param int $value |
| 48 | * |
| 49 | * @return void |
| 50 | */ |
| 51 | public function setQueueItemsTTL(int $value):void { $this->set(self::MAINTENANCE_QUEUE_HOURS_TTL, $value); } |
| 52 | public function setDownloadItemsTTL(int $value):void { $this->set(self::MAINTENANCE_DOWNLOAD_ITEMS_TTL, $value); } |
| 53 | public function setDownloadLimit(int $value):void { $this->set(self::MAINTENANCE_DOWNLOAD_LIMIT, $value); } |
| 54 | |
| 55 | /** |
| 56 | * @return int |
| 57 | */ |
| 58 | public function getAlertsTTL():int { return (int) $this->get(self::MAINTENANCE_QUEUE_ALERTS_TTL, 72); } |
| 59 | |
| 60 | /** |
| 61 | * @param int $value |
| 62 | * |
| 63 | * @return void |
| 64 | */ |
| 65 | public function setAlertsTTL(int $value):void { $this->set(self::MAINTENANCE_QUEUE_ALERTS_TTL, $value); } |
| 66 | |
| 67 | /** |
| 68 | * @return int |
| 69 | */ |
| 70 | public function getConfigExportRotate():int { |
| 71 | $schedules = $this->_backup->getSchedules(); |
| 72 | return $schedules[0]->getRetain(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @param int $value |
| 77 | * |
| 78 | * @return void |
| 79 | */ |
| 80 | public function setConfigExportRotate(int $value):void { |
| 81 | $schedules = $this->_backup->getSchedules(); |
| 82 | $schedules[0]->setRetain($value); |
| 83 | $this->_backup->setSchedules($schedules); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @return array |
| 88 | */ |
| 89 | public function getDisplay():array { |
| 90 | |
| 91 | return [ |
| 92 | self::MAINTENANCE_QUEUE_HOURS_TTL => $this->getQueueItemsTTL(), |
| 93 | self::MAINTENANCE_QUEUE_ALERTS_TTL => $this->getAlertsTTL(), |
| 94 | self::MAINTENANCE_DOWNLOAD_ITEMS_TTL => $this->getDownloadItemsTTL(), |
| 95 | self::MAINTENANCE_DOWNLOAD_LIMIT => $this->getDownloadLimit(), |
| 96 | self::CONFIG_EXPORT_ROTATE => $this->getConfigExportRotate(), |
| 97 | ]; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @return array |
| 102 | */ |
| 103 | public function getDisplayCLI():array { |
| 104 | |
| 105 | return [ |
| 106 | 'Queue Items TTL' => $this->getQueueItemsTTL(), |
| 107 | 'Alerts TTL' => $this->getAlertsTTL(), |
| 108 | 'Download Items TTL' => $this->getDownloadItemsTTL(), |
| 109 | 'Downloads Limits' => $this->getDownloadLimit(), |
| 110 | 'Config Export Rotate' => $this->getConfigExportRotate(), |
| 111 | ]; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @return void |
| 116 | * @throws FieldsValidationException |
| 117 | */ |
| 118 | public function validateFields():void { |
| 119 | |
| 120 | $changedFields = self::getChangedFields($this->getData(), (new Maintenance())->getData()); |
| 121 | |
| 122 | if(in_array(self::MAINTENANCE_QUEUE_HOURS_TTL, $changedFields)) { |
| 123 | if($this->getQueueItemsTTL() < 0) throw new FieldsValidationException("Done Queue items TTL must be a positive integer or 0 to disable"); |
| 124 | } |
| 125 | if(in_array(self::MAINTENANCE_DOWNLOAD_ITEMS_TTL, $changedFields)) { |
| 126 | if($this->getDownloadItemsTTL() < 0) throw new FieldsValidationException("Download TTL must be a positive integer or 0 to disable"); |
| 127 | } |
| 128 | if(in_array(self::MAINTENANCE_DOWNLOAD_LIMIT, $changedFields)) { |
| 129 | if($this->getDownloadLimit() < 0) throw new FieldsValidationException("Download limit must be a positive integer or 0 to disable"); |
| 130 | } |
| 131 | if(in_array(self::MAINTENANCE_QUEUE_ALERTS_TTL, $changedFields)) { |
| 132 | if($this->getAlertsTTL() < 0) throw new FieldsValidationException("System Alerts TTL must be a positive integer or 0 to disable"); |
| 133 | } |
| 134 | if(in_array(self::CONFIG_EXPORT_ROTATE, $changedFields)) { |
| 135 | if($this->getConfigExportRotate() < 1) throw new FieldsValidationException("Export Config Rotate Days must be grater then 0"); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @return void |
| 141 | * @throws InvalidArgumentException |
| 142 | * @throws JBException |
| 143 | * @throws \SleekDB\Exceptions\IOException |
| 144 | */ |
| 145 | public function save(): void { |
| 146 | parent::save(); |
| 147 | |
| 148 | $this->_backup->calculateNextRun(); |
| 149 | $this->_backup->save(); |
| 150 | } |
| 151 | } |