|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of graze/queue. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2015 Nature Delivered Ltd. <https://www.graze.com> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @license https://github.com/graze/queue/blob/master/LICENSE MIT |
|
12
|
|
|
* |
|
13
|
|
|
* @link https://github.com/graze/queue |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Graze\Queue\Handler; |
|
17
|
|
|
|
|
18
|
|
|
use Exception; |
|
19
|
|
|
use Graze\Queue\Adapter\AdapterInterface; |
|
20
|
|
|
use Graze\Queue\Message\MessageInterface; |
|
21
|
|
|
use Iterator; |
|
22
|
|
|
|
|
23
|
|
|
abstract class AbstractAcknowledgementHandler |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @param MessageInterface $message |
|
27
|
|
|
* @param AdapterInterface $adapter |
|
28
|
|
|
* @param mixed $result |
|
29
|
|
|
*/ |
|
30
|
|
|
abstract protected function acknowledge( |
|
31
|
|
|
MessageInterface $message, |
|
32
|
|
|
AdapterInterface $adapter, |
|
33
|
|
|
$result = null |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param MessageInterface $message |
|
38
|
|
|
* @param AdapterInterface $adapter |
|
39
|
|
|
* @param int $duration Number of seconds to ensure that this message is not seen by any other clients |
|
40
|
|
|
*/ |
|
41
|
|
|
abstract protected function extend( |
|
42
|
|
|
MessageInterface $message, |
|
43
|
|
|
AdapterInterface $adapter, |
|
44
|
|
|
$duration |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param MessageInterface $message |
|
49
|
|
|
* @param AdapterInterface $adapter |
|
50
|
|
|
* @param mixed $result |
|
51
|
|
|
*/ |
|
52
|
|
|
abstract protected function reject( |
|
53
|
|
|
MessageInterface $message, |
|
54
|
|
|
AdapterInterface $adapter, |
|
55
|
|
|
$result = null |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param AdapterInterface $adapter |
|
60
|
|
|
*/ |
|
61
|
|
|
abstract protected function flush(AdapterInterface $adapter); |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param Iterator $messages |
|
65
|
|
|
* @param AdapterInterface $adapter |
|
66
|
|
|
* @param callable $worker |
|
67
|
|
|
* |
|
68
|
|
|
* @throws Exception |
|
69
|
|
|
*/ |
|
70
|
23 |
|
public function __invoke(Iterator $messages, AdapterInterface $adapter, callable $worker) |
|
71
|
|
|
{ |
|
72
|
|
|
// Used to break from polling consumer |
|
73
|
23 |
|
$break = false; |
|
74
|
23 |
|
$done = function () use (&$break) { |
|
75
|
2 |
|
$break = true; |
|
76
|
23 |
|
}; |
|
77
|
|
|
|
|
78
|
|
|
try { |
|
79
|
23 |
|
foreach ($messages as $message) { |
|
80
|
18 |
|
if ($message->isValid()) { |
|
81
|
18 |
|
$result = call_user_func($worker, $message, $done); |
|
82
|
18 |
|
$this->acknowledge($message, $adapter, $result); |
|
83
|
18 |
|
} |
|
84
|
|
|
|
|
85
|
18 |
|
if ($break) { |
|
86
|
2 |
|
break; |
|
87
|
|
|
} |
|
88
|
23 |
|
} |
|
89
|
23 |
|
} catch (Exception $e) { |
|
90
|
3 |
|
$this->flush($adapter); |
|
91
|
3 |
|
throw $e; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
20 |
|
$this->flush($adapter); |
|
95
|
20 |
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|