Skip to content

Commit 3118461

Browse files
authored
Merge pull request #1 from maksimru/fix/worker-stability
Fix/worker stability
2 parents 89d5e7f + 5fba0d4 commit 3118461

File tree

4 files changed

+80
-3
lines changed

4 files changed

+80
-3
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,44 @@ In your ```config/queue.php``` file you have to provide the following:
5151
In your ```config/app.php``` add ```'Forumhouse\LaravelAmqp\ServiceProvider\LaravelAmqpServiceProvider'``` to the list of service
5252
providers registered.
5353

54+
Improved worker stability (PHP 7.1+ is required)
55+
------------
56+
57+
For better stability please add following code in app/Exceptions/Handler.php:
58+
59+
```php
60+
class Handler extends ExceptionHandler
61+
{
62+
```
63+
64+
to
65+
66+
```php
67+
class Handler extends ExceptionHandler
68+
{
69+
use AMQPFailureDetector;
70+
```
71+
72+
And
73+
74+
```php
75+
public function report(Exception $exception)
76+
{
77+
parent::report($exception);
78+
}
79+
```
80+
81+
to
82+
83+
```php
84+
public function report(Exception $exception)
85+
{
86+
$this->catchAMQPConnectionFailure($exception);
87+
parent::report($exception);
88+
}
89+
```
90+
91+
5492
Usage
5593
------------
5694

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"minimum-stability": "stable",
55
"license": "GPL-2.0",
66
"require": {
7-
"php": ">=7",
7+
"php": ">=7.1",
88
"php-amqplib/php-amqplib": "2.6.*"
99
},
1010
"require-dev": {

src/Jobs/AMQPJob.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,14 @@ public function getQueue()
125125
* Get the job identifier.
126126
*
127127
* @return string
128-
* @throws \OutOfBoundsException
129128
*/
130129
public function getJobId()
131130
{
132-
return $this->amqpMessage->get('message_id');
131+
try {
132+
return $this->amqpMessage->get('message_id');
133+
} catch (\OutOfBoundsException $exception){
134+
return null;
135+
}
133136
}
134137

135138
}

src/Traits/AMQPFailureDetector.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Forumhouse\LaravelAmqp\Traits;
4+
use Exception;
5+
use Illuminate\Support\Str;
6+
7+
trait AMQPFailureDetector
8+
{
9+
10+
public function catchAMQPConnectionFailure(Exception $exception)
11+
{
12+
if(\App::runningInConsole() && $this->causedByLostConnection($exception) && extension_loaded('posix')){
13+
posix_kill(getmypid(), SIGTERM);
14+
}
15+
}
16+
17+
protected function causedByLostConnection(Exception $e)
18+
{
19+
$message = $e->getMessage();
20+
21+
return Str::contains($message, [
22+
'server has gone away',
23+
'no connection to the server',
24+
'Lost connection',
25+
'is dead or not enabled',
26+
'Error while sending',
27+
'decryption failed or bad record mac',
28+
'server closed the connection unexpectedly',
29+
'SSL connection has been closed unexpectedly',
30+
'Error writing data to the connection',
31+
'Resource deadlock avoided',
32+
'Transaction() on null',
33+
'child connection forced to terminate due to client_idle_limit',
34+
]);
35+
}
36+
}

0 commit comments

Comments
 (0)