Repository navigation
Expand file tree
/
Copy pathLimitConsumerMemoryExtension.php
More file actions
73 lines (62 loc) · 1.68 KB
/
Copy pathLimitConsumerMemoryExtension.php
File metadata and controls
73 lines (62 loc) · 1.68 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
69
70
71
72
73
<?php
namespace Enqueue\Consumption\Extension;
use Enqueue\Consumption\Context;
use Enqueue\Consumption\EmptyExtensionTrait;
use Enqueue\Consumption\ExtensionInterface;
class LimitConsumerMemoryExtension implements ExtensionInterface
{
use EmptyExtensionTrait;
/**
* @var int
*/
protected $memoryLimit;
/**
* @param int $memoryLimit Megabytes
*/
public function __construct($memoryLimit)
{
if (false == is_int($memoryLimit)) {
throw new \InvalidArgumentException(sprintf(
'Expected memory limit is int but got: "%s"',
is_object($memoryLimit) ? get_class($memoryLimit) : gettype($memoryLimit)
));
}
$this->memoryLimit = $memoryLimit * 1024 * 1024;
}
/**
* {@inheritdoc}
*/
public function onBeforeReceive(Context $context)
{
$this->checkMemory($context);
}
/**
* {@inheritdoc}
*/
public function onPostReceived(Context $context)
{
$this->checkMemory($context);
}
/**
* {@inheritdoc}
*/
public function onIdle(Context $context)
{
$this->checkMemory($context);
}
/**
* @param Context $context
*/
protected function checkMemory(Context $context)
{
$memoryUsage = memory_get_usage(true);
if ($memoryUsage >= $this->memoryLimit) {
$context->getLogger()->debug(sprintf(
'[LimitConsumerMemoryExtension] Interrupt execution as memory limit reached. limit: "%s", used: "%s"',
$this->memoryLimit,
$memoryUsage
));
$context->setExecutionInterrupted(true);
}
}
}