|
2 | 2 |
|
3 | 3 | namespace Enqueue\Rpc;
|
4 | 4 |
|
5 |
| -use Enqueue\Psr\PsrConsumer; |
6 | 5 | use Enqueue\Psr\PsrMessage;
|
7 | 6 |
|
8 | 7 | class Promise
|
9 | 8 | {
|
10 | 9 | /**
|
11 |
| - * @var PsrConsumer |
| 10 | + * @var \Closure |
12 | 11 | */
|
13 |
| - private $consumer; |
| 12 | + private $receiveCallback; |
14 | 13 |
|
15 | 14 | /**
|
16 |
| - * @var int |
| 15 | + * @var \Closure |
17 | 16 | */
|
18 |
| - private $timeout; |
| 17 | + private $receiveNoWaitCallback; |
19 | 18 |
|
20 | 19 | /**
|
21 |
| - * @var string |
| 20 | + * @var \Closure |
22 | 21 | */
|
23 |
| - private $correlationId; |
| 22 | + private $finallyCallback; |
24 | 23 |
|
25 | 24 | /**
|
26 |
| - * @param PsrConsumer $consumer |
27 |
| - * @param string $correlationId |
28 |
| - * @param int $timeout |
| 25 | + * @var bool |
29 | 26 | */
|
30 |
| - public function __construct(PsrConsumer $consumer, $correlationId, $timeout) |
| 27 | + private $deleteReplyQueue; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var PsrMessage |
| 31 | + */ |
| 32 | + private $message; |
| 33 | + |
| 34 | + /** |
| 35 | + * @param \Closure $receiveCallback |
| 36 | + * @param \Closure $receiveNoWaitCallback |
| 37 | + * @param \Closure $finallyCallback |
| 38 | + */ |
| 39 | + public function __construct(\Closure $receiveCallback, \Closure $receiveNoWaitCallback, \Closure $finallyCallback) |
31 | 40 | {
|
32 |
| - $this->consumer = $consumer; |
33 |
| - $this->timeout = $timeout; |
34 |
| - $this->correlationId = $correlationId; |
| 41 | + $this->receiveCallback = $receiveCallback; |
| 42 | + $this->receiveNoWaitCallback = $receiveNoWaitCallback; |
| 43 | + $this->finallyCallback = $finallyCallback; |
| 44 | + |
| 45 | + $this->deleteReplyQueue = true; |
35 | 46 | }
|
36 | 47 |
|
37 | 48 | /**
|
| 49 | + * Blocks until message received or timeout expired. |
| 50 | + * |
| 51 | + * @deprecated use "receive" instead |
| 52 | + * |
38 | 53 | * @throws TimeoutException if the wait timeout is reached
|
39 | 54 | *
|
40 | 55 | * @return PsrMessage
|
41 | 56 | */
|
42 | 57 | public function getMessage()
|
43 | 58 | {
|
44 |
| - $endTime = time() + $this->timeout; |
45 |
| - |
46 |
| - while (time() < $endTime) { |
47 |
| - if ($message = $this->consumer->receive($this->timeout)) { |
48 |
| - if ($message->getCorrelationId() === $this->correlationId) { |
49 |
| - $this->consumer->acknowledge($message); |
| 59 | + return $this->receive(); |
| 60 | + } |
50 | 61 |
|
51 |
| - return $message; |
| 62 | + /** |
| 63 | + * Blocks until message received or timeout expired. |
| 64 | + * |
| 65 | + * @throws TimeoutException if the wait timeout is reached |
| 66 | + * |
| 67 | + * @return PsrMessage |
| 68 | + */ |
| 69 | + public function receive() |
| 70 | + { |
| 71 | + if (null == $this->message) { |
| 72 | + try { |
| 73 | + if ($message = $this->doReceive($this->receiveCallback)) { |
| 74 | + $this->message = $message; |
52 | 75 | }
|
| 76 | + } finally { |
| 77 | + call_user_func($this->finallyCallback, $this); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return $this->message; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Non blocking function. Returns message or null. |
| 86 | + * |
| 87 | + * @return PsrMessage|null |
| 88 | + */ |
| 89 | + public function receiveNoWait() |
| 90 | + { |
| 91 | + if (null == $this->message) { |
| 92 | + if ($message = $this->doReceive($this->receiveNoWaitCallback)) { |
| 93 | + $this->message = $message; |
53 | 94 |
|
54 |
| - $this->consumer->reject($message, true); |
| 95 | + call_user_func($this->finallyCallback, $this); |
55 | 96 | }
|
56 | 97 | }
|
57 | 98 |
|
58 |
| - throw TimeoutException::create($this->timeout, $this->correlationId); |
| 99 | + return $this->message; |
59 | 100 | }
|
60 | 101 |
|
61 | 102 | /**
|
62 |
| - * @param int $timeout |
| 103 | + * On TRUE deletes reply queue after getMessage call. |
| 104 | + * |
| 105 | + * @param bool $delete |
63 | 106 | */
|
64 |
| - public function setTimeout($timeout) |
| 107 | + public function setDeleteReplyQueue($delete) |
65 | 108 | {
|
66 |
| - $this->timeout = $timeout; |
| 109 | + $this->deleteReplyQueue = (bool) $delete; |
67 | 110 | }
|
68 | 111 |
|
69 | 112 | /**
|
70 |
| - * @return int |
| 113 | + * @return bool |
71 | 114 | */
|
72 |
| - public function getTimeout() |
| 115 | + public function isDeleteReplyQueue() |
73 | 116 | {
|
74 |
| - return $this->timeout; |
| 117 | + return $this->deleteReplyQueue; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @param \Closure $cb |
| 122 | + * |
| 123 | + * @return PsrMessage |
| 124 | + */ |
| 125 | + private function doReceive(\Closure $cb) |
| 126 | + { |
| 127 | + $message = call_user_func($cb, $this); |
| 128 | + |
| 129 | + if (null !== $message && false == $message instanceof PsrMessage) { |
| 130 | + throw new \RuntimeException(sprintf( |
| 131 | + 'Expected "%s" but got: "%s"', PsrMessage::class, is_object($message) ? get_class($message) : gettype($message))); |
| 132 | + } |
| 133 | + |
| 134 | + return $message; |
75 | 135 | }
|
76 | 136 | }
|
0 commit comments