Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
service.php
1<?php
2
4
8
9class Service
10{
11 private const JOB_PRIORITY = Application::JOB_PRIORITY_LOW - 2;
12 private const LOCK_KEY = 'sonet.eventlock';
13 private static Service|null $instance = null;
14 private static bool $isJobOn = false;
15 private static string $hitId;
16
20 private function __construct()
21 {
22 self::$hitId = $this->generateHid();
23 $this->enableJob();
24 $this->handleLostEvents();
25 }
26
30 public static function getInstance(): Service
31 {
32 if (!self::$instance)
33 {
34 self::$instance = new self();
35 }
36
37 return self::$instance;
38 }
39
44 public static function addEvent(string $type, array $data): void
45 {
46 self::getInstance()->storeEvent($type, $data);
47 }
48
49 public static function proceedEvents(): void
50 {
51 if ((EventCollection::getInstance())->isEmpty())
52 {
53 Application::getConnection()->unlock(self::LOCK_KEY);
54 return;
55 }
56
57 $service = self::getInstance();
58
61
62 $service->done();
63 }
64
69 private function storeEvent(string $type, array $data = []): void
70 {
71 $event = EventService\Event\Factory::buildEvent(self::$hitId, $type, $data);
72
73 if ($this->getEventCollection()->isDuplicate($event))
74 {
75 return;
76 }
77
78 $eventId = $this->saveToDb($event);
79 $event->setId($eventId);
80
81 $this->getEventCollection()->push($event);
82 }
83
84 private function handleLostEvents(): void
85 {
86 if (!Application::getConnection()->lock(self::LOCK_KEY))
87 {
88 return;
89 }
90
91 $events = EventTable::getLostEvents();
92 if (empty($events))
93 {
94 return;
95 }
96
97 foreach ($events as $row)
98 {
99 $event = EventService\Event\Factory::buildEvent(
100 $row['HID'],
101 $row['TYPE'],
102 Main\Web\Json::decode($row['DATA']),
103 $row['ID']
104 );
105
106 $this->getEventCollection()->push($event);
107 }
108 }
109
113 private function enableJob(): void
114 {
115 if (!self::$isJobOn)
116 {
117 $application = Application::getInstance();
118 $application && $application->addBackgroundJob(
119 [ __CLASS__, 'proceedEvents' ],
120 [],
121 self::JOB_PRIORITY
122 );
123
124 self::$isJobOn = true;
125 }
126 }
127
131 private function getEventCollection(): EventCollection
132 {
133 return EventCollection::getInstance();
134 }
135
141 private function saveToDb(Event $event): int
142 {
143 try
144 {
145 $res = EventTable::add([
146 'HID' => self::$hitId,
147 'TYPE' => $event->getType(),
148 'DATA' => Main\Web\Json::encode($event->getData()),
149 'LOG_DATA' => null,
150 ]);
151 }
152 catch (\Exception $e)
153 {
154 return 0;
155 }
156
157 return (int)$res->getId();
158 }
159
163 private function done(): void
164 {
165 $ids = $this->getEventCollection()->getEventsId();
166 if (empty($ids))
167 {
168 return;
169 }
170
171 EventTable::markProcessed([
172 '@ID' => $ids
173 ]);
174
175 Application::getConnection()->unlock(self::LOCK_KEY);
176 }
177
181 private function generateHid(): string
182 {
183 return sha1(microtime(true) . mt_rand(10000, 99999));
184 }
185}
static getConnection($name="")
static addEvent(string $type, array $data)
Definition service.php:44