• Модуль: tasks
  • Путь к файлу: ~/bitrix/modules/tasks/lib/util/disposableaction.php
  • Класс: BitrixTasksUtilDisposableAction
  • Вызов: DisposableAction::restoreReplicationAgents
static function restoreReplicationAgents()
{
	global $DB;

	// get all tasks that has replication = on
	$tasks = array();
	$res = $DB->query("
		select TT.ID as TT_ID, TT.REPLICATE, TT.REPLICATE_PARAMS as REPLICATE_PARAMS, TT.TPARAM_REPLICATION_COUNT as TT_TPARAM_REPLICATION_COUNT, TT.CREATED_BY as TT_CREATED_BY from b_tasks_template TT
		  	where TT.REPLICATE = 'Y'
	");
	while($item = $res->fetch())
	{
		$tasks[$item['TT_ID']] = $item;
	}

	// get all agents of name "CTasks::RepeatTaskByTemplateId(nnnn"
	$agents = array();
	$res = $DB->query("select NAME from b_agent where MODULE_ID = 'tasks' and ACTIVE = 'Y'");
	while($item = $res->fetch())
	{
		$found = array();

		if(preg_match('#^CTasks::RepeatTaskByTemplateId((d+)#', $item['NAME'], $found))
		{
			$templateId = intval($found[1]);
			if($templateId)
			{
				$agents[$templateId] = $item;
			}
		}
	}

	// for each task check what we must do with the corresponding agent
	foreach($tasks as $taskId => $taskData)
	{
		$name = 'CTasks::RepeatTaskByTemplateId('.$taskData['TT_ID'].');';
		$rParams = unserialize($taskData['REPLICATE_PARAMS'], ['allowed_classes' => false]);

		$endDate = (string) $rParams['END_DATE'];
		if($endDate != '' && MakeTimeStamp($endDate) < time())
		{
			if(isset($agents[$taskId])) // end date in the past, but agent still exists - remove it
			{
				CAgent::RemoveAgent($agents[$taskData['TT_ID']]['NAME'], 'tasks');
			}
		}
		else
		{
			if(!array_key_exists($taskData['TT_ID'], $agents))
			{
				$nextTime = CTasks::getNextTime($rParams, array(
					'ID' => $taskData['TT_ID'],
					'CREATED_BY' => $taskData['TT_CREATED_BY'],
					'TPARAM_REPLICATION_COUNT' => $taskData['TT_TPARAM_REPLICATION_COUNT'],
				));

				if ($nextTime) // task will be repeated, so add agent, if there is no such
				{
					CAgent::AddAgent(
						$name,
						'tasks',
						'N', 		// is periodic?
						86400, 		// interval (24 hours)
						$nextTime, 	// datecheck
						'Y', 		// is active?
						$nextTime	// next_exec
					);
				}
			}
		}
	}
}