1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
SyncService.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Integration\HumanResources\Sync;
4
5use Bitrix\HumanResources\Contract\Repository\NodeRelationRepository;
6use Bitrix\HumanResources\Contract\Service\NodeMemberService;
7use Bitrix\HumanResources\Item\NodeMember;
8use Bitrix\HumanResources\Item\NodeRelation;
9use Bitrix\HumanResources\Service\Container;
10use Bitrix\HumanResources\Service\NodeRelationService;
11use Bitrix\HumanResources\Type\RelationEntityType;
12use Bitrix\Im\V2\Common\PeriodAgentTrait;
13use Bitrix\Im\V2\Integration\HumanResources\Sync\Item\EntityType;
14use Bitrix\Im\V2\Integration\HumanResources\Sync\Item\SyncDirection;
15use Bitrix\Im\V2\Integration\HumanResources\Sync\SyncProcessor\Base;
16use Bitrix\Im\V2\Result;
17use Bitrix\Main\Application;
18use Bitrix\Main\Event;
19use Bitrix\Main\Loader;
20
22{
23 use PeriodAgentTrait;
24
25 protected const AGENT_SHORT_PERIOD = 5;
26 protected const AGENT_LONG_PERIOD = 300;
27
28 protected NodeMemberService $memberService;
29 protected NodeRelationService $relationService;
30 protected NodeRelationRepository $relationRepository;
33
34 public function __construct(
36 ?NodeMemberService $memberService = null,
37 ?NodeRelationService $nodeRelationService = null,
38 ?NodeRelationRepository $relationRepository = null
39 )
40 {
41 Loader::requireModule('humanresources');
42
43 $this->entityType = $entityType;
44 $this->syncProcessor = Base::getInstance($entityType);
45 $this->memberService = $memberService ?? Container::getNodeMemberService();
46 $this->relationService = $nodeRelationService ?? Container::getNodeRelationService();
47 $this->relationRepository = $relationRepository ?? Container::getNodeRelationRepository();
48 }
49
50 protected static function getAgentNameByEntityType(Item\EntityType $entityType): string
51 {
52 $agentNameByType = [
53 Item\EntityType::CHAT->value => '\Bitrix\Im\V2\Integration\HumanResources\Sync\SyncService::syncRelationAgent();',
54 Item\EntityType::USER->value => '\Bitrix\Im\V2\Integration\HumanResources\Sync\SyncService::syncMemberAgent();',
55 ];
56
57 return $agentNameByType[$entityType->value];
58 }
59
60 public static function onMemberAdded(Event $event): void
61 {
63 $member = $event->getParameter('member');
64
65 (new static(EntityType::USER))
66 ->startSync(Item\SyncInfo::createFromNodeMember($member, Item\SyncDirection::ADD))
67 ;
68 }
69
70 public static function onMemberDeleted(Event $event): void
71 {
73 $member = $event->getParameter('member');
74
75 (new static(EntityType::USER))
76 ->startSync(Item\SyncInfo::createFromNodeMember($member, Item\SyncDirection::DELETE))
77 ;
78 }
79
80 public static function onMemberUpdated(Event $event): void
81 {
83 $member = $event->getParameter('member');
85 $previousMember = $event->getParameter('previousMember');
86
87 if ($previousMember === null)
88 {
89 return;
90 }
91
92 if ($member->nodeId === $previousMember->nodeId)
93 {
94 return;
95 }
96
97 (new static(EntityType::USER))
98 ->startSync(Item\SyncInfo::createFromNodeMember($member, Item\SyncDirection::ADD))
99 ;
100 (new static(EntityType::USER))
101 ->startSync(Item\SyncInfo::createFromNodeMember($previousMember, Item\SyncDirection::DELETE))
102 ;
103 }
104
105 public static function onRelationAdded(Event $event): void
106 {
108 $relation = $event->getParameter('relation');
109 if (
110 $relation->entityType !== RelationEntityType::CHAT
111 || $relation->node === null
112 )
113 {
114 return;
115 }
116
117 (new static(EntityType::CHAT))
118 ->startSync(Item\SyncInfo::createFromNodeRelation($relation, Item\SyncDirection::ADD))
119 ;
120 }
121
122 public static function onRelationDeleted(Event $event): void
123 {
125 $relation = $event->getParameter('relation');
126 if (
127 $relation->entityType !== RelationEntityType::CHAT
128 || $relation->node === null
129 )
130 {
131 return;
132 }
133
134 (new static(EntityType::CHAT))
135 ->startSync(Item\SyncInfo::createFromNodeRelation($relation, Item\SyncDirection::DELETE))
136 ;
137 }
138
139 public static function syncRelationAgent(): string
140 {
141 if (!Loader::includeModule('humanresources'))
142 {
143 return self::getAgentNameByEntityType(Item\EntityType::CHAT);
144 }
145
146 (new static(EntityType::CHAT))->sync();
147
148 return self::getAgentNameByEntityType(Item\EntityType::CHAT);
149 }
150
151 public static function syncMemberAgent(): string
152 {
153 if (!Loader::includeModule('humanresources'))
154 {
155 return self::getAgentNameByEntityType(Item\EntityType::USER);
156 }
157
158 (new static(EntityType::USER))->sync();
159
160 return self::getAgentNameByEntityType(Item\EntityType::USER);
161 }
162
163 protected function startSync(Item\SyncInfo $syncInfo): Result
164 {
165 $itemResult = $this->syncProcessor->getOrCreateWithLock($syncInfo);
166 if ($itemResult->skip())
167 {
168 $this->determinePeriod(false);
169
170 return new Result();
171 }
172
173 $item = $itemResult->getResult();
174 try
175 {
176 $firstIterationResult = $this->syncProcessor->makeIteration($item);
177 if (!$firstIterationResult->hasMore())
178 {
179 $this->syncProcessor->finalizeSync($item);
180 }
181 $this->determinePeriod(false);
182
183 return $firstIterationResult;
184 }
185 catch (\Throwable $exception)
186 {
187 $item->setErrorStatus();
188
189 return new Result();
190 }
191 finally
192 {
193 $item->unlock();
194 }
195 }
196
197 protected function sync(): Result
198 {
199 $result = $this->syncNext();
200 $this->determinePeriod(true);
201
202 return $result;
203 }
204
205 protected function syncNext(): Result
206 {
207 $result = new Result();
208 $syncInfo = $this->syncProcessor->dequeue();
209
210 if ($syncInfo === null)
211 {
212 return $result;
213 }
214
215 $item = $this->syncProcessor->tryGetWithLock($syncInfo);
216 if ($item === null)
217 {
218 return $result;
219 }
220 try
221 {
222 $iterationResult = $this->syncProcessor->makeIteration($item);
223 if ($iterationResult->hasMore())
224 {
225 $item->unlock();
226 }
227 else
228 {
229 $this->syncProcessor->finalizeSync($item);
230 }
231 }
232 catch (\Throwable $exception)
233 {
234 $item->unlock();
235 $item->setErrorStatus();
236 }
237
238 return $result;
239 }
240
241 protected static function isAgentPeriodShort(int $newPeriod): bool
242 {
243 return $newPeriod === self::AGENT_SHORT_PERIOD;
244 }
245
246 protected function getPeriodGetter(): callable
247 {
248 return fn () => $this->syncProcessor->hasItemsInQueue() ? self::AGENT_SHORT_PERIOD : self::AGENT_LONG_PERIOD;
249 }
250
251 protected function determinePeriod(bool $fromAgent): void
252 {
253 self::setPeriodByName($fromAgent, self::getAgentNameByEntityType($this->entityType), $this->getPeriodGetter());
254 }
255}
static getInstance(EntityType $entityType)
Определения Base.php:49
startSync(Item\SyncInfo $syncInfo)
Определения SyncService.php:163
static getAgentNameByEntityType(Item\EntityType $entityType)
Определения SyncService.php:50
static isAgentPeriodShort(int $newPeriod)
Определения SyncService.php:241
NodeRelationRepository $relationRepository
Определения SyncService.php:30
__construct(EntityType $entityType, ?NodeMemberService $memberService=null, ?NodeRelationService $nodeRelationService=null, ?NodeRelationRepository $relationRepository=null)
Определения SyncService.php:34
Определения event.php:5
$result
Определения get_property_values.php:14
$event
Определения prolog_after.php:141