forked from graze/queue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBatchAcknowledgementHandler.php
67 lines (57 loc) · 1.39 KB
/
BatchAcknowledgementHandler.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
<?php
/**
* This file is part of graze/queue.
*
* Copyright (c) 2015 Nature Delivered Ltd. <https://www.graze.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license https://github.com/graze/queue/blob/master/LICENSE MIT
*
* @link https://github.com/graze/queue
*/
namespace Graze\Queue\Handler;
use Graze\Queue\Adapter\AdapterInterface;
use Graze\Queue\Message\MessageInterface;
class BatchAcknowledgementHandler extends AbstractAcknowledgementHandler
{
/**
* @var int
*/
protected $batchSize;
/**
* @var MessageInterface[]
*/
protected $messages = [];
/**
* @param int $batchSize
*/
public function __construct($batchSize = 0)
{
$this->batchSize = (integer) $batchSize;
}
/**
* {@inheritdoc}
*/
protected function acknowledge(
MessageInterface $message,
AdapterInterface $adapter,
$result = null
) {
$this->messages[] = $message;
if (count($this->messages) === $this->batchSize) {
$this->flush($adapter);
}
}
/**
* {@inheritdoc}
*/
protected function flush(AdapterInterface $adapter)
{
if (! empty($this->messages)) {
$adapter->acknowledge($this->messages);
$this->messages = [];
}
}
}