-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDBConfigurableConsumer.php
More file actions
169 lines (142 loc) · 5.9 KB
/
DBConfigurableConsumer.php
File metadata and controls
169 lines (142 loc) · 5.9 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace Smartbox\Integration\FrameworkBundle\Components\DB;
use Smartbox\CoreBundle\Type\SerializableArray;
use Smartbox\Integration\FrameworkBundle\Components\DB\Dbal\ConfigurableDbalProtocol;
use Smartbox\Integration\FrameworkBundle\Components\DB\NoSQL\NoSQLConfigurableProtocol;
use Smartbox\Integration\FrameworkBundle\Configurability\IsConfigurableService;
use Smartbox\Integration\FrameworkBundle\Core\Consumers\AbstractConsumer;
use Smartbox\Integration\FrameworkBundle\Core\Consumers\ConfigurableConsumerInterface;
use Smartbox\Integration\FrameworkBundle\Core\Consumers\Exceptions\NoResultsException;
use Smartbox\Integration\FrameworkBundle\Core\Consumers\IsStopableConsumer;
use Smartbox\Integration\FrameworkBundle\Core\Endpoints\EndpointInterface;
use Smartbox\Integration\FrameworkBundle\Core\Messages\Context;
use Smartbox\Integration\FrameworkBundle\Core\Messages\MessageInterface;
use Smartbox\Integration\FrameworkBundle\DependencyInjection\Traits\UsesLogger;
use Smartbox\Integration\FrameworkBundle\DependencyInjection\Traits\UsesSmartesbHelper;
use Smartbox\Integration\FrameworkBundle\Service;
class DBConfigurableConsumer extends AbstractConsumer implements ConfigurableConsumerInterface
{
use IsConfigurableService;
/** @var ConfigurableStepsProviderInterface */
protected $configurableStepsProvider;
/**
* {@inheritdoc}
*/
protected function initialize(EndpointInterface $endpoint)
{
}
/**
* @return ConfigurableStepsProviderInterface
*/
public function getConfigurableStepsProvider()
{
return $this->configurableStepsProvider;
}
/**
* @param ConfigurableStepsProviderInterface $configurableStepsProvider
*/
public function setConfigurableStepsProvider($configurableStepsProvider)
{
$this->configurableStepsProvider = $configurableStepsProvider;
}
/**
* Reads a message from the NoSQL database executing the configured steps.
*
* @param EndpointInterface $endpoint
*
* @return \Smartbox\Integration\FrameworkBundle\Core\Messages\Message
*/
protected function readMessage(EndpointInterface $endpoint)
{
$options = $endpoint->getOptions();
$method = $options[NoSQLConfigurableProtocol::OPTION_METHOD];
$config = $this->methodsConfiguration[$method];
$steps = $config[ConfigurableConsumerInterface::CONFIG_QUERY_STEPS];
$context = $this->getConfHelper()->createContext($options);
try {
$this->configurableStepsProvider->executeSteps($steps, $options, $context);
$result = $this->getConfHelper()->resolve(
$config[ConfigurableConsumerInterface::CONFIG_QUERY_RESULT],
$context
);
} catch (NoResultsException $exception) {
$result = null;
if ($options[ConfigurableDbalProtocol::OPTION_STOP_ON_NO_RESULTS]) {
$this->stop();
}
}
if (null == $result) {
return null;
} elseif (is_array($result)) {
$result = new SerializableArray($result);
}
$context = new Context([
Context::FLOWS_VERSION => $this->getFlowsVersion(),
Context::TRANSACTION_ID => uniqid('', true),
Context::ORIGINAL_FROM => $endpoint->getURI(),
]);
return $this->smartesbHelper->getMessageFactory()->createMessage($result, [], $context);
}
/**
* Executes the necessary actions after the message has been consumed.
*
* @param EndpointInterface $endpoint
* @param MessageInterface $message
*/
protected function onConsume(EndpointInterface $endpoint, MessageInterface $message)
{
$options = $endpoint->getOptions();
$method = $options[NoSQLConfigurableProtocol::OPTION_METHOD];
$config = $this->methodsConfiguration[$method];
$steps = $config[ConfigurableConsumerInterface::CONFIG_ON_CONSUME];
$context = $this->getConfHelper()->createContext($options, $message);
$this->configurableStepsProvider->executeSteps($steps, $options, $context);
}
/**
* @param EndpointInterface $endpoint
*/
public function consume(EndpointInterface $endpoint)
{
$sleepTime = (int) $endpoint->getOption(ConfigurableDbalProtocol::OPTION_SLEEP_TIME) * 1000;
$inactivityTrigger = (int) $endpoint->getOption(ConfigurableDbalProtocol::OPTION_INACTIVITY_TRIGGER);
$wakeup = microtime(true);
while (!$this->shouldStop()) {
// Receive
$startConsumeTime = microtime(true);
$message = $this->readMessage($endpoint);
// Process
if ($message) {
--$this->expirationCount;
$endpoint->handle($message);
if ($this->logger) {
$this->logger->info(
'A message was consumed on {date}',
['date' => \DateTime::createFromFormat('U.u', microtime(true))->format('Y-m-d H:i:s.u')]
);
}
$this->confirmMessage($endpoint, $message);
$endConsumeTime = $wakeup = microtime(true);
$this->dispatchConsumerTimingEvent((int) (($endConsumeTime - $startConsumeTime) * 1000), $message);
}
if ((microtime(true) - $wakeup) > $inactivityTrigger) { // I did nothing since the last x seconds, so little nap...
usleep($sleepTime);
}
}
$this->cleanUp($endpoint);
}
/**
* {@inheritdoc}
*/
protected function cleanUp(EndpointInterface $endpoint)
{
//TODO: Connect to the steps provider and ask it to shut down its connection
}
/**
* {@inheritdoc}
*/
protected function confirmMessage(EndpointInterface $endpoint, MessageInterface $message)
{
$this->onConsume($endpoint, $message);
return $message;
}
}