@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | $this->tableName = $tableName; |
| 54 | 54 | $this->options = $options; |
| 55 | 55 | $this->pdo = $pdo; |
| 56 | - if($options['use-locking'] ?? true) { |
|
| 56 | + if ($options['use-locking'] ?? true) { |
|
| 57 | 57 | $this->lock = $pdo->prepare('SELECT GET_LOCK(:name, 0)'); |
| 58 | 58 | $this->unlock = $pdo->prepare('SELECT RELEASE_LOCK(:name)'); |
| 59 | 59 | } |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | * @return bool |
| 65 | 65 | */ |
| 66 | 66 | public function has(string $key) { |
| 67 | - if($this->hasService === null) { |
|
| 67 | + if ($this->hasService === null) { |
|
| 68 | 68 | $this->hasService = $this->pdo->prepare("SELECT COUNT(*) FROM `{$this->tableName}` WHERE service_key=:key;"); |
| 69 | 69 | } |
| 70 | 70 | $this->hasService->execute(['key' => $key]); |
@@ -77,9 +77,9 @@ discard block |
||
| 77 | 77 | * @return MySQLAttributeRepository|void |
| 78 | 78 | */ |
| 79 | 79 | public function register(string $key) { |
| 80 | - $this->handleException(function () use ($key) { |
|
| 81 | - if($this->services === null) { |
|
| 82 | - if($this->getServiceKeys === null) { |
|
| 80 | + $this->handleException(function() use ($key) { |
|
| 81 | + if ($this->services === null) { |
|
| 82 | + if ($this->getServiceKeys === null) { |
|
| 83 | 83 | $this->getServiceKeys = $this->pdo->prepare("SELECT `service_key` FROM `{$this->tableName}`;"); |
| 84 | 84 | } |
| 85 | 85 | try { |
@@ -89,8 +89,8 @@ discard block |
||
| 89 | 89 | $this->getServiceKeys->closeCursor(); |
| 90 | 90 | } |
| 91 | 91 | } |
| 92 | - if(!in_array($key, $this->services, true)) { |
|
| 93 | - if($this->registerRow === null) { |
|
| 92 | + if (!in_array($key, $this->services, true)) { |
|
| 93 | + if ($this->registerRow === null) { |
|
| 94 | 94 | $this->registerRow = $this->pdo->prepare("INSERT INTO `{$this->tableName}` (`service_key`) VALUES (:key)"); |
| 95 | 95 | } |
| 96 | 96 | $this->registerRow->execute(['key' => $key]); |
@@ -103,15 +103,15 @@ discard block |
||
| 103 | 103 | * @return object |
| 104 | 104 | */ |
| 105 | 105 | public function getRowByKey(string $key) { |
| 106 | - if($this->getData === null) { |
|
| 106 | + if ($this->getData === null) { |
|
| 107 | 107 | $this->getData = $this->pdo->prepare("SELECT service_key, service_last_try, service_last_run, service_next_run FROM `{$this->tableName}` WHERE service_key=:key;"); |
| 108 | 108 | } |
| 109 | 109 | try { |
| 110 | - $this->handleException(function () use ($key) { |
|
| 110 | + $this->handleException(function() use ($key) { |
|
| 111 | 111 | $this->getData->execute(['key' => $key]); |
| 112 | 112 | }); |
| 113 | 113 | $result = $this->getData->fetchObject(); |
| 114 | - if(!is_object($result)) { |
|
| 114 | + if (!is_object($result)) { |
|
| 115 | 115 | throw new RuntimeException('Row not found'); |
| 116 | 116 | } |
| 117 | 117 | return $result; |
@@ -126,10 +126,10 @@ discard block |
||
| 126 | 126 | * @return void |
| 127 | 127 | */ |
| 128 | 128 | public function setLastTryDate(string $key, DateTimeInterface $datetime): void { |
| 129 | - if($this->updateTryDate === null) { |
|
| 129 | + if ($this->updateTryDate === null) { |
|
| 130 | 130 | $this->updateTryDate = $this->pdo->prepare("INSERT INTO `{$this->tableName}` (service_key, service_last_try) VALUES (:key, :dt) ON DUPLICATE KEY UPDATE service_last_try=:dt"); |
| 131 | 131 | } |
| 132 | - $this->handleException(function () use ($key, $datetime) { |
|
| 132 | + $this->handleException(function() use ($key, $datetime) { |
|
| 133 | 133 | $this->updateTryDate->execute(['key' => $key, 'dt' => $datetime->format('Y-m-d H:i:s')]); |
| 134 | 134 | }); |
| 135 | 135 | } |
@@ -140,10 +140,10 @@ discard block |
||
| 140 | 140 | * @return void |
| 141 | 141 | */ |
| 142 | 142 | public function setLastRunDate(string $key, DateTimeInterface $datetime): void { |
| 143 | - if($this->updateRunDate === null) { |
|
| 143 | + if ($this->updateRunDate === null) { |
|
| 144 | 144 | $this->updateRunDate = $this->pdo->prepare("INSERT INTO `{$this->tableName}` (service_key, service_last_run) VALUES (:key, :dt) ON DUPLICATE KEY UPDATE service_last_run=:dt"); |
| 145 | 145 | } |
| 146 | - $this->handleException(function () use ($key, $datetime) { |
|
| 146 | + $this->handleException(function() use ($key, $datetime) { |
|
| 147 | 147 | $this->updateRunDate->execute(['key' => $key, 'dt' => $datetime->format('Y-m-d H:i:s')]); |
| 148 | 148 | }); |
| 149 | 149 | } |
@@ -154,10 +154,10 @@ discard block |
||
| 154 | 154 | * @return void |
| 155 | 155 | */ |
| 156 | 156 | public function setNextRunDate(string $key, DateTimeInterface $datetime): void { |
| 157 | - if($this->updateNextDate === null) { |
|
| 157 | + if ($this->updateNextDate === null) { |
|
| 158 | 158 | $this->updateNextDate = $this->pdo->prepare("INSERT INTO `{$this->tableName}` (service_key, service_next_run) VALUES (:key, :dt) ON DUPLICATE KEY UPDATE service_next_run=:dt"); |
| 159 | 159 | } |
| 160 | - $this->handleException(function () use ($key, $datetime) { |
|
| 160 | + $this->handleException(function() use ($key, $datetime) { |
|
| 161 | 161 | $this->updateNextDate->execute(['key' => $key, 'dt' => $datetime->format('Y-m-d H:i:s')]); |
| 162 | 162 | }); |
| 163 | 163 | } |
@@ -170,7 +170,7 @@ discard block |
||
| 170 | 170 | public function lockAndIterateServices(?DateTimeInterface $now, callable $fn): int { |
| 171 | 171 | $data = (object) ['count' => 0]; |
| 172 | 172 | $services = $this->fetchServices($now); |
| 173 | - foreach($services as $service) { |
|
| 173 | + foreach ($services as $service) { |
|
| 174 | 174 | try { |
| 175 | 175 | $this->lock($service->getKey()); |
| 176 | 176 | $fn($service); |
@@ -187,15 +187,15 @@ discard block |
||
| 187 | 187 | * @return Service[] |
| 188 | 188 | */ |
| 189 | 189 | private function fetchServices(DateTimeInterface $now) { |
| 190 | - if($this->selectOverdueServices === null) { |
|
| 190 | + if ($this->selectOverdueServices === null) { |
|
| 191 | 191 | $this->selectOverdueServices = $this->pdo->prepare("SELECT service_key, service_last_try, service_last_run, service_next_run FROM `{$this->tableName}` WHERE IFNULL(service_next_run, DATE('2000-01-01')) <= :dt ORDER BY service_last_try;"); |
| 192 | 192 | } |
| 193 | - return $this->handleException(function () use ($now) { |
|
| 193 | + return $this->handleException(function() use ($now) { |
|
| 194 | 194 | $this->selectOverdueServices->execute(['dt' => $now->format('Y-m-d H:i:d')]); |
| 195 | 195 | try { |
| 196 | 196 | $services = $this->selectOverdueServices->fetchAll(PDO::FETCH_ASSOC); |
| 197 | 197 | $result = []; |
| 198 | - foreach($services as $service) { |
|
| 198 | + foreach ($services as $service) { |
|
| 199 | 199 | $result[] = new Service($service['service_key']); |
| 200 | 200 | } |
| 201 | 201 | return $result; |
@@ -215,7 +215,7 @@ discard block |
||
| 215 | 215 | $lockName = sprintf('%s%s', $this->options['lock-prefix'] ?? '', $key); |
| 216 | 216 | $this->lock->execute(['name' => $lockName]); |
| 217 | 217 | $lockObtained = $this->lock->fetchColumn(0); |
| 218 | - if(!$lockObtained) { |
|
| 218 | + if (!$lockObtained) { |
|
| 219 | 219 | throw new RuntimeException(sprintf('Could not obtain lock "%s"', $lockName)); |
| 220 | 220 | } |
| 221 | 221 | } finally { |
@@ -238,13 +238,13 @@ discard block |
||
| 238 | 238 | try { |
| 239 | 239 | return $fn(); |
| 240 | 240 | } catch (PDOException $e) { |
| 241 | - if($e->getCode() === self::MYSQL_ERR_TABLE_MSSING) { |
|
| 241 | + if ($e->getCode() === self::MYSQL_ERR_TABLE_MSSING) { |
|
| 242 | 242 | // Field is missing, let's have a look what is going on... |
| 243 | 243 | $this->pdo->exec("CREATE TABLE IF NOT EXISTS `{$this->tableName}` (`service_key` VARCHAR(255) NOT NULL DEFAULT '', `service_last_try` DATETIME NULL DEFAULT NULL, `service_last_run` DATETIME NULL DEFAULT NULL, `service_next_run` DATETIME NULL DEFAULT NULL, PRIMARY KEY (`service_key`));"); |
| 244 | 244 | return $this->retry($fn); |
| 245 | 245 | } |
| 246 | 246 | |
| 247 | - if($e->getCode() === self::MYSQL_ERR_TABLE_COLUMN_MSSING) { |
|
| 247 | + if ($e->getCode() === self::MYSQL_ERR_TABLE_COLUMN_MSSING) { |
|
| 248 | 248 | // Field is missing, let's have a look what is going on... |
| 249 | 249 | $this->checkIfOldTableVersion(); |
| 250 | 250 | return $this->retry($fn); |
@@ -259,7 +259,7 @@ discard block |
||
| 259 | 259 | * @return mixed |
| 260 | 260 | */ |
| 261 | 261 | private function retry(Closure $fn) { |
| 262 | - return $this->handleException(function () use ($fn) { |
|
| 262 | + return $this->handleException(function() use ($fn) { |
|
| 263 | 263 | return $fn(); |
| 264 | 264 | }); |
| 265 | 265 | } |
@@ -269,10 +269,10 @@ discard block |
||
| 269 | 269 | private function checkIfOldTableVersion() { |
| 270 | 270 | $serviceNextRunField = $this->pdo->query("SHOW COLUMNS FROM `{$this->tableName}` LIKE 'service_next_run';")->fetchAll(PDO::FETCH_ASSOC); |
| 271 | 271 | |
| 272 | - if(!count($serviceNextRunField)) { |
|
| 272 | + if (!count($serviceNextRunField)) { |
|
| 273 | 273 | $this->pdo->exec("ALTER TABLE `{$this->tableName}` ADD COLUMN `service_next_run` DATETIME NULL DEFAULT NULL AFTER `service_last_run`;"); |
| 274 | 274 | $serviceTimeoutField = $this->pdo->query("SHOW COLUMNS FROM `{$this->tableName}` LIKE 'service_timeout';")->fetchAll(PDO::FETCH_ASSOC); |
| 275 | - if(count($serviceTimeoutField)) { |
|
| 275 | + if (count($serviceTimeoutField)) { |
|
| 276 | 276 | $this->pdo->exec("UPDATE `{$this->tableName}` SET service_next_run = DATE_ADD(service_last_run, INTERVAL service_timeout SECOND)"); |
| 277 | 277 | $this->pdo->exec("ALTER TABLE `{$this->tableName}` DROP COLUMN `service_timeout`;"); |
| 278 | 278 | } |