@@ -1,9 +1,9 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace Kir\Services\Cmd\Dispatcher\Builder; |
| 3 | 3 | |
| 4 | -use Kir\Services\Cmd\Dispatcher\AttributeRepositories\MySQLAttributeRepository; |
|
| 5 | -use Kir\Services\Cmd\Dispatcher\Dispatcher; |
|
| 6 | -use Kir\Services\Cmd\Dispatcher\Dispatchers\DefaultDispatcher; |
|
| 4 | +use Kir\Services\Cmd\Dispatcher\AttributeRepositories\MySQLAttributeRepository; |
|
| 5 | +use Kir\Services\Cmd\Dispatcher\Dispatcher; |
|
| 6 | +use Kir\Services\Cmd\Dispatcher\Dispatchers\DefaultDispatcher; |
|
| 7 | 7 | use PDO; |
| 8 | 8 | |
| 9 | 9 | class MySQLBuilder {
|
@@ -35,8 +35,8 @@ |
||
| 35 | 35 | |
| 36 | 36 | /** |
| 37 | 37 | * @param $interval |
| 38 | - * @param DateTimeInterface|null $now |
|
| 39 | - * @return Generator|DateTimeImmutable |
|
| 38 | + * @param DateTimeInterface $now |
|
| 39 | + * @return Generator |
|
| 40 | 40 | */ |
| 41 | 41 | private static function parse($interval, DateTimeInterface $now) { |
| 42 | 42 | if(is_array($interval)) { |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | * @return DateTimeImmutable |
| 16 | 16 | */ |
| 17 | 17 | public static function getNext($interval, DateTimeInterface $now = null): DateTimeImmutable { |
| 18 | - if($now === null) { |
|
| 18 | + if ($now === null) { |
|
| 19 | 19 | try { |
| 20 | 20 | $now = new DateTimeImmutable(); |
| 21 | 21 | } catch (Throwable $e) { |
@@ -23,10 +23,10 @@ discard block |
||
| 23 | 23 | } |
| 24 | 24 | } |
| 25 | 25 | $result = null; |
| 26 | - foreach(self::parse($interval, $now) as $date) { |
|
| 27 | - if($result === null) { |
|
| 26 | + foreach (self::parse($interval, $now) as $date) { |
|
| 27 | + if ($result === null) { |
|
| 28 | 28 | $result = $date; |
| 29 | - } elseif($date < $result) { |
|
| 29 | + } elseif ($date < $result) { |
|
| 30 | 30 | $result = $date; |
| 31 | 31 | } |
| 32 | 32 | } |
@@ -39,11 +39,11 @@ discard block |
||
| 39 | 39 | * @return Generator|DateTimeImmutable |
| 40 | 40 | */ |
| 41 | 41 | private static function parse($interval, DateTimeInterface $now) { |
| 42 | - if(is_array($interval)) { |
|
| 43 | - foreach($interval as $inner) { |
|
| 42 | + if (is_array($interval)) { |
|
| 43 | + foreach ($interval as $inner) { |
|
| 44 | 44 | yield from self::parse($inner, $now); |
| 45 | 45 | } |
| 46 | - } elseif(ctype_digit($interval)) { |
|
| 46 | + } elseif (ctype_digit($interval)) { |
|
| 47 | 47 | yield self::parseInt($interval, $now); |
| 48 | 48 | } else { |
| 49 | 49 | yield self::parseString($interval, $now); |
@@ -65,7 +65,7 @@ discard block |
||
| 65 | 65 | * @return DateTimeInterface |
| 66 | 66 | */ |
| 67 | 67 | private static function parseString(string $interval, DateTimeInterface $now): DateTimeInterface { |
| 68 | - if(preg_match('/^(\\d{1,2}|\\*):(\\d{1,2}|\\*)(?::(\\d{1,2}|\\*))?$/', $interval, $matches)) { |
|
| 68 | + if (preg_match('/^(\\d{1,2}|\\*):(\\d{1,2}|\\*)(?::(\\d{1,2}|\\*))?$/', $interval, $matches)) { |
|
| 69 | 69 | $matches[] = 0; |
| 70 | 70 | [$hours, $minutes, $seconds] = array_slice($matches, 1); |
| 71 | 71 | $today = date_create_immutable($now->format('c'))->setTime((int) $hours, (int) $minutes, (int) $seconds); |
@@ -88,15 +88,15 @@ discard block |
||
| 88 | 88 | */ |
| 89 | 89 | private static function nearst(array $possibleDates, DateTimeInterface $now) { |
| 90 | 90 | $current = null; |
| 91 | - foreach($possibleDates as $possibleDate) { |
|
| 92 | - if($now > $possibleDate) { // The current date is in the past |
|
| 91 | + foreach ($possibleDates as $possibleDate) { |
|
| 92 | + if ($now > $possibleDate) { // The current date is in the past |
|
| 93 | 93 | continue; |
| 94 | 94 | } |
| 95 | - if($current === null || $possibleDate < $current) { |
|
| 95 | + if ($current === null || $possibleDate < $current) { |
|
| 96 | 96 | $current = $possibleDate; |
| 97 | 97 | } |
| 98 | 98 | } |
| 99 | - if($current !== null) { |
|
| 99 | + if ($current !== null) { |
|
| 100 | 100 | return $current; |
| 101 | 101 | } |
| 102 | 102 | throw new RuntimeException('No alternative lays in the future'); |
@@ -1,10 +1,10 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace Kir\Services\Cmd\Dispatcher; |
| 3 | 3 | |
| 4 | -use Kir\Services\Cmd\Dispatcher\AttributeRepositories\MySQLAttributeRepository; |
|
| 5 | -use Kir\Services\Cmd\Dispatcher\AttributeRepositories\SqliteAttributeRepository; |
|
| 6 | -use Kir\Services\Cmd\Dispatcher\Builder\MySQLBuilder; |
|
| 7 | -use Kir\Services\Cmd\Dispatcher\Dispatchers\DefaultDispatcher; |
|
| 4 | +use Kir\Services\Cmd\Dispatcher\AttributeRepositories\MySQLAttributeRepository; |
|
| 5 | +use Kir\Services\Cmd\Dispatcher\AttributeRepositories\SqliteAttributeRepository; |
|
| 6 | +use Kir\Services\Cmd\Dispatcher\Builder\MySQLBuilder; |
|
| 7 | +use Kir\Services\Cmd\Dispatcher\Dispatchers\DefaultDispatcher; |
|
| 8 | 8 | use PDO; |
| 9 | 9 | |
| 10 | 10 | class ServiceDispatcherBuilder {
|
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | $this->tableName = $tableName; |
| 55 | 55 | $this->options = $options; |
| 56 | 56 | $this->pdo = $pdo; |
| 57 | - if($options['use-locking'] ?? true) { |
|
| 57 | + if ($options['use-locking'] ?? true) { |
|
| 58 | 58 | $this->lock = $pdo->prepare('SELECT GET_LOCK(:name, 0)'); |
| 59 | 59 | $this->unlock = $pdo->prepare('SELECT RELEASE_LOCK(:name)'); |
| 60 | 60 | } |
@@ -65,7 +65,7 @@ discard block |
||
| 65 | 65 | * @return bool |
| 66 | 66 | */ |
| 67 | 67 | public function has(string $key) { |
| 68 | - if($this->hasService === null) { |
|
| 68 | + if ($this->hasService === null) { |
|
| 69 | 69 | $this->hasService = $this->pdo->prepare("SELECT COUNT(*) FROM `{$this->tableName}` WHERE service_key=:key;"); |
| 70 | 70 | } |
| 71 | 71 | $this->hasService->execute(['key' => $key]); |
@@ -78,9 +78,9 @@ discard block |
||
| 78 | 78 | * @return MySQLAttributeRepository|void |
| 79 | 79 | */ |
| 80 | 80 | public function register(string $key) { |
| 81 | - $this->handleException(function () use ($key) { |
|
| 82 | - if($this->services === null) { |
|
| 83 | - if($this->getServiceKeys === null) { |
|
| 81 | + $this->handleException(function() use ($key) { |
|
| 82 | + if ($this->services === null) { |
|
| 83 | + if ($this->getServiceKeys === null) { |
|
| 84 | 84 | $this->getServiceKeys = $this->pdo->prepare("SELECT `service_key` FROM `{$this->tableName}`;"); |
| 85 | 85 | } |
| 86 | 86 | try { |
@@ -90,8 +90,8 @@ discard block |
||
| 90 | 90 | $this->getServiceKeys->closeCursor(); |
| 91 | 91 | } |
| 92 | 92 | } |
| 93 | - if(!in_array($key, $this->services, true)) { |
|
| 94 | - if($this->registerRow === null) { |
|
| 93 | + if (!in_array($key, $this->services, true)) { |
|
| 94 | + if ($this->registerRow === null) { |
|
| 95 | 95 | $this->registerRow = $this->pdo->prepare("INSERT INTO `{$this->tableName}` (`service_key`) VALUES (:key)"); |
| 96 | 96 | } |
| 97 | 97 | $this->registerRow->execute(['key' => $key]); |
@@ -104,15 +104,15 @@ discard block |
||
| 104 | 104 | * @return object |
| 105 | 105 | */ |
| 106 | 106 | public function getRowByKey(string $key) { |
| 107 | - if($this->getData === null) { |
|
| 107 | + if ($this->getData === null) { |
|
| 108 | 108 | $this->getData = $this->pdo->prepare("SELECT service_key, service_last_try, service_last_run, service_next_run FROM `{$this->tableName}` WHERE service_key=:key;"); |
| 109 | 109 | } |
| 110 | 110 | try { |
| 111 | - $this->handleException(function () use ($key) { |
|
| 111 | + $this->handleException(function() use ($key) { |
|
| 112 | 112 | $this->getData->execute(['key' => $key]); |
| 113 | 113 | }); |
| 114 | 114 | $result = $this->getData->fetchObject(); |
| 115 | - if(!is_object($result)) { |
|
| 115 | + if (!is_object($result)) { |
|
| 116 | 116 | throw new RuntimeException('Row not found'); |
| 117 | 117 | } |
| 118 | 118 | return $result; |
@@ -127,10 +127,10 @@ discard block |
||
| 127 | 127 | * @return MySQLAttributeRepository|void |
| 128 | 128 | */ |
| 129 | 129 | public function setLastTryDate(string $key, DateTimeInterface $datetime) { |
| 130 | - if($this->updateTryDate === null) { |
|
| 130 | + if ($this->updateTryDate === null) { |
|
| 131 | 131 | $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"); |
| 132 | 132 | } |
| 133 | - $this->handleException(function () use ($key, $datetime) { |
|
| 133 | + $this->handleException(function() use ($key, $datetime) { |
|
| 134 | 134 | $this->updateTryDate->execute(['key' => $key, 'dt' => $datetime->format('Y-m-d H:i:s')]); |
| 135 | 135 | }); |
| 136 | 136 | } |
@@ -141,10 +141,10 @@ discard block |
||
| 141 | 141 | * @return MySQLAttributeRepository|void |
| 142 | 142 | */ |
| 143 | 143 | public function setLastRunDate(string $key, DateTimeInterface $datetime) { |
| 144 | - if($this->updateRunDate === null) { |
|
| 144 | + if ($this->updateRunDate === null) { |
|
| 145 | 145 | $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"); |
| 146 | 146 | } |
| 147 | - $this->handleException(function () use ($key, $datetime) { |
|
| 147 | + $this->handleException(function() use ($key, $datetime) { |
|
| 148 | 148 | $this->updateRunDate->execute(['key' => $key, 'dt' => $datetime->format('Y-m-d H:i:s')]); |
| 149 | 149 | }); |
| 150 | 150 | } |
@@ -155,10 +155,10 @@ discard block |
||
| 155 | 155 | * @return MySQLAttributeRepository|void |
| 156 | 156 | */ |
| 157 | 157 | public function setNextRunDate(string $key, DateTimeInterface $datetime) { |
| 158 | - if($this->updateNextDate === null) { |
|
| 158 | + if ($this->updateNextDate === null) { |
|
| 159 | 159 | $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"); |
| 160 | 160 | } |
| 161 | - $this->handleException(function () use ($key, $datetime) { |
|
| 161 | + $this->handleException(function() use ($key, $datetime) { |
|
| 162 | 162 | $this->updateNextDate->execute(['key' => $key, 'dt' => $datetime->format('Y-m-d H:i:s')]); |
| 163 | 163 | }); |
| 164 | 164 | } |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | public function lockAndIterateServices(?DateTimeInterface $now, callable $fn): int { |
| 173 | 173 | $data = (object) ['count' => 0]; |
| 174 | 174 | $services = $this->fetchServices($now); |
| 175 | - foreach($services as $service) { |
|
| 175 | + foreach ($services as $service) { |
|
| 176 | 176 | try { |
| 177 | 177 | $this->lock($service->getKey()); |
| 178 | 178 | $fn($service); |
@@ -189,15 +189,15 @@ discard block |
||
| 189 | 189 | * @return Service[] |
| 190 | 190 | */ |
| 191 | 191 | private function fetchServices(DateTimeInterface $now) { |
| 192 | - if($this->selectOverdueServices === null) { |
|
| 192 | + if ($this->selectOverdueServices === null) { |
|
| 193 | 193 | $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;"); |
| 194 | 194 | } |
| 195 | - return $this->handleException(function () use ($now) { |
|
| 195 | + return $this->handleException(function() use ($now) { |
|
| 196 | 196 | $this->selectOverdueServices->execute(['dt' => $now->format('Y-m-d H:i:d')]); |
| 197 | 197 | try { |
| 198 | 198 | $services = $this->selectOverdueServices->fetchAll(PDO::FETCH_ASSOC); |
| 199 | 199 | $result = []; |
| 200 | - foreach($services as $service) { |
|
| 200 | + foreach ($services as $service) { |
|
| 201 | 201 | $result[] = new Service($service['service_key']); |
| 202 | 202 | } |
| 203 | 203 | return $result; |
@@ -217,7 +217,7 @@ discard block |
||
| 217 | 217 | $lockName = sprintf('%s%s', $this->options['lock-prefix'] ?? '', $key); |
| 218 | 218 | $this->lock->execute(['name' => $lockName]); |
| 219 | 219 | $lockObtained = $this->lock->fetchColumn(0); |
| 220 | - if(!$lockObtained) { |
|
| 220 | + if (!$lockObtained) { |
|
| 221 | 221 | throw new RuntimeException(sprintf('Could not obtain lock "%s"', $lockName)); |
| 222 | 222 | } |
| 223 | 223 | } finally { |
@@ -240,13 +240,13 @@ discard block |
||
| 240 | 240 | try { |
| 241 | 241 | return $fn(); |
| 242 | 242 | } catch (PDOException $e) { |
| 243 | - if($e->getCode() === self::MYSQL_ERR_TABLE_MSSING) { |
|
| 243 | + if ($e->getCode() === self::MYSQL_ERR_TABLE_MSSING) { |
|
| 244 | 244 | // Field is missing, let's have a look what is going on... |
| 245 | 245 | $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`));"); |
| 246 | 246 | return $this->retry($fn); |
| 247 | 247 | } |
| 248 | 248 | |
| 249 | - if($e->getCode() === self::MYSQL_ERR_TABLE_COLUMN_MSSING) { |
|
| 249 | + if ($e->getCode() === self::MYSQL_ERR_TABLE_COLUMN_MSSING) { |
|
| 250 | 250 | // Field is missing, let's have a look what is going on... |
| 251 | 251 | $this->checkIfOldTableVersion(); |
| 252 | 252 | return $this->retry($fn); |
@@ -261,7 +261,7 @@ discard block |
||
| 261 | 261 | * @return mixed |
| 262 | 262 | */ |
| 263 | 263 | private function retry(Closure $fn) { |
| 264 | - return $this->handleException(function () use ($fn) { |
|
| 264 | + return $this->handleException(function() use ($fn) { |
|
| 265 | 265 | return $fn(); |
| 266 | 266 | }); |
| 267 | 267 | } |
@@ -271,10 +271,10 @@ discard block |
||
| 271 | 271 | private function checkIfOldTableVersion() { |
| 272 | 272 | $serviceNextRunField = $this->pdo->query("SHOW COLUMNS FROM `{$this->tableName}` LIKE 'service_next_run';")->fetchAll(PDO::FETCH_ASSOC); |
| 273 | 273 | |
| 274 | - if(!count($serviceNextRunField)) { |
|
| 274 | + if (!count($serviceNextRunField)) { |
|
| 275 | 275 | $this->pdo->exec("ALTER TABLE `{$this->tableName}` ADD COLUMN `service_next_run` DATETIME NULL DEFAULT NULL AFTER `service_last_run`;"); |
| 276 | 276 | $serviceTimeoutField = $this->pdo->query("SHOW COLUMNS FROM `{$this->tableName}` LIKE 'service_timeout';")->fetchAll(PDO::FETCH_ASSOC); |
| 277 | - if(count($serviceTimeoutField)) { |
|
| 277 | + if (count($serviceTimeoutField)) { |
|
| 278 | 278 | $this->pdo->exec("UPDATE `{$this->tableName}` SET service_next_run = DATE_ADD(service_last_run, INTERVAL service_timeout SECOND)"); |
| 279 | 279 | $this->pdo->exec("ALTER TABLE `{$this->tableName}` DROP COLUMN `service_timeout`;"); |
| 280 | 280 | } |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | try { |
| 81 | 81 | $this->getData->execute(['key' => $key]); |
| 82 | 82 | $result = $this->getData->fetchObject(); |
| 83 | - if(!is_object($result)) { |
|
| 83 | + if (!is_object($result)) { |
|
| 84 | 84 | throw new RuntimeException('Row not found'); |
| 85 | 85 | } |
| 86 | 86 | return $result; |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | public function lockAndIterateServices(DateTimeInterface $now, callable $fn): int { |
| 98 | 98 | $count = 0; |
| 99 | 99 | $services = $this->fetchServices($now); |
| 100 | - foreach($services as $service) { |
|
| 100 | + foreach ($services as $service) { |
|
| 101 | 101 | $fn($service); |
| 102 | 102 | } |
| 103 | 103 | return $count; |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | $this->selectServices->execute(['dt' => $now->format(self::SQLITE_DATETIME_FORMAT)]); |
| 112 | 112 | try { |
| 113 | 113 | $services = $this->selectServices->fetchAll(PDO::FETCH_ASSOC); |
| 114 | - foreach($services as $service) { |
|
| 114 | + foreach ($services as $service) { |
|
| 115 | 115 | yield new Service($service['service_key']); |
| 116 | 116 | } |
| 117 | 117 | } finally { |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | */ |
| 126 | 126 | private function migrate(int $version, string $statement): void { |
| 127 | 127 | $currentVersion = (int) $this->pdo->query('PRAGMA user_version')->fetchColumn(0); |
| 128 | - if($currentVersion < $version) { |
|
| 128 | + if ($currentVersion < $version) { |
|
| 129 | 129 | $this->pdo->exec($statement); |
| 130 | 130 | $this->pdo->exec("PRAGMA user_version={$version}"); |
| 131 | 131 | } |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | * @return $this |
| 58 | 58 | */ |
| 59 | 59 | public function on(string $event, callable $fn) { |
| 60 | - if(!array_key_exists($event, $this->listeners)) { |
|
| 60 | + if (!array_key_exists($event, $this->listeners)) { |
|
| 61 | 61 | $this->listeners[$event] = []; |
| 62 | 62 | } |
| 63 | 63 | $this->listeners[$event][] = $fn; |
@@ -70,8 +70,8 @@ discard block |
||
| 70 | 70 | */ |
| 71 | 71 | public function run(DateTimeInterface $now = null) { |
| 72 | 72 | $now = $now ?? date_create_immutable(); |
| 73 | - return $this->attributeRepository->lockAndIterateServices($now, function (Service $service) { |
|
| 74 | - if(!array_key_exists($service->getKey(), $this->services)) { |
|
| 73 | + return $this->attributeRepository->lockAndIterateServices($now, function(Service $service) { |
|
| 74 | + if (!array_key_exists($service->getKey(), $this->services)) { |
|
| 75 | 75 | return; |
| 76 | 76 | } |
| 77 | 77 | $eventParams = ['serviceName' => $service->getKey()]; |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | $this->fireEvent('service-start', $eventParams); |
| 80 | 80 | $serviceData = $this->services[$service->getKey()]; |
| 81 | 81 | $this->attributeRepository->setLastTryDate($service->getKey(), date_create_immutable()); |
| 82 | - if($this->methodInvoker !== null) { |
|
| 82 | + if ($this->methodInvoker !== null) { |
|
| 83 | 83 | $result = $this->methodInvoker->invoke($serviceData->fn, $eventParams); |
| 84 | 84 | } else { |
| 85 | 85 | $result = call_user_func($serviceData->fn, $service); |
@@ -87,13 +87,13 @@ discard block |
||
| 87 | 87 | $this->attributeRepository->setLastRunDate($service->getKey(), date_create_immutable()); |
| 88 | 88 | $nextRunDate = IntervalParser::getNext($serviceData->interval); |
| 89 | 89 | $this->attributeRepository->setNextRunDate($serviceData->key, $nextRunDate); |
| 90 | - if($result !== false) { |
|
| 90 | + if ($result !== false) { |
|
| 91 | 91 | $this->fireEvent('service-success', $eventParams); |
| 92 | 92 | } |
| 93 | 93 | } catch (Throwable $e) { |
| 94 | 94 | $eventParams['exception'] = $e; |
| 95 | 95 | $this->fireEvent('service-failure', $eventParams); |
| 96 | - if($this->logger !== null) { |
|
| 96 | + if ($this->logger !== null) { |
|
| 97 | 97 | $this->logger->critical("{$service}: {$e->getMessage()}", ['exception' => $e]); |
| 98 | 98 | } else { |
| 99 | 99 | throw new RuntimeException("{$service}: {$e->getMessage()}", (int) $e->getCode(), $e); |
@@ -107,14 +107,14 @@ discard block |
||
| 107 | 107 | * @param array $params |
| 108 | 108 | */ |
| 109 | 109 | private function fireEvent($event, $params) { |
| 110 | - if(array_key_exists($event, $this->listeners)) { |
|
| 110 | + if (array_key_exists($event, $this->listeners)) { |
|
| 111 | 111 | try { |
| 112 | - foreach($this->listeners[$event] as $listener) { |
|
| 112 | + foreach ($this->listeners[$event] as $listener) { |
|
| 113 | 113 | $this->methodInvoker->invoke($listener, $params); |
| 114 | 114 | } |
| 115 | 115 | } catch (Exception $e) { |
| 116 | 116 | // Supress exceptions emitted by events |
| 117 | - if($this->logger !== null) { |
|
| 117 | + if ($this->logger !== null) { |
|
| 118 | 118 | $this->logger->critical($e->getMessage(), array('exception' => $e)); |
| 119 | 119 | } |
| 120 | 120 | } |