1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
MessageBox.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
5namespace Bitrix\Main\Messenger\Entity;
6
7use Bitrix\Main\ArgumentException;
8use Bitrix\Main\DI\ServiceLocator;
9use Bitrix\Main\Messenger\Internals\Config\QueueConfigRegistry;
10use Bitrix\Main\Type\DateTime;
11
15final class MessageBox
16{
17 private ?int $id = null;
18
19 private string $queueId;
20
21 private int|string|null $itemId = null;
22
23 private string $className;
24
25 private DateTime $createdAt;
26
27 private DateTime $availableAt;
28
29 private int $ttl = 3;
30
31 private bool $rejected = false;
32
33 public function __construct(private readonly MessageInterface $message)
34 {
35 $this->className = get_class($this->message);
36 $this->createdAt = new DateTime();
37 $this->availableAt = new DateTime();
38 }
39
40 public function getId(): ?int
41 {
42 return $this->id;
43 }
44
45 public function setId(int $id): self
46 {
47 $this->id = $id;
48
49 return $this;
50 }
51
52 public function getQueueId(): string
53 {
54 return $this->queueId;
55 }
56
57 public function setQueueId(string $queueId): self
58 {
59 $this->queueId = $queueId;
60
61 return $this;
62 }
63
64 public function getMessage(): MessageInterface
65 {
66 return $this->message;
67 }
68
69 public function getCreatedAt(): DateTime
70 {
71 return $this->createdAt;
72 }
73
74 public function setCreatedAt(DateTime $createdAt): self
75 {
76 $this->createdAt = $createdAt;
77
78 return $this;
79 }
80
81 public function getItemId(): int|string|null
82 {
83 return $this->itemId;
84 }
85
86 public function setItemId(int|string|null $itemId): self
87 {
88 $this->itemId = $itemId;
89
90 return $this;
91 }
92
93 public function getClassName(): string
94 {
95 return $this->className;
96 }
97
98 public function getAvailableAt(): DateTime
99 {
100 return $this->availableAt;
101 }
102
103 public function setAvailableAt(DateTime $availableAt): self
104 {
105 $this->availableAt = $availableAt;
106
107 return $this;
108 }
109
110 public function getTtl(): int
111 {
112 return $this->ttl;
113 }
114
120 public function setTtl(int $ttl): self
121 {
122 if ($ttl < 0)
123 {
124 throw new ArgumentException('Ttl value should be a not negative number');
125 }
126
127 $this->ttl = $ttl;
128
129 return $this;
130 }
131
132 public function isRejected(): bool
133 {
134 return $this->rejected;
135 }
136
137 public function isDied(): bool
138 {
139 return $this->ttl < 1;
140 }
141
142 public function reject(): void
143 {
144 $this->rejected = true;
145
146 $this->ttl--;
147
148 if ($this->isDied())
149 {
150 return;
151 }
152
153 if ($this->availableAt > new DateTime())
154 {
155 return;
156 }
157
158 $retryStrategy = ServiceLocator::getInstance()
159 ->get(QueueConfigRegistry::class)
160 ->getQueueConfig($this->queueId)
161 ->retryStrategy
162 ;
163
164 $retry = $retryStrategy->getMaxRetryCount() - $this->ttl;
165
166 $availableAt = new DateTime();
167
168 $this->availableAt = $availableAt->add(
169 sprintf(
170 '+%s seconds',
171 intval($retryStrategy->getWaitingTime($retry) / 1000)
172 )
173 );
174 }
175
176 public function kill(): self
177 {
178 $this->ttl = 0;
179
180 $this->rejected = true;
181
182 return $this;
183 }
184
185 public function requeue(?int $retryDelay): self
186 {
187 $this->ttl++;
188
189 if ($retryDelay > 0)
190 {
191 $availableAt = new DateTime();
192
193 $this->availableAt = $availableAt->add(sprintf('+%s seconds', $retryDelay));
194 }
195
196 return $this;
197 }
198}
__construct(private readonly MessageInterface $message)
Определения MessageBox.php:33
setQueueId(string $queueId)
Определения MessageBox.php:57
setItemId(int|string|null $itemId)
Определения MessageBox.php:86
setAvailableAt(DateTime $availableAt)
Определения MessageBox.php:103
requeue(?int $retryDelay)
Определения MessageBox.php:185
setCreatedAt(DateTime $createdAt)
Определения MessageBox.php:74
$message
Определения payment.php:8