• Модуль: imopenlines
  • Путь к файлу: ~/bitrix/modules/imopenlines/lib/queue.php
  • Класс: BitrixImOpenLinesQueue
  • Вызов: Queue::transferToNextSession
static function transferToNextSession($limitTime = 60, $limit = 0, $lineId = 0)
{
	$time = new ToolsTime;

	$configs = [];
	$chats = [];
	$configManager = new Config();
	$runSessionIds = [];

	$count = 0;
	$countIterationPull = 0;
	while ($time->getElapsedTime() <= $limitTime && (empty($limit) || $count < $limit))
	{
		$reasonReturn = Queue::REASON_DEFAULT;

		if ($countIterationPull > 10 && Loader::includeModule('pull'))
		{
			$countIterationPull = 0;

			PullEvent::send();
		}

		$filter = [
			'<=DATE_QUEUE' => new DateTime()
		];

		if (!empty($lineId) && is_numeric($lineId) && $lineId > 0)
		{
			$filter['=SESSION.CONFIG_ID'] = $lineId;
		}

		if (!empty($runSessionIds))
		{
			$filter['!=SESSION_ID'] = $runSessionIds;
		}

		$select = SessionTable::getSelectFieldsPerformance('SESSION');

		$select[] = 'REASON_RETURN';

		$res = SessionCheckTable::getList([
			'select' => $select,
			'filter' => $filter,
			'order' => [
				'DATE_QUEUE' => 'ASC',
				'SESSION_ID' => 'ASC',
			],
			'limit' => 1
		]);

		if ($row = $res->fetch())
		{
			$fields = [];

			if (!empty($row['REASON_RETURN']))
			{
				$reasonReturn = $row['REASON_RETURN'];
			}
			unset($row['REASON_RETURN']);

			foreach ($row as $key=>$value)
			{
				$key = str_replace('IMOPENLINES_MODEL_SESSION_CHECK_SESSION_', '', $key);
				$fields[$key] = $value;
			}

			$runSessionIds[$fields['ID']] = $fields['ID'];

			if (!isset($configs[$fields['CONFIG_ID']]))
			{
				$configs[$fields['CONFIG_ID']] = $configManager->get($fields['CONFIG_ID']);
			}
			if (!isset($chats[$fields['CHAT_ID']]))
			{
				$chats[$fields['CHAT_ID']] = new Chat($fields['CHAT_ID']);
			}

			self::sendMessageReturnedSession($reasonReturn, $fields);

			$session = new Session();
			$session->loadByArray($fields, $configs[$fields['CONFIG_ID']], $chats[$fields['CHAT_ID']]);

			$resultTransfer = $session->transferToNextInQueue(false);

			if ($resultTransfer == true)
			{
				$countIterationPull++;
			}
			$count++;
		}
		else
		{
			break;
		}
	}

	if (Loader::includeModule('pull') && $countIterationPull > 0)
	{
		PullEvent::send();
	}
}