Пример #1
0
 function CheckAgents()
 {
     $err_mess = CAgent::err_mess() . "<br>Function: CheckAgents<br>Line: ";
     global $DB, $DOCUMENT_ROOT;
     $agents_use_crontab = COption::GetOptionString("main", "agents_use_crontab", "N");
     $str_crontab = "";
     if ($agents_use_crontab == "Y" || defined("BX_CRONTAB_SUPPORT") && BX_CRONTAB_SUPPORT === true) {
         $str_crontab = defined("BX_CRONTAB") && BX_CRONTAB == true ? " AND IS_PERIOD='N' " : " AND IS_PERIOD='Y' ";
     }
     if (CACHED_b_agent !== false) {
         if (file_exists($_SERVER["DOCUMENT_ROOT"] . "/" . BX_ROOT . "/managed_cache/" . $DB->type . "/b_agent")) {
             include $_SERVER["DOCUMENT_ROOT"] . "/" . BX_ROOT . "/managed_cache/" . $DB->type . "/b_agent";
             if (time() < $saved_time) {
                 return "";
             }
         }
     }
     $DB->StartTransaction();
     $DB->Query("SET LOCK_TIMEOUT 0", true, $err_mess . __LINE__);
     $strSql = "\n\t\t\tSELECT\n\t\t\t\tID, \n\t\t\t\tNAME, \n\t\t\t\tAGENT_INTERVAL, \n\t\t\t\tIS_PERIOD, \n\t\t\t\tMODULE_ID \n\t\t\tFROM \n\t\t\t\tb_agent \n\t\t\tWITH (TABLOCKX)\n\t\t\tWHERE \n\t\t\t\t\tACTIVE = 'Y'\n\t\t\t\tand NEXT_EXEC <= getdate()\n\t\t\t\t{$str_crontab}\n\t\t\tORDER BY \n\t\t\t\tSORT desc\n\t\t\t";
     $rs = $DB->Query($strSql, true, $err_mess . __LINE__);
     $arAgents = array();
     $ignore_abort = false;
     $i = 0;
     while (is_object($rs) && ($arAgent = $rs->Fetch())) {
         $i++;
         if (!$ignore_abort) {
             @set_time_limit(0);
             ignore_user_abort(true);
             $ignore_abort = true;
         }
         if (strlen($arAgent["MODULE_ID"]) > 0 && $arAgent["MODULE_ID"] != "main") {
             if (!CModule::IncludeModule($arAgent["MODULE_ID"])) {
                 continue;
             }
         }
         //эти переменные могут измениться в вызываемой функции
         $pPERIOD = $arAgent["AGENT_INTERVAL"];
         $eval_result = "";
         eval("\$eval_result=" . $arAgent["NAME"]);
         if (strlen($eval_result) <= 0) {
             $strSql = "\n\t\t\t\t\tDELETE FROM b_agent WHERE ID = " . $arAgent["ID"];
         } else {
             if ($arAgent["IS_PERIOD"] == "Y") {
                 $strSql = "\n\t\t\t\t\t\tUPDATE b_agent SET \n\t\t\t\t\t\t\tNAME = '" . $DB->ForSql($eval_result) . "', \n\t\t\t\t\t\t\tLAST_EXEC = getdate(), \n\t\t\t\t\t\t\tNEXT_EXEC = dateadd(second, " . intval($pPERIOD) . ", NEXT_EXEC)\n\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\tID = " . $arAgent["ID"];
             } else {
                 $strSql = "\n\t\t\t\t\t\tUPDATE b_agent SET \n\t\t\t\t\t\t\tNAME = '" . $DB->ForSql($eval_result) . "', \n\t\t\t\t\t\t\tLAST_EXEC = getdate(), \n\t\t\t\t\t\t\tNEXT_EXEC = dateadd(second, " . intval($pPERIOD) . ", getdate())\n\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\tID = " . $arAgent["ID"];
             }
         }
         $DB->Query($strSql, true, $err_mess . __LINE__);
     }
     $DB->Query("SET LOCK_TIMEOUT -1", true, $err_mess . __LINE__);
     $DB->Commit();
     if ($i === 0 && CACHED_b_agent !== false) {
         $fp = @fopen($_SERVER["DOCUMENT_ROOT"] . "/" . BX_ROOT . "/managed_cache/" . $DB->type . "/b_agent", "w");
         if ($fp) {
             $rs = $DB->Query("SELECT DATEDIFF(s, GETDATE(), MIN(NEXT_EXEC)) DATE_DIFF FROM b_agent WHERE ACTIVE='Y'");
             $ar = $rs->Fetch();
             if (!$ar || $ar["DATE_DIFF"] < 0) {
                 $date_diff = 0;
             } elseif ($ar["DATE_DIFF"] > CACHED_b_agent) {
                 $date_diff = CACHED_b_agent;
             } else {
                 $date_diff = $ar["DATE_DIFF"];
             }
             fputs($fp, "<?php \$saved_time=" . intval(time() + $date_diff) . ";?>");
             fclose($fp);
         }
     }
 }