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;
20 private function __construct()
22 self::$hitId = $this->generateHid();
24 $this->handleLostEvents();
34 self::$instance =
new self();
37 return self::$instance;
44 public static function addEvent(
string $type, array $data): void
46 self::getInstance()->storeEvent($type, $data);
51 if ((EventCollection::getInstance())->isEmpty())
57 $service = self::getInstance();
69 private function storeEvent(
string $type, array $data = []): void
71 $event = EventService\Event\Factory::buildEvent(self::$hitId, $type, $data);
73 if ($this->getEventCollection()->isDuplicate($event))
78 $eventId = $this->saveToDb($event);
79 $event->setId($eventId);
81 $this->getEventCollection()->push($event);
84 private function handleLostEvents(): void
91 $events = EventTable::getLostEvents();
97 foreach ($events as $row)
99 $event = EventService\Event\Factory::buildEvent(
102 Main\Web\Json::decode($row[
'DATA']),
106 $this->getEventCollection()->push($event);
113 private function enableJob(): void
118 $application && $application->addBackgroundJob(
119 [ __CLASS__,
'proceedEvents' ],
124 self::$isJobOn =
true;
131 private function getEventCollection(): EventCollection
133 return EventCollection::getInstance();
141 private function saveToDb(Event $event): int
145 $res = EventTable::add([
146 'HID' => self::$hitId,
147 'TYPE' => $event->getType(),
148 'DATA' => Main\Web\Json::encode($event->getData()),
152 catch (\Exception $e)
157 return (
int)$res->getId();
163 private function done(): void
165 $ids = $this->getEventCollection()->getEventsId();
171 EventTable::markProcessed([
181 private function generateHid(): string
183 return sha1(microtime(
true) . mt_rand(10000, 99999));
static getConnection($name="")