1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
service.php
См. документацию.
1<?php
2
3namespace Bitrix\Socialnetwork\Internals\EventService;
4
5use Bitrix\Main;
6use Bitrix\Main\Application;
7use Bitrix\Socialnetwork\Internals\EventService;
8
9class Service
10{
11 private const JOB_PRIORITY = Application::JOB_PRIORITY_LOW - 5;
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 // TODO: spaces stub
25 // $this->handleLostEvents();
26 }
27
31 public static function getInstance(): Service
32 {
33 if (!self::$instance)
34 {
35 self::$instance = new self();
36 }
37
38 return self::$instance;
39 }
40
45 public static function addEvent(string $type, array $data): void
46 {
47 self::getInstance()->pushEvent($type, $data);
48 // TODO: spaces stub
49 return;
50
51 // self::getInstance()->storeEvent($type, $data);
52 }
53
54 public static function proceedEvents(): void
55 {
57
58 // TODO: spaces stub
59 return;
60
61 if ((EventCollection::getInstance())->isEmpty())
62 {
63 Application::getConnection()->unlock(self::LOCK_KEY);
64 return;
65 }
66
67 $service = self::getInstance();
68
71
72 $service->done();
73 }
74
79 private function storeEvent(string $type, array $data = []): void
80 {
82
83 if ($this->getEventCollection()->isDuplicate($event))
84 {
85 return;
86 }
87
88 $eventId = $this->saveToDb($event);
89 $event->setId($eventId);
90
91 $this->getEventCollection()->push($event);
92 }
93
94 private function pushEvent(string $type, array $data = []): void
95 {
96 if (!in_array($type, EventDictionary::WORKGROUP_EVENTS_SUPPORTED, true))
97 {
98 return;
99 }
100
101 $event = EventService\Event\Factory::buildEvent(self::$hitId, $type, $data);
102
103 if ($this->getEventCollection()->isDuplicate($event))
104 {
105 return;
106 }
107
108 $this->getEventCollection()->push($event);
109 }
110
111 private function handleLostEvents(): void
112 {
113 if (!Application::getConnection()->lock(self::LOCK_KEY))
114 {
115 return;
116 }
117
118 $events = EventTable::getLostEvents();
119 if (empty($events))
120 {
121 return;
122 }
123
124 foreach ($events as $row)
125 {
126 $event = EventService\Event\Factory::buildEvent(
127 $row['HID'],
128 $row['TYPE'],
129 Main\Web\Json::decode($row['DATA']),
130 $row['ID']
131 );
132
133 $this->getEventCollection()->push($event);
134 }
135 }
136
140 private function enableJob(): void
141 {
142 if (!self::$isJobOn)
143 {
145 $application && $application->addBackgroundJob(
146 [ __CLASS__, 'proceedEvents' ],
147 [],
148 self::JOB_PRIORITY
149 );
150
151 self::$isJobOn = true;
152 }
153 }
154
158 private function getEventCollection(): EventCollection
159 {
160 return EventCollection::getInstance();
161 }
162
168 private function saveToDb(Event $event): int
169 {
170 try
171 {
172 $res = EventTable::add([
173 'HID' => self::$hitId,
174 'TYPE' => $event->getType(),
175 'DATA' => Main\Web\Json::encode($event->getData()),
176 'LOG_DATA' => null,
177 'PROCESSED' => Main\Type\DateTime::createFromTimestamp(0),
178 ]);
179 }
180 catch (\Exception $e)
181 {
182 return 0;
183 }
184
185 return (int)$res->getId();
186 }
187
191 private function done(): void
192 {
193 $ids = $this->getEventCollection()->getEventsId();
194 if (empty($ids))
195 {
196 return;
197 }
198
199 EventTable::markProcessed([
200 '@ID' => $ids
201 ]);
202
203 Application::getConnection()->unlock(self::LOCK_KEY);
204 }
205
209 private function generateHid(): string
210 {
211 return sha1(microtime(true) . mt_rand(10000, 99999));
212 }
213}
$type
Определения options.php:106
const JOB_PRIORITY_LOW
Определения application.php:32
static getInstance()
Определения application.php:98
static getConnection($name="")
Определения application.php:638
static buildEvent(string $hitId, string $type, array $data=[], int|null $eventId=null)
Определения factory.php:10
static addEvent(string $type, array $data)
Определения service.php:45
$data['IS_AVAILABLE']
Определения .description.php:13
</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
$application
Определения bitrix.php:23
$service
Определения payment.php:18
$event
Определения prolog_after.php:141