-
Notifications
You must be signed in to change notification settings - Fork 6
/
PheanstalkConsumer.php
119 lines (97 loc) · 3.06 KB
/
PheanstalkConsumer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
declare(strict_types=1);
namespace Enqueue\Pheanstalk;
use Interop\Queue\Consumer;
use Interop\Queue\Exception\InvalidMessageException;
use Interop\Queue\Message;
use Interop\Queue\Queue;
use Pheanstalk\Job;
use Pheanstalk\Pheanstalk;
class PheanstalkConsumer implements Consumer
{
/**
* @var PheanstalkDestination
*/
private $destination;
/**
* @var Pheanstalk
*/
private $pheanstalk;
public function __construct(PheanstalkDestination $destination, Pheanstalk $pheanstalk)
{
$this->destination = $destination;
$this->pheanstalk = $pheanstalk;
}
/**
* @return PheanstalkDestination
*/
public function getQueue(): Queue
{
return $this->destination;
}
/**
* @return PheanstalkMessage
*/
public function receive(int $timeout = 0): ?Message
{
if (0 === $timeout) {
while (true) {
if ($job = $this->pheanstalk->reserveFromTube($this->destination->getName(), 5)) {
return $this->convertJobToMessage($job);
}
}
} else {
if ($job = $this->pheanstalk->reserveFromTube($this->destination->getName(), $timeout / 1000)) {
return $this->convertJobToMessage($job);
}
}
return null;
}
/**
* @return PheanstalkMessage
*/
public function receiveNoWait(): ?Message
{
if ($job = $this->pheanstalk->reserveFromTube($this->destination->getName(), 0)) {
return $this->convertJobToMessage($job);
}
return null;
}
/**
* @param PheanstalkMessage $message
*/
public function acknowledge(Message $message): void
{
InvalidMessageException::assertMessageInstanceOf($message, PheanstalkMessage::class);
if (false == $message->getJob()) {
throw new \LogicException('The message could not be acknowledged because it does not have job set.');
}
$this->pheanstalk->delete($message->getJob());
}
/**
* @param PheanstalkMessage $message
*/
public function reject(Message $message, bool $requeue = false): void
{
InvalidMessageException::assertMessageInstanceOf($message, PheanstalkMessage::class);
if (false == $message->getJob()) {
$state = $requeue ? 'requeued' : 'rejected';
throw new \LogicException(sprintf('The message could not be %s because it does not have job set.', $state));
}
if ($requeue) {
$this->pheanstalk->release($message->getJob(), $message->getPriority(), $message->getDelay());
return;
}
$this->acknowledge($message);
}
private function convertJobToMessage(Job $job): PheanstalkMessage
{
$stats = $this->pheanstalk->statsJob($job);
$message = PheanstalkMessage::jsonUnserialize($job->getData());
if (isset($stats['reserves'])) {
$message->setRedelivered($stats['reserves'] > 1);
}
$message->setJob($job);
return $message;
}
}