403Webshell
Server IP : 80.87.202.40  /  Your IP : 216.73.216.169
Web Server : Apache
System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64
User : bitrix ( 600)
PHP Version : 8.2.27
Disable Function : NONE
MySQL : OFF |  cURL : ON |  WGET : ON |  Perl : ON |  Python : OFF |  Sudo : ON |  Pkexec : ON
Directory :  /home/bitrix/ext_www/cvetdv.ru/bitrix/modules/wbs24.ozonapinew/lib/Agents/

Upload File :
current_dir [ Writeable] document_root [ Writeable]

 

Command :


[ Back ]     

Current File : /home/bitrix/ext_www/cvetdv.ru/bitrix/modules/wbs24.ozonapinew/lib/Agents/CheckUp.php
<?php
namespace Wbs24\Ozonapinew\Agents;

use Bitrix\Main\Localization\Loc;
use Wbs24\Ozonapinew\{
    Main,
    Wrappers,
    Accounts,
    Error,
    Settings
};

class CheckUp
{
    use Accounts;
    // смещение проверки выполнения агентов в часах
    protected $checkUpDelay = 1;
    // время интервала запуска проверки агентов в часах
    protected $nextCheckUpInterval = 1;

    public function __construct($accountIndex = 1, $objects = [])
    {
        $this->main = $objects['Main'] ?? new Main();
        $this->moduleId = $this->main->getModuleId();
        $this->suffix = strtoupper($this->moduleId);
        $this->wrappers = new Wrappers($objects);
        $this->Error = $objects['Error'] ?? new Error();
        $this->Settings = $objects['Settings'] ?? new Settings();
        $this->setAccount($accountIndex);
        // последнаяя дата проверки агентов
        $this->lastDateCheckAgents = $this->wrappers->Option->get(
            $this->moduleId,
            'lastDateCheckAgents'
        );
    }

    // проверить агенты модуля
    function checkUp()
    {
        $startCheckUp = $this->checkTimeToRunCheckUp();
        if (!$startCheckUp) return;

        $moduleAgents = $this->getModuleAgents();

        // если нет ни одного созданного агента
        if (!$moduleAgents) {
            $this->sendErrors([
                'errorLevel' => 'NOTICE',
                'errorMessage' => Loc::getMessage($this->suffix.".ALL_AGENTS_NOT_CREATED"),
                'errorDescription' => Loc::getMessage($this->suffix.".ALL_AGENTS_NOT_CREATED"),
            ]);
            return;
        }

        // проверка агентов модуля
        foreach ($moduleAgents as $agent) {
            $agentId = $agent['ID'] ?? '';
            $agentName = $agent['NAME'] ?? '';
            $nextExec = $agent['NEXT_EXEC'] ?? '';
            $nextExecTime = new \DateTime($nextExec);
            $nextExecTime->modify('+'.$this->checkUpDelay.' hour');
            $currentTime = new \DateTime();

            if (!$this->verifyAgent($agentName)) continue;

            $siteInfo = $this->Settings->getSiteInfo();
            $agentEntity = 'http://'.$siteInfo['serverName'].'/bitrix/admin/agent_list.php?PAGEN_1=1&SIZEN_1=20&lang=ru&set_filter=Y&adm_filter_applied=0&find='.$agentId.'&find_type=id';

            // если текущая дата больше чем дата последнего выполнения агента + n часов
            if ($currentTime > $nextExecTime) {
                $this->sendErrors([
                    'errorLevel' => 'WARNING',
                    'errorMessage' =>
                        Loc::getMessage($this->suffix.".AGENT_FIRST_PART")
                        .$agentName
                        .Loc::getMessage($this->suffix.".AGENT_NOT_EXECUTE_LONG_TIME_FIRST_PART")
                        .$this->checkUpDelay
                        .Loc::getMessage($this->suffix.".AGENT_NOT_EXECUTE_LONG_TIME_SECOND_PART")
                    ,
                    'errorDescription' =>
                        Loc::getMessage($this->suffix.".AGENT_NOT_EXECUTE_LONG_TIME_DESCRIPTION")
                        .$this->checkUpDelay
                        .Loc::getMessage($this->suffix.".AGENT_NOT_EXECUTE_LONG_TIME_SECOND_PART")
                    ,
                    'entityId' => '<a href="'.$agentEntity.'">'.$agentId.'</a>'
                ]);
            }
        }
    }

    protected function verifyAgent($agentName)
    {
        $found = false;
        $verifiedAgents = [
            'getOrdersAgent',
            'ordersCurlExecAgent',
            'cleanOrdersStack',
            'statusesCurlExecAgent',
            'updatePricesAgent',
            'updateStocksAgent',
        ];

        foreach ($verifiedAgents as $agent) {
            if (strpos($agentName, $agent) !== false) {
                $found = true;
                break;
            }
        }

        return $found;
    }

    // получить агенты модуля
    protected function getModuleAgents()
    {
        $queryResult = $this->wrappers->CAgent->getList(
            $arOrder = ["ID" => "DESC"],
            $arFilter = [
                'MODULE_ID' => $this->moduleId,
            ]
        );

        $agents = [];
        while ($agent = $queryResult->fetch()) {
            $agents[] = $agent;
        }

        return $agents;
    }

    // проверить, не наступила ли дата проверки агентов
    protected function checkTimeToRunCheckUp()
    {
        if (!$this->lastDateCheckAgents) {
            // проставить новую дату последней проверки
            $this->wrappers->Option->set(
                $this->moduleId,
                'lastDateCheckAgents',
                time()
            );

            return true;
        }
        $startCheckUp = false;
        $currentTime = new \DateTime();

        $nextCheckUpDate = new \DateTime();
        $nextCheckUpDate->setTimestamp($this->lastDateCheckAgents);
        $nextCheckUpDate->modify('+'.$this->nextCheckUpInterval.' hour');

        if ($currentTime > $nextCheckUpDate) {
            $this->wrappers->Option->set(
                $this->moduleId,
                'lastDateCheckAgents',
                time()
            );
        }

        return $currentTime > $nextCheckUpDate;
    }

    // отправить ошибки
    protected function sendErrors($param)
    {
        [
            'errorLevel' => $errorLevel,
            'errorMessage' => $errorMessage,
            'errorDescription' => $errorDescription,
            'entityId' => $entityId,
        ] = $param;

        $siteInfo = $this->Settings->getSiteInfo();

        $this->Error->errorProcess([
            'siteName' => $siteInfo['siteNameHtml'],
            'dateTime' => date("Y-m-d H:i:s", time()),
            'errorLevel' => $errorLevel,
            'errorType' => 'INTERNAL',
            'errorCode' => 'AGENT_ERRORS',
            'errorMessage' => $errorMessage,
            'errorDescription' => $errorDescription,
            'accountId' => null,
            'apiUrl' => '',
            'entityId' => $entityId,
            'entityType' => 'agents',
            'requestBody' => '',
            'responseBody' => '',
            'trace' => ''
        ]);
    }
}

Youez - 2016 - github.com/yon3zu
LinuXploit