forked from oroinc/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterruptConsumptionExtension.php
More file actions
82 lines (67 loc) · 2.08 KB
/
Copy pathInterruptConsumptionExtension.php
File metadata and controls
82 lines (67 loc) · 2.08 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
74
75
76
77
78
79
80
81
82
<?php
namespace Oro\Bundle\MessageQueueBundle\Consumption\Extension;
use Oro\Bundle\MessageQueueBundle\Consumption\CacheState;
use Oro\Bundle\MessageQueueBundle\Consumption\InterruptConsumptionExtensionTrait;
use Oro\Component\MessageQueue\Consumption\AbstractExtension;
use Oro\Component\MessageQueue\Consumption\Context;
class InterruptConsumptionExtension extends AbstractExtension
{
use InterruptConsumptionExtensionTrait;
/**
* @var int
*/
protected $timestamp;
/**
* Date time when the consumer was started.
*
* @var \DateTime
*/
protected $startTime;
/**
* @var CacheState
*/
protected $cacheState;
/**
* @param string $filePath
*/
public function __construct($filePath, CacheState $cacheState)
{
$this->touch($filePath);
$this->filePath = $filePath;
$this->timestamp = filemtime($filePath);
$this->startTime = new \DateTime('now', new \DateTimeZone('UTC'));
$this->cacheState = $cacheState;
}
/**
* {@inheritdoc}
*/
public function onBeforeReceive(Context $context)
{
if (!file_exists($this->filePath)) {
$this->interruptExecution($context, 'The cache was cleared.');
return;
}
clearstatcache(true, $this->filePath);
if (filemtime($this->filePath) > $this->timestamp) {
$this->interruptExecution($context, 'The cache was invalidated.');
return;
}
$cacheChangeDate = $this->cacheState->getChangeDate();
if ($cacheChangeDate && $cacheChangeDate > $this->startTime) {
$this->interruptExecution($context, 'The cache has changed.');
}
}
/**
* @param Context $context
* @param string $reason
*/
private function interruptExecution(Context $context, $reason)
{
$context->getLogger()->info(
'Execution interrupted: ' . $reason,
['context' => $context]
);
$context->setExecutionInterrupted(true);
$context->setInterruptedReason($reason);
}
}