Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
queue.php
1<?php
2
4
7
8class Queue
9{
10 private array $popped = [];
11 private static Queue|null $instance = null;
12 private static array $inQueue = [];
13
14 public static function isInQueue(int $userId): bool
15 {
16 if (!array_key_exists($userId, self::$inQueue))
17 {
18 $res = QueueTable::getRow([
19 'filter' => [
20 '=USER_ID' => $userId
21 ]
22 ]);
23
24 self::$inQueue[$userId] = (bool) $res;
25 }
26
27 return self::$inQueue[$userId];
28 }
29
30 public static function getInstance()
31 {
32 if (!self::$instance)
33 {
34 self::$instance = new self();
35 }
36 return self::$instance;
37 }
38
42 private function __construct()
43 {
44
45 }
46
47 public function add(int $userId, string $type, array $logs): void
48 {
49 if (empty($logs))
50 {
51 return;
52 }
53
54 $req = [];
55 foreach ($logs as $logId)
56 {
57 $req[] = $userId .',"'. $type .'",' . (int) $logId;
58 }
59
60 $sql = "
61 INSERT INTO `". QueueTable::getTableName(). "`
62 (`USER_ID`, `TYPE`, `SONET_LOG_ID`)
63 VALUES
64 (". implode("),(", $req) .")
65 ";
66
67 Application::getConnection()->query($sql);
68
69 self::$inQueue[$userId] = true;
70 }
71
77 public function get(int $limit): array
78 {
79 if (!empty($this->popped))
80 {
81 throw new CounterQueuePopException();
82 }
83
84 $sql = "
85 SELECT
86 ID,
87 USER_ID,
88 TYPE,
89 SONET_LOG_ID
90 FROM `". QueueTable::getTableName() ."`
91 ORDER BY ID ASC
92 LIMIT {$limit}
93 ";
94
95 $res = Application::getConnection()->query($sql);
96
97 $queue = [];
98 while ($row = $res->fetch())
99 {
100 $this->popped[] = $row['ID'];
101
102 $userId = (int) $row['USER_ID'];
103 $type = $row['TYPE'];
104 $key = $userId.'_'.$type;
105
106 if (!array_key_exists($key, $queue))
107 {
108 $queue[$userId.'_'.$type] = [
109 'USER_ID' => $userId,
110 'TYPE' => $type
111 ];
112 }
113 $queue[$key]['SONET_LOGS'][] = (int) $row['SONET_LOG_ID'];
114 }
115
116 return $queue;
117 }
118
122 public function done(): void
123 {
124 if (empty($this->popped))
125 {
126 return;
127 }
128
129 $sql = "
130 DELETE
131 FROM `". QueueTable::getTableName() ."`
132 WHERE ID IN (". implode(",", $this->popped) .")
133 ";
134 Application::getConnection()->query($sql);
135
136 $this->popped = [];
137 }
138}
static getConnection($name="")
add(int $userId, string $type, array $logs)
Definition queue.php:47