1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
baseagent.php
См. документацию.
1<?php
2
3namespace Bitrix\Calendar\Core\Queue\Agent;
4
5use Bitrix\Calendar\Core\Queue\Interfaces;
6use Bitrix\Calendar\Core\Queue\Interfaces\Processor;
7use Bitrix\Calendar\Internals\Log\Logger;
8use CAgent;
9use Exception;
10
11abstract class BaseAgent
12{
13 private const TIME_LIMIT = 10;
14 private static ?Logger $logger = null;
15
16 protected bool $isEscalated = false;
17
21 public static function runAgent(): string
22 {
23 try
24 {
25 $runner = new static();
26 $runner->run();
27 self::modifyAgent($runner);
28 }
29 catch(Exception $exception)
30 {
31 if (is_null(self::$logger))
32 {
33 self::$logger = new Logger();
34 }
35
36 self::$logger->log($exception);
37 }
38
39 return static::getAgentName();
40 }
41
45 protected static function getAgentName(): string
46 {
47 return static::class . "::runAgent();";
48 }
49
53 protected static function getModule(): string
54 {
55 return 'calendar';
56 }
57
63 private static function modifyAgent(BaseAgent $runner)
64 {
65 $agent = CAgent::getList(
66 [],
67 [
68 'MODULE_ID' => self::getModule(),
69 '=NAME' =>self::getAgentName(),
70 ]
71 )->Fetch();
72 if ($agent)
73 {
74 $interval = $runner->isEscalated
75 ? $runner->getEscalatedInterval()
76 : $runner->getInterval()
77 ;
78 if ((int)$agent['AGENT_INTERVAL'] !== $interval)
79 {
80 CAgent::Update($agent['ID'],['AGENT_INTERVAL' => $interval]);
81 }
82 }
83 }
84
88 protected function run()
89 {
90 $consumer = $this->getConsumer();
91 $processor = $this->getProcessor();
92
93 $startTime = time();
94 $this->deescalateMe();
95 while ($message = $consumer->receive())
96 {
97 $result = $processor->process($message);
98
100 {
101 $consumer->acknowledge($message);
102 }
104 {
105 $consumer->reject($message);
106 }
107
108 $this->escalateMe();
109 if ((time() - $startTime) > $this->getTimeLimit())
110 {
111 break;
112 }
113 }
114 }
115
119 protected function escalateMe(): void
120 {
121 $this->isEscalated = true;
122 }
123
127 protected function deescalateMe(): void
128 {
129 $this->isEscalated = false;
130 }
131
135 protected function getInterval(): int
136 {
137 return 3600;
138 }
139
143 protected function getEscalatedInterval(): int
144 {
145 return 60;
146 }
147
151 protected function getTimeLimit(): int
152 {
153 return self::TIME_LIMIT;
154 }
155
159 abstract protected function getConsumer(): Interfaces\Consumer;
160
164 abstract protected function getProcessor(): Interfaces\Processor;
165}
$startTime
Определения sync.php:69
$result
Определения get_property_values.php:14
$message
Определения payment.php:8