Your IP : 3.135.182.115
<?php
namespace Bitrix\MessageService;
use Bitrix\Main\Application;
use Bitrix\Main\Config;
use Bitrix\Main\Error;
use Bitrix\Main\Type;
use Bitrix\MessageService\Sender\Result\SendMessage;
use Bitrix\MessageService\Sender\SmsManager;
use Bitrix\Main\Localization\Loc;
Loc::loadMessages(__FILE__);
class Queue
{
private static function hasMessages()
{
$connection = Application::getConnection();
$queryString = 'SELECT 1 FROM b_messageservice_message WHERE SUCCESS_EXEC=\'N\' LIMIT 1';
return is_array($connection->query($queryString)->fetch());
}
/**
* @return string
*/
public static function run()
{
if (
defined('DisableMessageServiceCheck') && DisableMessageServiceCheck === true
|| (
!defined('DisableMessageServiceCheck')
&& defined("DisableEventsCheck")
&& DisableEventsCheck === true
)
)
{
return null;
}
if (!static::hasMessages())
{
return "";
}
return static::sendMessages();
}
/**
* @return string
* @throws \Bitrix\Main\ArgumentException
* @throws \Bitrix\Main\ArgumentNullException
* @throws \Bitrix\Main\ArgumentTypeException
*/
public static function sendMessages()
{
if(defined("BX_FORK_AGENTS_AND_EVENTS_FUNCTION"))
{
if(\CMain::forkActions(array(get_called_class(), "sendMessages")))
return "";
}
$connection = Application::getConnection();
$lockTag = \CMain::getServerUniqID().'_b_messageservice_message';
$lockDb = $connection->query("SELECT GET_LOCK('".$lockTag."', 0) as L");
$lockRow = $lockDb->fetch();
if($lockRow["L"]=="0")
return "";
$limit = abs((int)Config\Option::get("messageservice", "queue_limit", 5));
if (!$limit)
{
$limit = 5;
}
$strSql = "SELECT ID ,TYPE, SENDER_ID, AUTHOR_ID,
MESSAGE_FROM, MESSAGE_TO, MESSAGE_HEADERS, MESSAGE_BODY
FROM b_messageservice_message
WHERE SUCCESS_EXEC = 'N'
ORDER BY ID
LIMIT ".$limit;
$messagesResult = $connection->query($strSql);
$notifyUpdateMessages = array();
if($messagesResult)
{
while($message = $messagesResult->fetch())
{
$message['MESSAGE_HEADERS'] = unserialize($message['MESSAGE_HEADERS']);
$toUpdate = array('SUCCESS_EXEC' => "E", 'DATE_EXEC' => new Type\DateTime);
try
{
$result = static::sendMessage($message);
if ($result->isSuccess())
{
$toUpdate['SUCCESS_EXEC'] = 'Y';
if ($result->getExternalId() !== null)
{
$toUpdate['EXTERNAL_ID'] = $result->getExternalId();
}
if ($result->getStatus() !== null)
{
$toUpdate['STATUS_ID'] = $result->getStatus();
}
}
else
{
$errors = $result->getErrorMessages();
$toUpdate['EXEC_ERROR'] = implode(PHP_EOL, $errors);
$toUpdate['STATUS_ID'] = MessageStatus::ERROR;
}
Internal\Entity\MessageTable::update($message["ID"], $toUpdate);
$toUpdate['ID'] = $message['ID'];
$toUpdate['DATE_EXEC'] = (string)$toUpdate['DATE_EXEC'];
$notifyUpdateMessages[] = $toUpdate;
}
catch (\Exception $e)
{
$application = \Bitrix\Main\Application::getInstance();
$exceptionHandler = $application->getExceptionHandler();
$exceptionHandler->writeToLog($e);
$toUpdate['EXEC_ERROR'] = $e->getMessage();
$toUpdate['STATUS_ID'] = MessageStatus::EXCEPTION;
Internal\Entity\MessageTable::update($message["ID"], $toUpdate);
$toUpdate['ID'] = $message["ID"];
$toUpdate['DATE_EXEC'] = (string)$toUpdate['DATE_EXEC'];
unset($toUpdate['EXEC_ERROR']);
$notifyUpdateMessages[] = $toUpdate;
break;
}
}
}
$connection->query("SELECT RELEASE_LOCK('".$lockTag."')");
if ($notifyUpdateMessages)
{
Integration\Pull::onMessagesUpdate($notifyUpdateMessages);
}
return null;
}
/**
* @param array $messageFields
* @return SendMessage
*/
private static function sendMessage(array $messageFields)
{
$type = $messageFields['TYPE'];
if ($type === MessageType::SMS)
{
$sender = SmsManager::getSenderById($messageFields['SENDER_ID']);
if (!$sender)
{
$result = new SendMessage();
$result->addError(new Error(Loc::getMessage("MESSAGESERVICE_QUEUE_SENDER_NOT_FOUND")));
return $result;
}
return $sender->sendMessage($messageFields);
}
$result = new SendMessage();
$result->addError(new Error(Loc::getMessage("MESSAGESERVICE_QUEUE_MESSAGE_TYPE_ERROR")));
return $result;
}
/**
* @return string
* @throws \Bitrix\Main\ArgumentNullException
*/
public static function cleanUpAgent()
{
$period = abs(intval(Config\Option::get("messageservice", "clean_up_period", 14)));
$periodInSeconds = $period * 24 * 3600;
if ($periodInSeconds > 0)
{
$connection = \Bitrix\Main\Application::getConnection();
$datetime = $connection->getSqlHelper()->addSecondsToDateTime('-' . $periodInSeconds);
$strSql = "DELETE FROM b_messageservice_message WHERE DATE_EXEC <= " . $datetime ;
$connection->query($strSql);
}
return "\Bitrix\MessageService\Queue::cleanUpAgent();";
}
}