-
Notifications
You must be signed in to change notification settings - Fork 13
/
DbalProducer.php
166 lines (136 loc) · 4.96 KB
/
DbalProducer.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
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
<?php
declare(strict_types=1);
namespace Enqueue\Dbal;
use Interop\Queue\Destination;
use Interop\Queue\Exception\Exception;
use Interop\Queue\Exception\InvalidDestinationException;
use Interop\Queue\Exception\InvalidMessageException;
use Interop\Queue\Message;
use Interop\Queue\Producer;
use Ramsey\Uuid\Uuid;
class DbalProducer implements Producer
{
/**
* @var int|null
*/
private $priority;
/**
* @var int|float|null
*/
private $deliveryDelay;
/**
* @var int|float|null
*/
private $timeToLive;
/**
* @var DbalContext
*/
private $context;
public function __construct(DbalContext $context)
{
$this->context = $context;
}
/**
* @param DbalDestination $destination
* @param DbalMessage $message
*/
public function send(Destination $destination, Message $message): void
{
InvalidDestinationException::assertDestinationInstanceOf($destination, DbalDestination::class);
InvalidMessageException::assertMessageInstanceOf($message, DbalMessage::class);
if (null !== $this->priority && null === $message->getPriority()) {
$message->setPriority($this->priority);
}
if (null !== $this->deliveryDelay && null === $message->getDeliveryDelay()) {
$message->setDeliveryDelay($this->deliveryDelay);
}
if (null !== $this->timeToLive && null === $message->getTimeToLive()) {
$message->setTimeToLive($this->timeToLive);
}
$body = $message->getBody();
$publishedAt = null !== $message->getPublishedAt() ?
$message->getPublishedAt() :
(int) (microtime(true) * 10000)
;
$dbalMessage = [
'id' => Uuid::uuid4(),
'published_at' => $publishedAt,
'body' => $body,
'headers' => JSON::encode($message->getHeaders()),
'properties' => JSON::encode($message->getProperties()),
'priority' => -1 * $message->getPriority(),
'queue' => $destination->getQueueName(),
'redelivered' => false,
'delivery_id' => null,
'redeliver_after' => null,
];
$delay = $message->getDeliveryDelay();
if ($delay) {
if (!is_int($delay)) {
throw new \LogicException(sprintf('Delay must be integer but got: "%s"', is_object($delay) ? get_class($delay) : gettype($delay)));
}
if ($delay <= 0) {
throw new \LogicException(sprintf('Delay must be positive integer but got: "%s"', $delay));
}
$dbalMessage['delayed_until'] = time() + (int) ($delay / 1000);
}
$timeToLive = $message->getTimeToLive();
if ($timeToLive) {
if (!is_int($timeToLive)) {
throw new \LogicException(sprintf('TimeToLive must be integer but got: "%s"', is_object($timeToLive) ? get_class($timeToLive) : gettype($timeToLive)));
}
if ($timeToLive <= 0) {
throw new \LogicException(sprintf('TimeToLive must be positive integer but got: "%s"', $timeToLive));
}
$dbalMessage['time_to_live'] = time() + (int) ($timeToLive / 1000);
}
try {
$rowsAffected = $this->context->getDbalConnection()->insert($this->context->getTableName(), $dbalMessage, [
'id' => DbalType::GUID,
'published_at' => DbalType::INTEGER,
'body' => DbalType::TEXT,
'headers' => DbalType::TEXT,
'properties' => DbalType::TEXT,
'priority' => DbalType::SMALLINT,
'queue' => DbalType::STRING,
'time_to_live' => DbalType::INTEGER,
'delayed_until' => DbalType::INTEGER,
'redelivered' => DbalType::SMALLINT,
'delivery_id' => DbalType::STRING,
'redeliver_after' => DbalType::BIGINT,
]);
if (1 !== $rowsAffected) {
throw new Exception('The message was not enqueued. Dbal did not confirm that the record is inserted.');
}
} catch (\Exception $e) {
throw new Exception('The transport fails to send the message due to some internal error.', 0, $e);
}
}
public function setDeliveryDelay(int $deliveryDelay = null): Producer
{
$this->deliveryDelay = $deliveryDelay;
return $this;
}
public function getDeliveryDelay(): ?int
{
return $this->deliveryDelay;
}
public function setPriority(int $priority = null): Producer
{
$this->priority = $priority;
return $this;
}
public function getPriority(): ?int
{
return $this->priority;
}
public function setTimeToLive(int $timeToLive = null): Producer
{
$this->timeToLive = $timeToLive;
return $this;
}
public function getTimeToLive(): ?int
{
return $this->timeToLive;
}
}