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