Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
groupqueueservice.php
1<?php
2
4
10
12{
13 private const LOCK_KEY = 'group_queue';
14 private const LIFETIME = 9800;
15
23 public function addToDB(int $type, int $entityId, int $groupId)
24 {
25 if (!in_array($type, GroupQueueTable::TYPE))
26 {
27 return;
28 }
29 if (!Locker::lock(self::LOCK_KEY, $entityId))
30 {
31 return;
32 }
33
34 $current = $this->getCurrentRow($type, $entityId, $groupId);
35 if (isset($current['ID']) || isset($current[0]['ID']))
36 {
37 Locker::unlock(self::LOCK_KEY, $entityId);
38 return;
39 }
40
41 GroupQueueTable::add([
42 'TYPE' => $type,
43 'ENTITY_ID' => $entityId,
44 'GROUP_ID' => $groupId,
45 ]);
46 Locker::unlock(self::LOCK_KEY, $entityId);
47 }
48
56 public function releaseGroup(int $type, int $entityId, int $groupId)
57 {
58 $current = $this->getCurrentRow($type, $entityId, $groupId)[0];
59 if (!isset($current['ID']))
60 {
61 return;
62 }
63
64 GroupQueueTable::delete($current['ID']);
65
66 $this->isReleased($groupId);
67 }
68
77 public function isReleased(int $groupId): bool
78 {
79 $entities = GroupQueueTable::query()
80 ->setSelect([
81 'ID',
82 'DATE_INSERT',
83 ])
84 ->where('GROUP_ID', $groupId)
85 ->exec()
86 ->fetchAll();
87
88 foreach ($entities as $key =>$entity)
89 {
90 $dateTime = DateTime::createFromPhp(new \DateTime());
91
92 if (!$entity['DATE_INSERT'] || abs($dateTime->getTimestamp() - $entity['DATE_INSERT']->getTimestamp()) > self::LIFETIME)
93 {
94 try
95 {
96 GroupQueueTable::delete($entity['ID']);
97 } catch (\Exception $e)
98 {
99 }
100 unset($entities[$key]);
101 }
102 }
103
104 if (empty($entities))
105 {
106 GroupTable::update($groupId, [
107 'fields' => ['STATUS' => GroupTable::STATUS_DONE]
108 ]);
109 }
110
111 return empty($entities);
112 }
113
114 private function getCurrentRow(int $type, int $entityId, int $groupId): array
115 {
116 return GroupQueueTable::query()
117 ->setSelect(['ID'])
118 ->where('TYPE', $type)
119 ->where('ENTITY_ID', $entityId)
120 ->where('GROUP_ID', $groupId)
121 ->exec()
122 ->fetchAll();
123 }
124
125 public function isEntityProcessed(int $type, int $entityId)
126 {
127 return GroupQueueTable::query()
128 ->setSelect([
129 'ID',
130 'DATE_INSERT',
131 ])
132 ->where('ENTITY_ID', $entityId)
133 ->where('TYPE', $type)
134 ->exec()
135 ->fetch();
136 }
137}
static createFromPhp(\DateTime $datetime)
Definition datetime.php:232
releaseGroup(int $type, int $entityId, int $groupId)
addToDB(int $type, int $entityId, int $groupId)