|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\AMQP\Client\Channel\Basic\Consumer; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\AMQP\{ |
|
7
|
|
|
Client\Channel\Basic\Consumer as ConsumerInterface, |
|
8
|
|
|
Model\Basic\Message, |
|
9
|
|
|
Exception\Reject, |
|
10
|
|
|
Exception\Requeue, |
|
11
|
|
|
Exception\Cancel |
|
12
|
|
|
}; |
|
13
|
|
|
use Psr\Log\LoggerInterface; |
|
14
|
|
|
|
|
15
|
|
|
final class Logger implements ConsumerInterface |
|
16
|
|
|
{ |
|
17
|
|
|
private $consumer; |
|
18
|
|
|
private $logger; |
|
19
|
|
|
|
|
20
|
9 |
|
public function __construct( |
|
21
|
|
|
ConsumerInterface $consumer, |
|
22
|
|
|
LoggerInterface $logger |
|
23
|
|
|
) { |
|
24
|
9 |
|
$this->consumer = $consumer; |
|
25
|
9 |
|
$this->logger = $logger; |
|
26
|
9 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
public function foreach(callable $consume): void |
|
32
|
|
|
{ |
|
33
|
6 |
|
$this->consumer->foreach(function(Message $message, ...$args) use ($consume): void { |
|
34
|
|
|
try { |
|
35
|
5 |
|
$this->logger->debug( |
|
36
|
5 |
|
'AMQP message received', |
|
37
|
5 |
|
['body' => (string) $message->body()] |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
5 |
|
$consume($message, ...$args); |
|
41
|
4 |
|
} catch (Reject $e) { |
|
42
|
1 |
|
$this->logger->warning( |
|
43
|
1 |
|
'AMQP message rejected', |
|
44
|
1 |
|
['body' => (string) $message->body()] |
|
45
|
|
|
); |
|
46
|
1 |
|
throw $e; |
|
47
|
3 |
|
} catch (Requeue $e) { |
|
48
|
1 |
|
$this->logger->info( |
|
49
|
1 |
|
'AMQP message requeued', |
|
50
|
1 |
|
['body' => (string) $message->body()] |
|
51
|
|
|
); |
|
52
|
1 |
|
throw $e; |
|
53
|
2 |
|
} catch (Cancel $e) { |
|
54
|
1 |
|
$this->logger->warning( |
|
55
|
1 |
|
'AMQP consumer canceled', |
|
56
|
1 |
|
['body' => (string) $message->body()] |
|
57
|
|
|
); |
|
58
|
1 |
|
throw $e; |
|
59
|
1 |
|
} catch (\Throwable $e) { |
|
60
|
1 |
|
$this->logger->error( |
|
61
|
1 |
|
'AMQP message consumption generated an exception', |
|
62
|
|
|
[ |
|
63
|
1 |
|
'body' => (string) $message->body(), |
|
64
|
1 |
|
'exception' => get_class($e), |
|
65
|
|
|
] |
|
66
|
|
|
); |
|
67
|
1 |
|
throw $e; |
|
68
|
|
|
} |
|
69
|
6 |
|
}); |
|
70
|
6 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function take(int $count): ConsumerInterface |
|
76
|
|
|
{ |
|
77
|
1 |
|
$this->consumer = $this->consumer->take($count); |
|
78
|
|
|
|
|
79
|
1 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public function filter(callable $predicate): ConsumerInterface |
|
86
|
|
|
{ |
|
87
|
1 |
|
$this->consumer = $this->consumer->filter(function(Message $message, ...$args) use ($predicate): bool { |
|
88
|
1 |
|
$return = $predicate($message, ...$args); |
|
89
|
|
|
|
|
90
|
1 |
|
if (!$return) { |
|
91
|
1 |
|
$this->logger->info( |
|
92
|
1 |
|
'AMQP message was filtered', |
|
93
|
1 |
|
['body' => (string) $message->body()] |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
return $return; |
|
98
|
1 |
|
}); |
|
99
|
|
|
|
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|