Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
pushworker.php
1<?php
2
4
9
10final class PushWorker
11{
12 private bool $canUse;
13 private static $setJob = false;
14 private static $queue = [];
15
16 public function __construct()
17 {
18 $this->canUse = Loader::includeModule('pull');
19 }
20
21 public function subscribe(int $userId, string $command): void
22 {
23 if ($this->canUse)
24 {
25 \CPullWatch::Add($userId, $command);
26 }
27 }
28
29 public function send(string $command, array $params, array $userIds = []): void
30 {
31 if (empty($userIds))
32 {
33 $userIds = [CurrentUser::get()?->getId() ?? 0];
34 }
35
36 if ($this->canUse)
37 {
38 Pull\Event::add(
39 $userIds,
40 [
41 'module_id' => 'bizproc',
42 'command' => $command,
43 'params' => $params,
44 ]
45 );
46 }
47 }
48
49 public function sendLast(string $tag, string $command, array $params, array $userIds = [])
50 {
51 if ($this->canUse)
52 {
53 self::$queue[$tag] = [$command, $params, $userIds];
54 $this->setBackgroundJob();
55 }
56 }
57
58 private function setBackgroundJob()
59 {
60 if (!self::$setJob)
61 {
62 Main\Application::getInstance()->addBackgroundJob(
63 [__CLASS__, 'doBackgroundJob'],
64 [],
66 );
67 self::$setJob = true;
68 }
69 }
70
71 public static function doBackgroundJob()
72 {
73 $push = new self();
74
75 foreach (self::$queue as [$command, $params, $userIds])
76 {
77 $push->send($command, $params, $userIds);
78 }
79 }
80}
sendLast(string $tag, string $command, array $params, array $userIds=[])
subscribe(int $userId, string $command)
send(string $command, array $params, array $userIds=[])