Repository navigation
Expand file tree
/
Copy pathPromise.php
More file actions
68 lines (57 loc) · 1.44 KB
/
Copy pathPromise.php
File metadata and controls
68 lines (57 loc) · 1.44 KB
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
<?php
namespace Enqueue\Rpc;
use Enqueue\Psr\PsrConsumer;
class Promise
{
/**
* @var PsrConsumer
*/
private $consumer;
/**
* @var int
*/
private $timeout;
/**
* @var string
*/
private $correlationId;
/**
* @param PsrConsumer $consumer
* @param string $correlationId
* @param int $timeout
*/
public function __construct(PsrConsumer $consumer, $correlationId, $timeout)
{
$this->consumer = $consumer;
$this->timeout = $timeout;
$this->correlationId = $correlationId;
}
public function getMessage()
{
$endTime = time() + $this->timeout;
while (time() < $endTime) {
if ($message = $this->consumer->receive($this->timeout)) {
if ($message->getCorrelationId() === $this->correlationId) {
$this->consumer->acknowledge($message);
return $message;
}
$this->consumer->reject($message, true);
}
}
throw new \LogicException(sprintf('Time outed without receiving reply message. Timeout: %s, CorrelationId: %s', $this->timeout, $this->correlationId));
}
/**
* @param int $timeout
*/
public function setTimeout($timeout)
{
$this->timeout = $timeout;
}
/**
* @return int
*/
public function getTimeout()
{
return $this->timeout;
}
}