1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
queue.php
См. документацию.
1<?php
2
3namespace Bitrix\Socialnetwork\Internals\EventService\Queue;
4
5use Bitrix\Main\UserTable;
6use Bitrix\Main\Web\Json;
7use Bitrix\Socialnetwork\Internals\EventService\Event;
8use Bitrix\Socialnetwork\Internals\EventService\EventTable;
9use Bitrix\Socialnetwork\Internals\EventService\Exception\CounterQueuePopException;
10use Bitrix\Socialnetwork\Internals\EventService\Processors\SpaceEventProcessor;
11use Bitrix\Socialnetwork\Internals\EventService\Recepients\Recepient;
12
13class Queue
14{
15 const PRIORITY_LOW = 2;
16 const PRIORITY_MEDIUM = 1;
17 const PRIORITY_HIGH = 0;
18
19 private array $popped = [];
20 private array $buffer = [];
21 private static Queue|null $instance = null;
22
23 public static function getInstance()
24 {
25 if (!self::$instance)
26 {
27 self::$instance = new self();
28 }
29 return self::$instance;
30 }
31
32 private function __construct()
33 {
34
35 }
36
37 public function add(Event $event, Recepient $user): void
38 {
39 $this->buffer[] = [
40 'EVENT_ID' => $event->getId(),
41 'USER_ID' => $user->getId(),
42 'PRIORITY' => $this->getPriority($user),
43 ];
44 }
45
46 public function save(): void
47 {
48 if (empty($this->buffer))
49 {
50 return;
51 }
52
53 QueueTable::addMulti($this->buffer, true);
54 $this->buffer = [];
55 (new Agent())->addAgent();
56 }
57
58 public function process(): int
59 {
60 $spaceProcessor = new SpaceEventProcessor();
61 $queue = $this->get($spaceProcessor->getStepLimit());
62 $events = $this->getEvents($queue['event_ids']);
63 $users = $this->getUsers($queue['user_ids']);
64
65 foreach ($queue['rows'] as $row)
66 {
67 $event = (isset($events[$row['event_id']])) ? $events[$row['event_id']] : null;
68 $user = (isset($users[$row['user_id']])) ? $users[$row['user_id']] : null;
69
70 if ($event && $user)
71 {
72 $spaceProcessor->processEventForUser($event, $user);
73 }
74 }
75
76 $this->done();
77
78 return count($queue['rows']);
79 }
80
81 private function get(int $limit): array
82 {
83 if (!empty($this->popped))
84 {
85 throw new CounterQueuePopException();
86 }
87
89 'select' => [
90 'ID',
91 'USER_ID',
92 'EVENT_ID',
93 'PRIORITY',
94 ],
95 'order' => [
96 'PRIORITY' => 'ASC',
97 'DATETIME' => 'ASC',
98 ],
99 'limit' => $limit,
100 ]);
101
102 $queue = [
103 'event_ids' => [],
104 'user_ids' => [],
105 'rows' => [],
106 ];
107
108 while ($row = $iterator->fetch())
109 {
110 $this->popped[] = $row['ID'];
111
112 $userId = (int) $row['USER_ID'];
113 $eventId = (int) $row['EVENT_ID'];
114 $queue['user_ids'][] = $userId;
115 $queue['event_ids'][] = $eventId;
116 $queue['rows'][] = [
117 'user_id' => $userId,
118 'event_id' => $eventId,
119 ];
120 }
121
122 return $queue;
123 }
124
125 private function done(): void
126 {
127 if (empty($this->popped))
128 {
129 return;
130 }
131
132 QueueTable::deleteByFilter([
133 'ID' => $this->popped,
134 ]);
135
136 $this->popped = [];
137 }
138
139 private function getPriority(Recepient $user): int
140 {
141 if (!$user->isOnline())
142 {
143 return self::PRIORITY_LOW;
144 }
145
146 if ($user->isWatchingSpaces())
147 {
148 return self::PRIORITY_HIGH;
149 }
150
151 return self::PRIORITY_MEDIUM;
152 }
153
154 private function getEvents(array $eventIds): array
155 {
156 if (empty($eventIds))
157 {
158 return [];
159 }
160
162 'select' => ['ID', 'HID', 'TYPE', 'DATA'],
163 'filter' => [
164 'ID' => $eventIds,
165 ],
166 ]);
167
168 $events = [];
169
170 while ($row = $res->fetch())
171 {
172 $events[$row['ID']] = Event\Factory::buildEvent(
173 $row['HID'],
174 $row['TYPE'],
175 Json::decode($row['DATA']),
176 $row['ID']
177 );
178 }
179
180 return $events;
181 }
182
183 private function getUsers(array $userIds): array
184 {
185 if (empty($userIds))
186 {
187 return [];
188 }
189
190 $res = UserTable::getList([
191 'select' => ['ID', 'IS_ONLINE'],
192 'filter' => [
193 'ID' => $userIds,
194 ],
195 ]);
196
197 $users = [];
198
199 while ($row = $res->fetch())
200 {
201 $isOnline = ($user['IS_ONLINE'] ?? 'Y') === 'Y';
202 $users[$row['ID']] = new Recepient($row['ID'], $isOnline);
203 }
204
205 return $users;
206 }
207}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getList(array $parameters=array())
Определения datamanager.php:431
static addMulti($rows, $ignoreEvents=false)
Определения datamanager.php:1041
add(Event $event, Recepient $user)
Определения queue.php:37
buffer
Определения ebay_mip_setup.php:303
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$user
Определения mysql_to_pgsql.php:33
$event
Определения prolog_after.php:141
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$iterator
Определения yandex_run.php:610