1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
Queue.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Async;
4
5use Bitrix\Im\V2\Async\Promise\BackgroundJobPromise;
6use Bitrix\Im\V2\Async\Promise\State;
7use Bitrix\Main\Application;
8use Bitrix\Main\SystemException;
9use Throwable;
10
11class Queue
12{
13 private static self $instance;
14 private ?Throwable $lastException = null;
15 private bool $isScheduled = false;
16 private bool $isInBackground = false;
20 private array $promises = [];
21 private array $promisesInProgress = [];
22 private array $completedPromises = [];
23 private array $keysToRemove = [];
24
25 private function __construct()
26 {
27 }
28
29 public static function getInstance(): static
30 {
31 self::$instance ??= new self();
32
33 return self::$instance;
34 }
35
36 public function add(BackgroundJobPromise $promise): void
37 {
38 if ($this->isInBackground && $promise->immediateIfBackground())
39 {
40 $promise->onWait();
41 $this->completedPromises[$promise->id] = $promise;
42
43 return;
44 }
45
46 $this->promises[$promise->id] = $promise;
47 $this->schedule();
48 }
49
50 public function onAfterPromiseComplete(BackgroundJobPromise $promise): void
51 {
52 $this->keysToRemove[] = $promise->id;
53 $this->completedPromises[$promise->id] = $promise;
54 }
55
59 public function wait(?BackgroundJobPromise $target = null): mixed
60 {
61 if ($target && $target->getState() !== State::Pending)
62 {
63 return $target->getResult();
64 }
65
66 try
67 {
68 $result = $this->processQueue($target);
69 $this->processLastException();
70
71 return $result;
72 }
73 finally
74 {
75 $this->cleanQueue();
76 }
77 }
78
82 private function processQueue(?BackgroundJobPromise $target = null): mixed
83 {
84 while (($promise = current($this->promises)) !== false)
85 {
86 $this->processPromise($promise);
87
88 if ($target && isset($this->completedPromises[$target->id]))
89 {
90 return $this->completedPromises[$target->id]->getResult();
91 }
92
93 next($this->promises);
94 }
95
96 if ($target)
97 {
98 $this->lastException = new SystemException('The target promise was not fulfilled');
99 }
100
101 return null;
102 }
103
107 private function processLastException(): void
108 {
109 if (!$this->lastException)
110 {
111 return;
112 }
113
114 $exception = $this->lastException;
115 $this->lastException = null;
116
117 throw $exception;
118 }
119
120 private function processPromise(Promise $promise): void
121 {
122 if (isset($this->promisesInProgress[$promise->id]))
123 {
124 return;
125 }
126
127 $this->promisesInProgress[$promise->id] = $promise;
128
129 try
130 {
131 $promise->onWait();
132 }
133 catch (Throwable $exception)
134 {
135 $this->lastException = $exception;
136 }
137
138 unset($this->promisesInProgress[$promise->id]);
139 }
140
141 private function runInBackground(): void
142 {
143 $this->isInBackground = true;
144 $this->wait();
145 $this->isScheduled = false;
146 }
147
148 private function schedule(): void
149 {
150 if ($this->isScheduled)
151 {
152 return;
153 }
154
155 Application::getInstance()->addBackgroundJob(fn () => $this->runInBackground());
156 $this->isScheduled = true;
157 }
158
159 private function cleanQueue(): void
160 {
161 foreach ($this->keysToRemove as $key)
162 {
163 unset($this->promises[$key]);
164 }
165
166 $this->keysToRemove = [];
167 }
168}
wait(?BackgroundJobPromise $target=null)
Определения Queue.php:59
onAfterPromiseComplete(BackgroundJobPromise $promise)
Определения Queue.php:50
static getInstance()
Определения Queue.php:29
add(BackgroundJobPromise $promise)
Определения Queue.php:36
static getInstance()
Определения servicelocator.php:33
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
if(empty($signedUserToken)) $key
Определения quickway.php:257