1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
DbStorage.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
6
15
17{
18 private const LOCK_KEY = 'queueLock';
19
20 private const LOCK_TIMEOUT = 0;
21
22 private MessageRepository $repository;
23
24 private static bool $locked;
25
26 public function __construct(Entity $tableEntity)
27 {
28 $this->repository = new MessageRepository($tableEntity);
29 }
30
31 public function __destruct()
32 {
33 $this->unlock();
34 }
35
40 public function getOneByQueue(string $queueId): ?MessageBox
41 {
42 if (!$this->lock())
43 {
44 return null;
45 }
46
47 $messageBox = $this->repository->getOneByQueue($queueId);
48
49 if ($messageBox !== null)
50 {
51 $this->repository->updateStatus(new MessageBoxCollection($messageBox), MessageStatus::Processing);
52 }
53
54 return $messageBox;
55 }
56
61 public function getReadyMessagesOfQueue(string $queueId, int $limit = 500): iterable
62 {
63 if (!$this->lock())
64 {
65 return [];
66 }
67
68 $messageBoxes = $this->repository->getReadyMessagesOfQueue($queueId, $limit);
69
70 $this->repository->updateStatus($messageBoxes, MessageStatus::Processing);
71
72 return $messageBoxes;
73 }
74
78 public function save(MessageBox $messageBox): void
79 {
80 $this->repository->save($messageBox);
81 }
82
86 public function delete(MessageBox $messageBox): void
87 {
88 $this->repository->delete($messageBox);
89 }
90
91 private function lock(): bool
92 {
93 if (isset(self::$locked))
94 {
95 return self::$locked;
96 }
97
98 return self::$locked = Application::getConnection()->lock(self::LOCK_KEY, self::LOCK_TIMEOUT);
99 }
100
101 private function unlock(): void
102 {
103 Application::getConnection()->unlock(self::LOCK_KEY);
104
105 self::$locked = false;
106 }
107
111 public function unlockStaleMessages(): void
112 {
113 $messageBoxes = $this->repository->getStaleMessages();
114
115 $this->repository->updateStatus($messageBoxes, MessageStatus::New);
116 }
117}
static getConnection($name="")
Определения application.php:638
getReadyMessagesOfQueue(string $queueId, int $limit=500)
Определения DbStorage.php:61
__construct(Entity $tableEntity)
Определения DbStorage.php:26
save(MessageBox $messageBox)
Определения DbStorage.php:78
Определения ufield.php:9