@@ -9,9 +9,9 @@ discard block |
||
| 9 | 9 | * @return int |
| 10 | 10 | */ |
| 11 | 11 | public static function parse($interval) { |
| 12 | - if(is_array($interval)) { |
|
| 12 | + if (is_array($interval)) { |
|
| 13 | 13 | $result = array(); |
| 14 | - foreach($interval as $intervalStr) { |
|
| 14 | + foreach ($interval as $intervalStr) { |
|
| 15 | 15 | $result[] = self::parseString($intervalStr); |
| 16 | 16 | } |
| 17 | 17 | return min($result); |
@@ -26,10 +26,10 @@ discard block |
||
| 26 | 26 | * @throws Exception |
| 27 | 27 | */ |
| 28 | 28 | private static function parseString($interval) { |
| 29 | - if(preg_match('/^\\d+$/', $interval)) { |
|
| 29 | + if (preg_match('/^\\d+$/', $interval)) { |
|
| 30 | 30 | return $interval; |
| 31 | 31 | } |
| 32 | - if(preg_match('/^(\\d{1,2}|\\*):(\\d{1,2}|\\*)(?::(\\d{1,2}|\\*))?$/', $interval, $matches)) { |
|
| 32 | + if (preg_match('/^(\\d{1,2}|\\*):(\\d{1,2}|\\*)(?::(\\d{1,2}|\\*))?$/', $interval, $matches)) { |
|
| 33 | 33 | $matches[] = 0; |
| 34 | 34 | list($hours, $minutes, $seconds) = array_slice($matches, 1); |
| 35 | 35 | $possibleDates = array( |
@@ -47,9 +47,9 @@ discard block |
||
| 47 | 47 | * @throws Exception |
| 48 | 48 | */ |
| 49 | 49 | private static function nearst(array $possibleDates) { |
| 50 | - foreach($possibleDates as $possibleDate) { |
|
| 50 | + foreach ($possibleDates as $possibleDate) { |
|
| 51 | 51 | $time = strtotime($possibleDate); |
| 52 | - if($time > time()) { |
|
| 52 | + if ($time > time()) { |
|
| 53 | 53 | return $time - time(); |
| 54 | 54 | } |
| 55 | 55 | } |
@@ -65,13 +65,13 @@ discard block |
||
| 65 | 65 | */ |
| 66 | 66 | public function store($key, $timeout, array $data = []) { |
| 67 | 67 | $key = trim(strtolower($key)); |
| 68 | - if(!in_array($key, $this->services)) { |
|
| 68 | + if (!in_array($key, $this->services)) { |
|
| 69 | 69 | $this->services[] = $key; |
| 70 | 70 | } else { |
| 71 | 71 | throw new RuntimeException("Duplicate service: {$key}"); |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - if($this->has($key)) { |
|
| 74 | + if ($this->has($key)) { |
|
| 75 | 75 | $this->updateService->execute(['key' => $key, 'timeout' => $timeout]); |
| 76 | 76 | } else { |
| 77 | 77 | $this->insertService->execute(['key' => $key, 'timeout' => $timeout, 'try' => '2000-01-01 00:00:00', 'run' => '2000-01-01 00:00:00']); |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | $this->pdo->exec('BEGIN EXCLUSIVE TRANSACTION'); |
| 91 | 91 | try { |
| 92 | 92 | $services = $this->fetchServices(); |
| 93 | - foreach($services as $service) { |
|
| 93 | + foreach ($services as $service) { |
|
| 94 | 94 | $this->updateTryDate->execute(['key' => $service->getKey()]); |
| 95 | 95 | $fn($service); |
| 96 | 96 | $this->updateRunDate->execute(['key' => $service->getKey()]); |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | } |
| 99 | 99 | $this->pdo->exec('COMMIT'); |
| 100 | 100 | return $count; |
| 101 | - } catch(Throwable $e) { |
|
| 101 | + } catch (Throwable $e) { |
|
| 102 | 102 | $this->pdo->exec('ROLLBACK'); |
| 103 | 103 | throw $e; |
| 104 | 104 | } |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | $this->selectServices->execute(); |
| 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 { |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | * @return $this |
| 57 | 57 | */ |
| 58 | 58 | public function on($event, $fn) { |
| 59 | - if(!array_key_exists($event, $this->listeners)) { |
|
| 59 | + if (!array_key_exists($event, $this->listeners)) { |
|
| 60 | 60 | $this->listeners[$event] = array(); |
| 61 | 61 | } |
| 62 | 62 | $this->listeners[$event][] = $fn; |
@@ -67,11 +67,11 @@ discard block |
||
| 67 | 67 | * @return int Number of successfully executed services |
| 68 | 68 | */ |
| 69 | 69 | public function run() { |
| 70 | - return $this->attributeRepository->lockAndIterateServices(function (Service $service) { |
|
| 71 | - if(!array_key_exists($service->getKey(), $this->services)) { |
|
| 70 | + return $this->attributeRepository->lockAndIterateServices(function(Service $service) { |
|
| 71 | + if (!array_key_exists($service->getKey(), $this->services)) { |
|
| 72 | 72 | return; |
| 73 | 73 | } |
| 74 | - if(array_key_exists($service->getKey(), $this->standardTimeouts)) { |
|
| 74 | + if (array_key_exists($service->getKey(), $this->standardTimeouts)) { |
|
| 75 | 75 | $standardTimeout = $this->standardTimeouts[$service->getKey()]; |
| 76 | 76 | } else { |
| 77 | 77 | $standardTimeout = 0; |
@@ -82,18 +82,18 @@ discard block |
||
| 82 | 82 | ]; |
| 83 | 83 | try { |
| 84 | 84 | $this->fireEvent('service-start', $eventParams); |
| 85 | - if($this->methodInvoker !== null) { |
|
| 85 | + if ($this->methodInvoker !== null) { |
|
| 86 | 86 | $result = $this->methodInvoker->invoke($this->services[$service->getKey()], $eventParams); |
| 87 | 87 | } else { |
| 88 | 88 | $result = call_user_func($this->services[$service->getKey()], $service); |
| 89 | 89 | } |
| 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 | } |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | $pdo->exec("CREATE TABLE IF NOT EXISTS `{$tableName}` (`service_key` VARCHAR(255) NOT NULL DEFAULT '', `service_last_try` DATETIME NULL DEFAULT '2000-01-01 00:00:00', `service_last_run` DATETIME NULL DEFAULT '2000-01-01 00:00:00', `service_timeout` INT UNSIGNED NULL DEFAULT '0', PRIMARY KEY (`service_key`));"); |
| 38 | 38 | |
| 39 | 39 | $skipLocked = ''; |
| 40 | - if($options['skip-locked'] ?? false) { |
|
| 40 | + if ($options['skip-locked'] ?? false) { |
|
| 41 | 41 | $skipLocked = ' SKIP LOCKED'; |
| 42 | 42 | } |
| 43 | 43 | $this->selectServices = $pdo->prepare("SELECT service_key FROM `{$tableName}` WHERE DATE_ADD(service_last_run, INTERVAL service_timeout SECOND) <= NOW() ORDER BY GREATEST(service_last_try, service_last_run) ASC FOR UPDATE{$skipLocked};"); |
@@ -68,13 +68,13 @@ discard block |
||
| 68 | 68 | */ |
| 69 | 69 | public function store($key, $timeout, array $data = []) { |
| 70 | 70 | $key = trim(strtolower($key)); |
| 71 | - if(!in_array($key, $this->services)) { |
|
| 71 | + if (!in_array($key, $this->services)) { |
|
| 72 | 72 | $this->services[] = $key; |
| 73 | 73 | } else { |
| 74 | 74 | throw new RuntimeException("Duplicate service: {$key}"); |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | - if($this->has($key)) { |
|
| 77 | + if ($this->has($key)) { |
|
| 78 | 78 | $this->updateService->execute(['key' => $key, 'timeout' => $timeout]); |
| 79 | 79 | } else { |
| 80 | 80 | $this->insertService->execute(['key' => $key, 'timeout' => $timeout, 'try' => '2000-01-01 00:00:00', 'run' => '2000-01-01 00:00:00']); |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | $this->pdo->exec('START TRANSACTION'); |
| 94 | 94 | try { |
| 95 | 95 | $services = $this->fetchServices(); |
| 96 | - foreach($services as $service) { |
|
| 96 | + foreach ($services as $service) { |
|
| 97 | 97 | $this->updateTryDate->execute(['key' => $service['service_key']]); |
| 98 | 98 | $fn($service); |
| 99 | 99 | $this->updateRunDate->execute(['key' => $service['service_key']]); |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | } |
| 102 | 102 | $this->pdo->exec('COMMIT'); |
| 103 | 103 | return $count; |
| 104 | - } catch(Throwable $e) { |
|
| 104 | + } catch (Throwable $e) { |
|
| 105 | 105 | $this->pdo->exec('ROLLBACK'); |
| 106 | 106 | throw $e; |
| 107 | 107 | } |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | $this->selectServices->execute(); |
| 115 | 115 | try { |
| 116 | 116 | $services = $this->selectServices->fetchAll(PDO::FETCH_ASSOC); |
| 117 | - foreach($services as $service) { |
|
| 117 | + foreach ($services as $service) { |
|
| 118 | 118 | yield new Service($service['service_key']); |
| 119 | 119 | } |
| 120 | 120 | } finally { |