1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
eventoffline.php
См. документацию.
1<?php
2namespace Bitrix\Rest;
3
4use Bitrix\Main;
5use Bitrix\Main\Data\Cache;
6use Bitrix\Rest\Exceptions\ArgumentTypeException;
7
38
39class EventOfflineTable extends Main\Entity\DataManager
40{
41 const PROCESS_ID_LIFETIME = 2952000; // 30 days
42 private const OFFLINE_EVENT_DEFAULT_TIMEOUT = 1;
43 private const OFFLINE_EVENT_CACHE_PREFIX = 'OFFLINE_EVENT_TIMEOUT';
44
45 private static $isSendOfflineEvent = false;
46
52 public static function getTableName()
53 {
54 return 'b_rest_event_offline';
55 }
56
62 public static function getMap()
63 {
64 return array(
65 'ID' => array(
66 'data_type' => 'integer',
67 'primary' => true,
68 'autocomplete' => true,
69 ),
70 'TIMESTAMP_X' => array(
71 'data_type' => 'datetime',
72 ),
73 'MESSAGE_ID' => array(
74 'data_type' => 'string',
75 'required' => true,
76 ),
77 'APP_ID' => array(
78 'data_type' => 'integer',
79 'required' => true,
80 ),
81 'EVENT_NAME' => array(
82 'data_type' => 'string',
83 'required' => true,
84 ),
85 'EVENT_DATA' => array(
86 'data_type' => 'text',
87 'serialized' => true,
88 ),
89 'EVENT_ADDITIONAL' => array(
90 'data_type' => 'text',
91 'serialized' => true,
92 ),
93 'PROCESS_ID' => array(
94 'data_type' => 'string',
95 'default_value' => '',
96 ),
97 'CONNECTOR_ID' => array(
98 'data_type' => 'string',
99 'default_value' => '',
100 ),
101 'ERROR' => array(
102 'data_type' => 'integer',
103 'default_value' => 0,
104 ),
105 );
106 }
107
108 public static function cleanProcessAgent()
109 {
111
112 $tableName = static::getTableName();
113 $dateTime = $connection->getSqlHelper()->addSecondsToDateTime('-' . static::PROCESS_ID_LIFETIME);
114
115 $sql = "DELETE FROM {$tableName} WHERE PROCESS_ID<>'' AND TIMESTAMP_X<{$dateTime}";
116
117 $connection->query($sql);
118
119 return "\\Bitrix\\Rest\\EventOfflineTable::cleanProcessAgent();";
120 }
121
122 public static function callEvent($fields)
123 {
124 if(!isset($fields['CONNECTOR_ID']))
125 {
126 $fields['CONNECTOR_ID'] = '';
127 }
128
129 $addFields = array(
130 'TIMESTAMP_X' => new Main\Type\DateTime(),
131 'MESSAGE_ID' => static::getMessageId($fields),
132 'APP_ID' => $fields['APP_ID'],
133 'EVENT_NAME' => $fields['EVENT_NAME'],
134 'EVENT_DATA' => serialize($fields['EVENT_DATA']),
135 'EVENT_ADDITIONAL' => serialize($fields['EVENT_ADDITIONAL']),
136 'CONNECTOR_ID' => $fields['CONNECTOR_ID'],
137 );
138
139 $updateFields = array(
140 'TIMESTAMP_X' => new Main\Type\DateTime(),
141 'EVENT_DATA' => serialize($fields['EVENT_DATA']),
142 'EVENT_ADDITIONAL' => serialize($fields['EVENT_ADDITIONAL']),
143 );
144
145 if(array_key_exists('ERROR', $fields))
146 {
147 $addFields['ERROR'] = intval($fields['ERROR']) > 0 ? 1 : 0;
148 $updateFields['ERROR'] = intval($fields['ERROR']) > 0 ? 1 : 0;
149 }
150
152 $queries = $connection->getSqlHelper()->prepareMerge(
153 static::getTableName(),
154 array('MESSAGE_ID', 'APP_ID', 'CONNECTOR_ID', 'PROCESS_ID'),
155 $addFields,
156 $updateFields
157 );
158
159 foreach($queries as $query)
160 {
161 $connection->queryExecute($query);
162 }
163 }
164
165 public static function markEvents($filter, $order, $limit): string
166 {
167 $processId = static::getProcessId();
168
169 $limit = intval($limit);
170 $query = new EventOfflineQuery(static::getEntity());
171 $query->setOrder($order);
172 $query->setLimit($limit);
173
174 if (is_array($filter))
175 {
176 foreach ($filter as $key => $value)
177 {
178 $matches = [];
179 if (preg_match('/^([\W]{1,2})(.+)/', $key, $matches) && $matches[0] === $key)
180 {
181 if (
182 !is_string($matches[2])
183 || !is_string($matches[1])
184 )
185 {
186 throw new ArgumentTypeException('FILTER_KEYS', 'string');
187 }
188 if (is_array($value) || is_object($value))
189 {
190 throw new ArgumentTypeException($key);
191 }
192 $query->where(
193 $matches[2],
194 $matches[1],
195 $value
196 );
197 }
198 else
199 {
200 if (!is_string($key))
201 {
202 throw new ArgumentTypeException('FILTER_KEYS', 'string');
203 }
204 if (is_array($value) || is_object($value))
205 {
206 throw new ArgumentTypeException($key);
207 }
208 $query->where(
209 $key,
210 $value
211 );
212 }
213 }
214 }
215
216 $query->mark($processId);
217
218 return $processId;
219 }
220
221 public static function clearEvents($processId, $appId, $connectorId, $listIds = false)
222 {
224
225 $tableName = static::getTableName();
226 $processId = $connection->getSqlHelper()->forSql($processId);
227 $appId = intval($appId);
228 $connectorId = $connection->getSqlHelper()->forSql($connectorId);
229
230 $sql = "DELETE FROM {$tableName} WHERE PROCESS_ID='{$processId}' AND APP_ID='{$appId}' AND CONNECTOR_ID='{$connectorId}'";
231
232 if($listIds !== false)
233 {
234 array_map('intval', $listIds);
235 $sql .= " AND ID IN ('".implode("', '", $listIds)."')";
236 }
237
238 $connection->query($sql);
239 }
240
241 public static function clearEventsByMessageId($processId, $appId, $connectorId, $listIds = false)
242 {
244 $helper = $connection->getSqlHelper();
245
246 $tableName = static::getTableName();
247 $processId = $connection->getSqlHelper()->forSql($processId);
248 $appId = intval($appId);
249 $connectorId = $connection->getSqlHelper()->forSql($connectorId);
250
251 $sql = "DELETE FROM {$tableName} WHERE PROCESS_ID='{$processId}' AND APP_ID='{$appId}' AND CONNECTOR_ID='{$connectorId}'";
252
253 if($listIds !== false)
254 {
255 foreach($listIds as $key => $id)
256 {
257 $listIds[$key] = $helper->forSql($id);
258 }
259
260 $sql .= " AND MESSAGE_ID IN ('".implode("', '", $listIds)."')";
261 }
262
263 $connection->query($sql);
264 }
265
266 public static function markError($processId, $appId, $connectorId, array $listIds)
267 {
268 if(count($listIds) > 0)
269 {
271 $helper = $connection->getSqlHelper();
272
273 foreach($listIds as $key => $id)
274 {
275 $listIds[$key] = $helper->forSql($id);
276 }
277
278 $queryWhere = array(
279 "APP_ID='".intval($appId)."'",
280 "CONNECTOR_ID='".$helper->forSql($connectorId)."'",
281 "MESSAGE_ID IN ('".implode("', '", $listIds)."')",
282 );
283
284 $sqlTable = static::getTableName();
285 $sqlWhere = implode(" AND ", $queryWhere);
286 $sqlProcessId = $helper->forSql($processId);
287
288 $sql = array();
289 $sql[] = "DELETE FROM {$sqlTable} WHERE {$sqlWhere} AND ERROR=0 AND PROCESS_ID <> '{$sqlProcessId}'";
290 $sql[] = "UPDATE {$sqlTable} SET ERROR=1, PROCESS_ID=IF(PROCESS_ID='{$sqlProcessId}', '', 'fake_process_id') WHERE {$sqlWhere} AND ERROR=0 ORDER BY PROCESS_ID ASC";
291 $sql[] = "DELETE FROM {$sqlTable} WHERE {$sqlWhere} AND PROCESS_ID='fake_process_id'";
292
293 foreach($sql as $query)
294 {
295 $connection->query($query);
296 }
297 }
298 }
299
300 protected static function getProcessId()
301 {
303 }
304
305 protected static function getMessageId($fields)
306 {
307 return isset($fields['MESSAGE_ID']) ? $fields['MESSAGE_ID'] : md5($fields['EVENT_NAME'].'|'.Main\Web\Json::encode($fields['EVENT_DATA']));
308 }
309
310 public static function checkSendTime($id, $timeout = null) : bool
311 {
312 $result = false;
313 $timeout = !is_null($timeout) ? (int)$timeout : static::OFFLINE_EVENT_DEFAULT_TIMEOUT;
314
315 if ($timeout > 0)
316 {
317 $key = static::OFFLINE_EVENT_CACHE_PREFIX. '|' . $id . '|'. $timeout;
318 $cache = Cache::createInstance();
319 if ($cache->initCache($timeout, $key))
320 {
321 $result = false;
322 }
323 elseif ($cache->startDataCache())
324 {
325 $result = true;
326 $data = 1;
327 $cache->endDataCache($data);
328 }
329 }
330 elseif (static::$isSendOfflineEvent === false)
331 {
332 static::$isSendOfflineEvent = true;
333 $result = true;
334 }
335
336 return $result;
337 }
338
339 public static function prepareOfflineEvent($params, $handler)
340 {
341 $data = reset($params);
342 if (!is_array($data['APP_LIST']) || !in_array((int) $handler['APP_ID'], $data['APP_LIST'], true))
343 {
344 throw new RestException('Wrong application.');
345 }
346
347 $timeout = $handler['OPTIONS']['minTimeout'] ?? null;
348 if (!static::checkSendTime($handler['ID'], $timeout))
349 {
350 throw new RestException('Time is not up.');
351 }
352
353 return null;
354 }
355
363 public static function deleteByApp(mixed $appId): Main\DB\Result
364 {
366
367 return $connection->query("DELETE FROM ".static::getTableName()." WHERE APP_ID='".$appId."'");
368 }
369}
$connection
Определения actionsdefinitions.php:38
static getConnection($name="")
Определения application.php:638
Определения result.php:20
static getString($length, $caseSensitive=false)
Определения random.php:76
static clearEvents($processId, $appId, $connectorId, $listIds=false)
Определения eventoffline.php:221
static checkSendTime($id, $timeout=null)
Определения eventoffline.php:310
static getMap()
Определения eventoffline.php:62
static cleanProcessAgent()
Определения eventoffline.php:108
const PROCESS_ID_LIFETIME
Определения eventoffline.php:41
static clearEventsByMessageId($processId, $appId, $connectorId, $listIds=false)
Определения eventoffline.php:241
static getProcessId()
Определения eventoffline.php:300
static prepareOfflineEvent($params, $handler)
Определения eventoffline.php:339
static callEvent($fields)
Определения eventoffline.php:122
static deleteByApp(mixed $appId)
Определения eventoffline.php:363
static getMessageId($fields)
Определения eventoffline.php:305
static getTableName()
Определения eventoffline.php:52
static markError($processId, $appId, $connectorId, array $listIds)
Определения eventoffline.php:266
static markEvents($filter, $order, $limit)
Определения eventoffline.php:165
$data['IS_AVAILABLE']
Определения .description.php:13
</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
$query
Определения get_search.php:11
$filter
Определения iblock_catalog_list.php:54
Определения arrayresult.php:2
Определения collection.php:2
Определения cookie.php:3
$order
Определения payment.php:8
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$matches
Определения index.php:22
$fields
Определения yandex_run.php:501