1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Werkspot\MessageQueue\Message; |
4
|
|
|
|
5
|
|
|
use DateInterval; |
6
|
|
|
use DateTime; |
7
|
|
|
use DateTimeImmutable; |
8
|
|
|
use Ramsey\Uuid\Uuid; |
9
|
|
|
use Throwable; |
10
|
|
|
|
11
|
|
|
class Message implements MessageInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $id; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $destination; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var mixed |
25
|
|
|
*/ |
26
|
|
|
private $payload; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
private $priority; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var DateTimeImmutable |
35
|
|
|
*/ |
36
|
|
|
private $deliverAt; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var DateTimeImmutable |
40
|
|
|
*/ |
41
|
|
|
private $createdAt; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var DateTimeImmutable|null |
45
|
|
|
*/ |
46
|
|
|
private $updatedAt; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
private $tries = 0; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string|null |
55
|
|
|
*/ |
56
|
|
|
private $errors; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
private $metadata; |
62
|
|
|
|
63
|
12 |
|
public function __construct( |
64
|
|
|
$payload, |
65
|
|
|
string $destination, |
66
|
|
|
DateTimeImmutable $deliverAt, |
67
|
|
|
int $priority, |
68
|
|
|
array $metadata = [] |
69
|
|
|
) { |
70
|
12 |
|
$this->id = Uuid::uuid4()->toString(); |
71
|
12 |
|
$this->payload = $payload; |
72
|
12 |
|
$this->destination = $destination; |
73
|
12 |
|
$this->deliverAt = $deliverAt; |
74
|
12 |
|
$this->priority = $priority; |
75
|
12 |
|
$this->metadata = $metadata; |
76
|
12 |
|
$this->createdAt = new DateTimeImmutable(); |
77
|
12 |
|
$this->updatedAt = $this->createdAt; |
78
|
12 |
|
} |
79
|
|
|
|
80
|
6 |
|
public function getId(): string |
81
|
|
|
{ |
82
|
6 |
|
return $this->id; |
83
|
|
|
} |
84
|
|
|
|
85
|
10 |
|
public function getDestination(): string |
86
|
|
|
{ |
87
|
10 |
|
return $this->destination; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
11 |
|
public function getPayload() |
94
|
|
|
{ |
95
|
11 |
|
return $this->payload; |
96
|
|
|
} |
97
|
|
|
|
98
|
11 |
|
public function getPriority(): int |
99
|
|
|
{ |
100
|
11 |
|
return $this->priority; |
101
|
|
|
} |
102
|
|
|
|
103
|
11 |
|
public function getDeliverAt(): DateTimeImmutable |
104
|
|
|
{ |
105
|
11 |
|
return $this->deliverAt; |
106
|
|
|
} |
107
|
|
|
|
108
|
6 |
|
public function getCreatedAt(): DateTimeImmutable |
109
|
|
|
{ |
110
|
6 |
|
return $this->createdAt; |
111
|
|
|
} |
112
|
|
|
|
113
|
6 |
|
public function getUpdatedAt(): DateTimeImmutable |
114
|
|
|
{ |
115
|
6 |
|
return $this->updatedAt; |
116
|
|
|
} |
117
|
|
|
|
118
|
10 |
|
public function getTries(): int |
119
|
|
|
{ |
120
|
10 |
|
return $this->tries; |
121
|
|
|
} |
122
|
|
|
|
123
|
5 |
|
public function getErrors(): ?string |
124
|
|
|
{ |
125
|
5 |
|
return $this->errors; |
126
|
|
|
} |
127
|
|
|
|
128
|
4 |
|
public function fail(Throwable $error): void |
129
|
|
|
{ |
130
|
4 |
|
$now = new DateTimeImmutable(); |
131
|
|
|
|
132
|
4 |
|
$errorMessage = sprintf( |
133
|
4 |
|
"[%s] '%s': '%s'\n%s", |
134
|
4 |
|
$now->format(DateTime::ATOM), |
135
|
4 |
|
get_class($error), |
136
|
4 |
|
$error->getMessage(), |
137
|
4 |
|
$error->getTraceAsString() |
138
|
|
|
); |
139
|
|
|
|
140
|
4 |
|
$this->errors .= $errorMessage . "\n\n"; |
141
|
|
|
|
142
|
4 |
|
$this->tries++; |
143
|
4 |
|
$this->updateDeliveryDate(); |
144
|
4 |
|
} |
145
|
|
|
|
146
|
5 |
|
public function getMetadata(): array |
147
|
|
|
{ |
148
|
5 |
|
return $this->metadata; |
149
|
|
|
} |
150
|
|
|
|
151
|
4 |
|
private function updateDeliveryDate(): void |
152
|
|
|
{ |
153
|
4 |
|
$this->deliverAt = $this->defineDeliveryDate(); |
154
|
4 |
|
} |
155
|
|
|
|
156
|
4 |
|
private function defineDeliveryDate(): DateTimeImmutable |
157
|
|
|
{ |
158
|
4 |
|
$interval = $this->getDateTimeImmutableIntervalForTry($this->tries + 1); |
159
|
|
|
|
160
|
4 |
|
return $this->deliverAt->add($interval); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* By default we try the command in: |
165
|
|
|
* - try 1: 0 minutes |
166
|
|
|
* - try 2: 1 minutes |
167
|
|
|
* - try 3: 4 minutes |
168
|
|
|
* - try 4: 9 minutes |
169
|
|
|
* |
170
|
|
|
* @param int $try The try of the command, try 1 is the first time the message is delivered |
171
|
|
|
*/ |
172
|
4 |
|
private function getDateTimeImmutableIntervalForTry(int $try): DateInterval |
173
|
|
|
{ |
174
|
4 |
|
$waitingTimeInMinutes = ($try - 1) * ($try - 1); |
175
|
|
|
|
176
|
4 |
|
return new DateInterval(sprintf('PT%dM', $waitingTimeInMinutes)); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|