@@ -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 | } |
@@ -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 | } |
@@ -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']); |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | public function lockAndIterateServices($fn) { |
89 | 89 | $count = 0; |
90 | 90 | $services = $this->fetchServices(); |
91 | - foreach($services as $service) { |
|
91 | + foreach ($services as $service) { |
|
92 | 92 | $this->updateTryDate->execute(['key' => $service->getKey()]); |
93 | 93 | $this->pdo->exec('BEGIN EXCLUSIVE TRANSACTION'); |
94 | 94 | try { |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | $this->updateRunDate->execute(['key' => $service->getKey()]); |
98 | 98 | $count++; |
99 | 99 | $this->pdo->exec('COMMIT'); |
100 | - } catch(Throwable $e) { |
|
100 | + } catch (Throwable $e) { |
|
101 | 101 | $this->pdo->exec('ROLLBACK'); |
102 | 102 | throw $e; |
103 | 103 | } |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | $this->selectServices->execute(); |
113 | 113 | try { |
114 | 114 | $services = $this->selectServices->fetchAll(PDO::FETCH_ASSOC); |
115 | - foreach($services as $service) { |
|
115 | + foreach ($services as $service) { |
|
116 | 116 | yield new Service($service['service_key']); |
117 | 117 | } |
118 | 118 | } finally { |
@@ -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']); |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | public function lockAndIterateServices($fn) { |
92 | 92 | $count = 0; |
93 | 93 | $services = $this->fetchServices(); |
94 | - foreach($services as $service) { |
|
94 | + foreach ($services as $service) { |
|
95 | 95 | $this->updateTryDate->execute(['key' => $service->getKey()]); |
96 | 96 | $this->pdo->exec('START TRANSACTION'); |
97 | 97 | try { |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | $this->updateRunDate->execute(['key' => $service->getKey()]); |
101 | 101 | $count++; |
102 | 102 | $this->pdo->exec('COMMIT'); |
103 | - } catch(Throwable $e) { |
|
103 | + } catch (Throwable $e) { |
|
104 | 104 | $this->pdo->exec('ROLLBACK'); |
105 | 105 | throw $e; |
106 | 106 | } |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | $this->selectServices->execute(); |
116 | 116 | try { |
117 | 117 | $services = $this->selectServices->fetchAll(PDO::FETCH_ASSOC); |
118 | - foreach($services as $service) { |
|
118 | + foreach ($services as $service) { |
|
119 | 119 | yield new Service($service['service_key']); |
120 | 120 | } |
121 | 121 | } finally { |