12Loc::loadMessages(__FILE__);
16 protected const LOCK_TIME = 600;
18 public static function AddAgent(
36 WHERE NAME = '".
$DB->ForSql(
$name).
"'
37 AND USER_ID".($user_id?
" = ".(int)$user_id:
" IS NULL")
39 if (!($agent =
$z->Fetch()))
42 "MODULE_ID" => $module,
46 "AGENT_INTERVAL" => $interval,
47 "IS_PERIOD" => $period,
48 "USER_ID" => $user_id,
50 $next_exec = (string)$next_exec;
64 $e =
new CAdminException(
array(
66 "id" =>
"agent_exist",
68 ? Loc::getMessage(
"MAIN_AGENT_ERROR_EXIST_FOR_USER",
array(
'#AGENT#' =>
$name,
'#USER_ID#' => $user_id))
69 : Loc::getMessage(
"MAIN_AGENT_ERROR_EXIST_EXT",
array(
'#AGENT#' =>
$name))
88 if (CACHED_b_agent !==
false)
102 public static function RemoveAgent(
$name, $module =
"", $user_id =
false)
106 if (trim($module) ==
'')
107 $module =
"AND (MODULE_ID is null or ".$DB->Length(
"MODULE_ID").
" = 0)";
109 $module =
"AND MODULE_ID = '".$DB->ForSql($module, 50).
"'";
113 WHERE NAME = '".$DB->ForSql(
$name).
"'
115 AND USER_ID".($user_id ?
" = ".(int)$user_id :
" IS NULL");
120 public static function Delete($id)
128 $DB->Query(
"DELETE FROM b_agent WHERE ID = ".$id);
133 public static function RemoveModuleAgents($module)
139 $strSql =
"DELETE FROM b_agent WHERE MODULE_ID='".$DB->ForSql($module,255).
"'";
158 if(CAgent::CheckFields(
$arFields, $ign_name))
160 if(CACHED_b_agent !==
false)
164 $strSql =
"UPDATE b_agent SET ".$strUpdate.
" WHERE ID=".
$ID;
172 public static function GetById(
$ID)
174 return CAgent::GetList(Array(), Array(
"ID"=>intval(
$ID)));
177 public static function GetList($arOrder = Array(
"ID" =>
"DESC"),
$arFilter =
array())
181 $arSqlSearch =
array();
182 $arSqlOrder =
array();
186 "ACTIVE" =>
"A.ACTIVE",
187 "IS_PERIOD" =>
"A.IS_PERIOD",
189 "MODULE_ID" =>
"A.MODULE_ID",
190 "USER_ID" =>
"A.USER_ID",
191 "LAST_EXEC" =>
"A.LAST_EXEC",
192 "AGENT_INTERVAL" =>
"A.AGENT_INTERVAL",
193 "NEXT_EXEC" =>
"A.NEXT_EXEC",
194 "DATE_CHECK" =>
"A.DATE_CHECK",
204 if ((
string)
$val ==
'')
209 switch(strtoupper(
$key))
212 $arSqlSearch[] =
"A.ID=".(int)
$val;
215 $t_val = mb_strtoupper(
$val);
216 if($t_val ==
"Y" || $t_val ==
"N")
217 $arSqlSearch[] =
"A.ACTIVE='".$t_val.
"'";
220 $t_val = mb_strtoupper(
$val);
221 if($t_val==
"Y" || $t_val==
"N")
222 $arSqlSearch[] =
"A.IS_PERIOD='".$t_val.
"'";
225 $arSqlSearch[] =
"A.NAME LIKE '".$DB->ForSQLLike(
$val).
"'";
228 $arSqlSearch[] =
"A.NAME = '".$DB->ForSQL(
$val).
"'";
231 $arSqlSearch[] =
"A.MODULE_ID = '".$DB->ForSQL(
$val).
"'";
234 $arSqlSearch[] =
"A.USER_ID ".(intval(
$val)<=0?
"IS NULL":
"=".intval(
$val));
240 $date2 = mktime(0, 0, 0,
$arr[
"MM"],
$arr[
"DD"]+1,
$arr[
"YYYY"]);
241 $arSqlSearch[] =
"A.LAST_EXEC>=".$DB->CharToDateFunction(
$DB->ForSql(
$val),
"SHORT").
" AND A.LAST_EXEC<".
$DB->CharToDateFunction(
ConvertTimeStamp($date2),
"SHORT");
248 $date2 = mktime(0, 0, 0,
$arr[
"MM"],
$arr[
"DD"]+1,
$arr[
"YYYY"]);
249 $arSqlSearch[] =
"A.NEXT_EXEC>=".$DB->CharToDateFunction(
$DB->ForSql(
$val),
"SHORT").
" AND A.NEXT_EXEC<".
$DB->CharToDateFunction(
ConvertTimeStamp($date2),
"SHORT");
255 foreach($arOrder as $by =>
$order)
257 $by = mb_strtoupper($by);
259 if (isset($arOFields[$by]))
265 $arSqlOrder[] = $arOFields[$by].
" ".
$order;
269 $strSql =
"SELECT A.ID, A.MODULE_ID, A.USER_ID, B.LOGIN, B.NAME as USER_NAME, B.LAST_NAME, A.SORT, ".
270 "A.NAME, A.ACTIVE, A.RUNNING, ".
271 $DB->DateToCharFunction(
"A.LAST_EXEC").
" as LAST_EXEC, ".
272 $DB->DateToCharFunction(
"A.NEXT_EXEC").
" as NEXT_EXEC, ".
273 $DB->DateToCharFunction(
"A.DATE_CHECK").
" as DATE_CHECK, ".
274 "A.AGENT_INTERVAL, A.IS_PERIOD, A.RETRY_COUNT ".
275 "FROM b_agent A LEFT JOIN b_user B ON(A.USER_ID = B.ID)";
276 $strSql .= !empty($arSqlSearch) ?
" WHERE ".implode(
" AND ", $arSqlSearch) :
"";
277 $strSql .= !empty($arSqlOrder) ?
" ORDER BY ".implode(
", ", $arSqlOrder) :
"";
284 public static function CheckFields(
$arFields, $ign_name =
false)
291 $errMsg[] =
array(
"id" =>
"NAME",
"text" => Loc::getMessage(
"MAIN_AGENT_ERROR_NAME"));
297 || !
$DB->IsDate(
$arFields[
"NEXT_EXEC"],
false,
false,
"FULL")
301 $errMsg[] =
array(
"id" =>
"NEXT_EXEC",
"text" => Loc::getMessage(
"MAIN_AGENT_ERROR_NEXT_EXEC"));
305 array_key_exists(
"DATE_CHECK",
$arFields)
307 && !
$DB->IsDate(
$arFields[
"DATE_CHECK"],
false,
false,
"FULL")
310 $errMsg[] =
array(
"id" =>
"DATE_CHECK",
"text" => Loc::getMessage(
"MAIN_AGENT_ERROR_DATE_CHECK"));
316 && !
$DB->IsDate(
$arFields[
"LAST_EXEC"],
false,
false,
"FULL")
319 $errMsg[] =
array(
"id" =>
"LAST_EXEC",
"text" => Loc::getMessage(
"MAIN_AGENT_ERROR_LAST_EXEC"));
326 $e =
new CAdminException($errMsg);
338 protected static function OnCron()
340 if (COption::GetOptionString(
'main',
'agents_use_crontab',
'N') ==
'Y' || (defined(
'BX_CRONTAB_SUPPORT') && BX_CRONTAB_SUPPORT ===
true))
342 return (defined(
'BX_CRONTAB') &&
BX_CRONTAB ===
true);
347 public static function CheckAgents()
349 define(
"START_EXEC_AGENTS_1", microtime(
true));
351 define(
"BX_CHECK_AGENT_START",
true);
354 if((defined(
"NO_AGENT_CHECK") &&
NO_AGENT_CHECK===
true) || (defined(
"BX_CLUSTER_GROUP") && BX_CLUSTER_GROUP !== 1))
357 $res = CAgent::ExecuteAgents();
359 define(
"START_EXEC_AGENTS_2", microtime(
true));
364 public static function ExecuteAgents()
368 $cron = static::OnCron();
372 $str_crontab = ($cron ?
" AND IS_PERIOD='N' " :
" AND IS_PERIOD='Y' ");
380 $cache_id =
"agents".$str_crontab;
381 if (CACHED_b_agent !==
false &&
$CACHE_MANAGER->Read(CACHED_b_agent, $cache_id,
"agents"))
384 if (time() < $saved_time)
390 $connection = \Bitrix\Main\Application::getConnection();
398 AND NEXT_EXEC <= " . $helper->getCurrentDateTimeFunction() .
"
399 AND (DATE_CHECK IS NULL OR DATE_CHECK <= " . $helper->getCurrentDateTimeFunction() .
")
404 $db_result_agents =
$DB->Query($strSql);
405 if ($db_result_agents->Fetch())
414 if (CACHED_b_agent !==
false)
416 $rs =
$DB->Query(
"SELECT UNIX_TIMESTAMP(NEXT_EXEC)-UNIX_TIMESTAMP(" . $helper->getCurrentDateTimeFunction() .
") DATE_DIFF FROM b_agent WHERE ACTIVE = 'Y' " . $str_crontab .
" ORDER BY NEXT_EXEC LIMIT 1");
419 if (!
$ar ||
$ar[
"DATE_DIFF"] < 0)
421 elseif (
$ar[
"DATE_DIFF"] > CACHED_b_agent)
422 $date_diff = CACHED_b_agent;
424 $date_diff =
$ar[
"DATE_DIFF"];
438 "SELECT ID, NAME, AGENT_INTERVAL, IS_PERIOD, MODULE_ID, RETRY_COUNT ".
440 "WHERE ACTIVE = 'Y' ".
441 " AND NEXT_EXEC <= " . $helper->getCurrentDateTimeFunction() .
" ".
442 " AND (DATE_CHECK IS NULL OR DATE_CHECK <= " . $helper->getCurrentDateTimeFunction() .
") ".
444 " ORDER BY RUNNING ASC, SORT desc ";
447 $limit = ($cron ?
COption::GetOptionInt(
"main",
"agents_cron_limit", 1000) : COption::GetOptionInt(
"main",
"agents_limit", 100));
450 $strSql .=
' LIMIT ' . $limit;
453 $db_result_agents =
$DB->Query($strSql);
455 $agents_array =
array();
456 while ($db_result_agents_array = $db_result_agents->Fetch())
458 $agents_array[] = $db_result_agents_array;
459 $ids .= ($ids <>
''?
', ':
'').$db_result_agents_array[
"ID"];
463 $strSql =
"UPDATE b_agent SET DATE_CHECK = " . $helper->addSecondsToDateTime(self::LOCK_TIME) .
" WHERE ID IN (".$ids.
")";
470 $logFunction = (defined(
"BX_AGENTS_LOG_FUNCTION") && function_exists(BX_AGENTS_LOG_FUNCTION)? BX_AGENTS_LOG_FUNCTION :
false);
472 ignore_user_abort(
true);
475 foreach ($agents_array as $arAgent)
477 if (time() -
$startTime > self::LOCK_TIME - 30)
485 $logFunction($arAgent,
"start");
488 if ($arAgent[
"MODULE_ID"] <>
'' && $arAgent[
"MODULE_ID"]!=
"main")
490 if (!CModule::IncludeModule($arAgent[
"MODULE_ID"]))
495 $DB->Query(
"UPDATE b_agent SET RUNNING = 'Y', RETRY_COUNT = RETRY_COUNT+1 WHERE ID = ".$arAgent[
"ID"]);
498 $pPERIOD = $arAgent[
"AGENT_INTERVAL"];
508 $e = eval(
"\$eval_result=".$arAgent[
"NAME"]);
510 catch (Throwable $exception)
515 $exceptionHandler =
$application->getExceptionHandler();
516 $exceptionHandler->writeToLog(
new \Bitrix\Main\SystemException(
"Error in agent {$arAgent["NAME
"]}.", 0,
'', 0, $exception));
525 $logFunction($arAgent,
"finish", $eval_result, $e);
532 elseif ($eval_result ==
'')
534 $strSql =
"DELETE FROM b_agent WHERE ID = ".$arAgent[
"ID"];
538 if ($logFunction && function_exists(
'token_get_all'))
540 if (
count(token_get_all(
"<?php ".$eval_result)) < 3)
543 $logFunction($arAgent,
"not_callable", $eval_result, $e);
549 NAME = '".$DB->ForSQL($eval_result).
"',
550 LAST_EXEC = " . $helper->getCurrentDateTimeFunction() .
",
551 NEXT_EXEC = " . $helper->addSecondsToDateTime($pPERIOD, $arAgent[
"IS_PERIOD"]==
"Y"?
"NEXT_EXEC" :
null) .
",
555 WHERE ID = ".$arAgent[
"ID"];
564class CAgent
extends CAllAgent
static GetOptionInt($module_id, $name, $def="", $site=false)
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
ExecuteModuleEventEx($arEvent, $arParams=[])
ParseDateTime($datetime, $format=false)
ConvertTimeStamp($timestamp=false, $type="SHORT", $site=false, $bSearchInSitesOnly=false)
GetModuleEvents($MODULE_ID, $MESSAGE_ID, $bReturnArray=false)
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
if(empty($signedUserToken)) $key
</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."%"