1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
QueueService.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Recent\Initializer;
4
5use Bitrix\Im\Model\RecentInitQueueTable;
6use Bitrix\Im\V2\Recent\Initializer\Queue\QueueItem;
7use Bitrix\Main\Application;
8
10{
11 protected const LOCK_TTL = 1;
12 protected const LOCK_NAME = 'recent_init_dequeue';
13
14 protected static self $instance;
15
16 private function __construct()
17 {
18 }
19
20 public static function getInstance(): static
21 {
22 self::$instance ??= new static();
23
24 return self::$instance;
25 }
26
27 public function save(?QueueItem $queueItem): ?QueueItem
28 {
29 if ($queueItem === null)
30 {
31 return null;
32 }
33
34 if ($queueItem->id === 0)
35 {
36 return $this->add($queueItem);
37 }
38
39 return $this->update($queueItem);
40 }
41
42 protected function add(QueueItem $queueItem): ?QueueItem
43 {
44 if ($queueItem->id !== 0)
45 {
46 return $queueItem;
47 }
48
49 $result = RecentInitQueueTable::add($this->getFieldsForAddOrUpdate($queueItem));
50
51 if (!$result->isSuccess())
52 {
53 return null;
54 }
55
56 $id = $result->getId();
57
58 return $queueItem->setId($id);
59 }
60
61 public function addMulti(array $items): void
62 {
63 RecentInitQueueTable::addMulti($this->getFieldsForMultiAdd($items), true);
64 }
65
67 {
68 return array_map(fn (QueueItem $item) => $this->getFieldsForAddOrUpdate($item), $items);
69 }
70
71 protected function update(QueueItem $queueItem): ?QueueItem
72 {
73 if ($queueItem->id === 0)
74 {
75 return null;
76 }
77
78 RecentInitQueueTable::update($queueItem->id, $this->getFieldsForAddOrUpdate($queueItem));
79
80 return $queueItem;
81 }
82
83 protected function getFieldsForAddOrUpdate(QueueItem $queueItem): array
84 {
85 $fields = $queueItem->getFields();
86 unset($fields['ID']);
87
88 return $fields;
89 }
90
91 public function getFirst(): ?QueueItem
92 {
93 $lock = $this->lock();
94 if (!$lock)
95 {
96 return null;
97 }
98
99 try
100 {
101 return $this->getFirstInternal()?->lock();
102 }
103 finally
104 {
105 $this->unlock();
106 }
107 }
108
109 public function isQueueEmpty(): bool
110 {
111 $row = RecentInitQueueTable::query()
112 ->setSelect(['ID'])
113 ->setLimit(1)
114 ->fetch()
115 ;
116
117 return $row === false;
118 }
119
120 public function delete(QueueItem $queueItem): void
121 {
122 if ($queueItem->id)
123 {
124 RecentInitQueueTable::delete($queueItem->id);
125 }
126 }
127
128 protected function getFirstInternal(): ?QueueItem
129 {
130 $row = RecentInitQueueTable::query()
131 ->setSelect(['*'])
132 ->where('IS_LOCKED', false)
133 ->setLimit(1)
134 ->setOrder(['ID' => 'ASC'])
135 ->fetch()
136 ;
137
138 if (!$row)
139 {
140 return null;
141 }
142
143 return QueueItem::createFromRow($row);
144 }
145
146 protected function lock(): bool
147 {
148 return Application::getConnection(RecentInitQueueTable::getConnectionName())
149 ->lock(self::LOCK_NAME, self::LOCK_TTL)
150 ;
151 }
152
153 protected function unlock(): bool
154 {
155 return Application::getConnection(RecentInitQueueTable::getConnectionName())->unlock(self::LOCK_NAME);
156 }
157}
update(QueueItem $queueItem)
Определения QueueService.php:71
save(?QueueItem $queueItem)
Определения QueueService.php:27
getFieldsForMultiAdd(array $items)
Определения QueueService.php:66
add(QueueItem $queueItem)
Определения QueueService.php:42
getFieldsForAddOrUpdate(QueueItem $queueItem)
Определения QueueService.php:83
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$items
Определения template.php:224
$fields
Определения yandex_run.php:501