Passed
Pull Request — master (#1025)
by butschster
09:34
created
src/Queue/tests/Interceptor/Consume/RetryPolicyInterceptorTest.php 1 patch
Braces   +37 added lines, -15 removed lines patch added patch discarded remove patch
@@ -78,9 +78,12 @@  discard block
 block discarded – undo
78 78
                 retryPolicy: new \Spiral\Queue\RetryPolicy(maxAttempts: 2, delay: 4)
79 79
             ));
80 80
 
81
-        try {
81
+        try
82
+        {
82 83
             $this->interceptor->process('foo-job', 'bar', [], $this->core);
83
-        } catch (RetryException $e) {
84
+        }
85
+        catch (RetryException $e)
86
+        {
84 87
             $this->assertSame(4, $e->getOptions()->getDelay());
85 88
             $this->assertSame(['attempts' => ['1']], $e->getOptions()->getHeaders());
86 89
         }
@@ -111,9 +114,12 @@  discard block
 block discarded – undo
111 114
             ->with(self::class, 'bar', [])
112 115
             ->willThrowException(new TestRetryException());
113 116
 
114
-        try {
117
+        try
118
+        {
115 119
             $this->interceptor->process(self::class, 'bar', [], $this->core);
116
-        } catch (RetryException $e) {
120
+        }
121
+        catch (RetryException $e)
122
+        {
117 123
             $this->assertSame(1, $e->getOptions()->getDelay());
118 124
             $this->assertSame(['attempts' => ['1']], $e->getOptions()->getHeaders());
119 125
         }
@@ -131,9 +137,12 @@  discard block
 block discarded – undo
131 137
                 retryPolicy: new \Spiral\Queue\RetryPolicy(maxAttempts: 2, delay: 4)
132 138
             ));
133 139
 
134
-        try {
140
+        try
141
+        {
135 142
             $this->interceptor->process(self::class, 'bar', [], $this->core);
136
-        } catch (RetryException $e) {
143
+        }
144
+        catch (RetryException $e)
145
+        {
137 146
             $this->assertSame(4, $e->getOptions()->getDelay());
138 147
             $this->assertSame(['attempts' => ['1']], $e->getOptions()->getHeaders());
139 148
         }
@@ -151,14 +160,17 @@  discard block
 block discarded – undo
151 160
             ->with(self::class, 'bar', ['headers' => ['attempts' => ['1']]])
152 161
             ->willThrowException(new TestRetryException());
153 162
 
154
-        try {
163
+        try
164
+        {
155 165
             $this->interceptor->process(
156 166
                 self::class,
157 167
                 'bar',
158 168
                 ['headers' => ['attempts' => ['1']]],
159 169
                 $this->core
160 170
             );
161
-        } catch (RetryException $e) {
171
+        }
172
+        catch (RetryException $e)
173
+        {
162 174
             $this->assertSame(8, $e->getOptions()->getDelay());
163 175
             $this->assertSame(['attempts' => ['2']], $e->getOptions()->getHeaders());
164 176
         }
@@ -178,14 +190,17 @@  discard block
 block discarded – undo
178 190
                 retryPolicy: new \Spiral\Queue\RetryPolicy(maxAttempts: 3, delay: 4, multiplier: 2)
179 191
             ));
180 192
 
181
-        try {
193
+        try
194
+        {
182 195
             $this->interceptor->process(
183 196
                 self::class,
184 197
                 'bar',
185 198
                 ['headers' => ['attempts' => ['1']]],
186 199
                 $this->core
187 200
             );
188
-        } catch (RetryException $e) {
201
+        }
202
+        catch (RetryException $e)
203
+        {
189 204
             $this->assertSame(8, $e->getOptions()->getDelay());
190 205
             $this->assertSame(['attempts' => ['2']], $e->getOptions()->getHeaders());
191 206
         }
@@ -202,7 +217,8 @@  discard block
 block discarded – undo
202 217
             ->method('callAction')
203 218
             ->with(self::class, 'bar', ['headers' => ['attempts' => ['1']]])
204 219
             ->willThrowException(new TestRetryException(
205
-                retryPolicy: new class implements RetryPolicyInterface {
220
+                retryPolicy: new class implements RetryPolicyInterface
221
+                {
206 222
                     public function isRetryable(\Throwable $exception, int $attempts = 0): bool
207 223
                     {
208 224
                         return true;
@@ -215,14 +231,17 @@  discard block
 block discarded – undo
215 231
                 }
216 232
             ));
217 233
 
218
-        try {
234
+        try
235
+        {
219 236
             $this->interceptor->process(
220 237
                 self::class,
221 238
                 'bar',
222 239
                 ['headers' => ['attempts' => ['1']]],
223 240
                 $this->core
224 241
             );
225
-        } catch (RetryException $e) {
242
+        }
243
+        catch (RetryException $e)
244
+        {
226 245
             $this->assertSame(5, $e->getOptions()->getDelay());
227 246
             $this->assertSame(['attempts' => ['2']], $e->getOptions()->getHeaders());
228 247
         }
@@ -244,14 +263,17 @@  discard block
 block discarded – undo
244 263
                 )
245 264
             ));
246 265
 
247
-        try {
266
+        try
267
+        {
248 268
             $this->interceptor->process(
249 269
                 self::class,
250 270
                 'bar',
251 271
                 ['headers' => ['attempts' => ['1']]],
252 272
                 $this->core
253 273
             );
254
-        } catch (RetryException $e) {
274
+        }
275
+        catch (RetryException $e)
276
+        {
255 277
             $this->assertSame(8, $e->getOptions()->getDelay());
256 278
             $this->assertSame(['attempts' => ['2']], $e->getOptions()->getHeaders());
257 279
         }
Please login to merge, or discard this patch.
src/Queue/src/Interceptor/Consume/RetryPolicyInterceptor.php 1 patch
Braces   +11 added lines, -5 removed lines patch added patch discarded remove patch
@@ -23,22 +23,27 @@  discard block
 block discarded – undo
23 23
 
24 24
     public function process(string $controller, string $action, array $parameters, CoreInterface $core): mixed
25 25
     {
26
-        try {
26
+        try
27
+        {
27 28
             return $core->callAction($controller, $action, $parameters);
28
-        } catch (\Throwable $e) {
29
+        }
30
+        catch (\Throwable $e)
31
+        {
29 32
             // In some cases job handler class may not exist or be just a string.
30 33
             // In this case we can't get retry policy from it.
31 34
             $class = \class_exists($controller) ? new \ReflectionClass($controller) : null;
32 35
             $policy = $this->getRetryPolicy($e, $class);
33 36
 
34
-            if ($policy === null) {
37
+            if ($policy === null)
38
+            {
35 39
                 throw $e;
36 40
             }
37 41
 
38 42
             $headers = $parameters['headers'] ?? [];
39 43
             $attempts = (int)($headers['attempts'][0] ?? 0);
40 44
 
41
-            if ($policy->isRetryable($e, $attempts) === false) {
45
+            if ($policy->isRetryable($e, $attempts) === false)
46
+            {
42 47
                 throw $e;
43 48
             }
44 49
 
@@ -57,7 +62,8 @@  discard block
 block discarded – undo
57 62
             ? $this->reader->firstClassMetadata($handler, Attribute::class)
58 63
             : null;
59 64
 
60
-        if ($exception instanceof JobException && $exception->getPrevious() !== null) {
65
+        if ($exception instanceof JobException && $exception->getPrevious() !== null)
66
+        {
61 67
             $exception = $exception->getPrevious();
62 68
         }
63 69
 
Please login to merge, or discard this patch.