<commit_before><?php
/**
 * @package modules.task
 */
class task_PlannedTaskRunner
{
	/**
	 * @param task_persistentdocument_plannedtask $runnableTask
	 */
	static function executeSystemTask($runnableTask)
	{
		$taskService = task_PlannedtaskService::getInstance();
		$logMessages = array();
		try
		{
			ob_start();
			$taskClassName = $runnableTask->getSystemtaskclassname();
			if (!f_util_ClassUtils::classExists($taskClassName))
			{
				throw new Exception("Class $taskClassName does not exist");
			}
			
			$reflectionClass = new ReflectionClass($taskClassName);
			
			if (!$reflectionClass->implementsInterface('task_SystemTask'))
			{
				throw new Exception("Class $taskClassName does not implement task_SystemTask");
			}
			
			$classInstance = $reflectionClass->newInstance();
			$classInstance->setParameterString($runnableTask->getParameters());
			$classInstance->setPlannedTask($runnableTask);
			$startTime = time();
			$classInstance->run();
			if (Framework::isInfoEnabled())
			{
				Framework::info($runnableTask->getId() . ' [' . $taskClassName . '] executed.');
			}
			$failed = false;
		}
		catch (BaseException $e)
		{
			$logMessages[] = $e->getLocaleMessage();
			$failed = true;
		}
		catch (Exception $e)
		{
			$logMessages[] = $e->getMessage();
			$failed = true;
		}
		if (defined('MYSQL_WAIT_TIMEOUT') && time() - $startTime >=  MYSQL_WAIT_TIMEOUT)
		{
			// Make sure we didn't loose the MySQL connection due to inactivity timeout
			f_persistentdocument_PersistentProvider::refresh();
		}
		if ($failed === true)
		{
			$logMessages[] = ob_get_clean();
		}
		$runnableTask->setHasFailed($failed);
		
		$taskService->rescheduleIfNecesseary($runnableTask);
		if ($failed)
		{
			$action = 'run-failed.plannedtask';
			UserActionLoggerService::getInstance()->addUserDocumentEntry(null, $action, $runnableTask, array('message' => implode("\n", $logMessages)), 'task');
		}
	}
	
	/**
	 * @param string $baseURL
	 * @return void
	 */
	static function main($baseURL)
	{
		$taskService = task_PlannedtaskService::getInstance();
		$runnableTasks = $taskService->getRunnableTasks();
		$runningIds = array();
		foreach ($runnableTasks as $runnableTask)
		{
			if ($taskService->run($runnableTask))
			{
				$runningIds[] = $runnableTask->getId();
			}
		}

		foreach ($runningIds as $runningId)
		{
			$url = $baseURL . '/changecron.php?taskId=' . $runningId;
			self::launchTask($url);
		}
	}
	
	/**
	 * @param string $URL
	 */
	public static function launchTask($URL)
	{
		$rc = curl_init();
		curl_setopt($rc, CURLOPT_RETURNTRANSFER, false);
		curl_setopt($rc, CURLOPT_USERAGENT, 'RBSChange/3.0');
		curl_setopt($rc, CURLOPT_POSTFIELDS, null);
		curl_setopt($rc, CURLOPT_POST, false);
		curl_setopt($rc, CURLOPT_TIMEOUT, 5);
		curl_setopt($rc, CURLOPT_FOLLOWLOCATION, 0);
		curl_setopt($rc, CURLOPT_URL, $URL);
		curl_exec($rc);
		curl_close($rc);
	}
	
	/**
	 * @param string $pingURL
	 */
	public static function pingChangeCronURL($pingURL)
	{
		$rc = curl_init();
		curl_setopt($rc, CURLOPT_RETURNTRANSFER, false);
		curl_setopt($rc, CURLOPT_USERAGENT, 'RBSChange/3.0');
		curl_setopt($rc, CURLOPT_POSTFIELDS, null);
		curl_setopt($rc, CURLOPT_POST, false);
		curl_setopt($rc, CURLOPT_TIMEOUT, 1);
		curl_setopt($rc, CURLOPT_FOLLOWLOCATION, 0);
		curl_setopt($rc, CURLOPT_URL, $pingURL);
		curl_exec($rc);
		curl_close($rc);
	}	
	
	/**
	 * @param string $start
	 */
	public static function setChangeCronToken($start)
	{
		$tokenPath = f_util_FileUtils::buildCachePath('changecron.pid');
		f_util_FileUtils::writeAndCreateContainer($tokenPath, $start, f_util_FileUtils::OVERRIDE);
	}

	/**
	 * @return string<commit_msg>[FIX] Optimize non caching changecron.php request<commit_after><?php
/**
 * @package modules.task
 */
class task_PlannedTaskRunner
{
	/**
	 * @param task_persistentdocument_plannedtask $runnableTask
	 */
	static function executeSystemTask($runnableTask)
	{
		$taskService = task_PlannedtaskService::getInstance();
		$logMessages = array();
		try
		{
			ob_start();
			$taskClassName = $runnableTask->getSystemtaskclassname();
			if (!f_util_ClassUtils::classExists($taskClassName))
			{
				throw new Exception("Class $taskClassName does not exist");
			}
			
			$reflectionClass = new ReflectionClass($taskClassName);
			
			if (!$reflectionClass->implementsInterface('task_SystemTask'))
			{
				throw new Exception("Class $taskClassName does not implement task_SystemTask");
			}
			
			$classInstance = $reflectionClass->newInstance();
			$classInstance->setParameterString($runnableTask->getParameters());
			$classInstance->setPlannedTask($runnableTask);
			$startTime = time();
			$classInstance->run();
			if (Framework::isInfoEnabled())
			{
				Framework::info($runnableTask->getId() . ' [' . $taskClassName . '] executed.');
			}
			$failed = false;
		}
		catch (BaseException $e)
		{
			$logMessages[] = $e->getLocaleMessage();
			$failed = true;
		}
		catch (Exception $e)
		{
			$logMessages[] = $e->getMessage();
			$failed = true;
		}
		if (defined('MYSQL_WAIT_TIMEOUT') && time() - $startTime >=  MYSQL_WAIT_TIMEOUT)
		{
			// Make sure we didn't loose the MySQL connection due to inactivity timeout
			f_persistentdocument_PersistentProvider::refresh();
		}
		if ($failed === true)
		{
			$logMessages[] = ob_get_clean();
		}
		$runnableTask->setHasFailed($failed);
		
		$taskService->rescheduleIfNecesseary($runnableTask);
		if ($failed)
		{
			$action = 'run-failed.plannedtask';
			UserActionLoggerService::getInstance()->addUserDocumentEntry(null, $action, $runnableTask, array('message' => implode("\n", $logMessages)), 'task');
		}
	}
	
	/**
	 * @param string $baseURL
	 * @return void
	 */
	static function main($baseURL)
	{
		$taskService = task_PlannedtaskService::getInstance();
		$runnableTasks = $taskService->getRunnableTasks();
		$runningIds = array();
		foreach ($runnableTasks as $runnableTask)
		{
			if ($taskService->run($runnableTask))
			{
				$runningIds[] = $runnableTask->getId();
			}
		}

		foreach ($runningIds as $runningId)
		{
			$url = $baseURL . '/changecron.php?taskId=' . $runningId;
			self::launchTask($url);
		}
	}
	
	/**
	 * @param string $URL
	 */
	public static function launchTask($URL)
	{
		//Framework::info(__METHOD__ . ' ' . $URL);
		
		$rc = curl_init();
		curl_setopt($rc, CURLOPT_RETURNTRANSFER, false);
		curl_setopt($rc, CURLOPT_HTTPHEADER, array('Cache-Control: no-store, no-cache, no-transform', 'Pragma: no-cache', 'Connection: close'));
		curl_setopt($rc, CURLOPT_USERAGENT, 'RBSChange/3.0');
		curl_setopt($rc, CURLOPT_POST, 1);
		curl_setopt($rc, CURLOPT_POSTFIELDS, null);	
		curl_setopt($rc, CURLOPT_TIMEOUT, 5);
		curl_setopt($rc, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($rc, CURLOPT_URL, $URL);
		curl_exec($rc);
		curl_close($rc);
	}
	
	/**
	 * @param string $pingURL
	 */
	public static function pingChangeCronURL($pingURL)
	{
		//Framework::info(__METHOD__ . ' ' . $pingURL);
		
		$rc = curl_init();
		curl_setopt($rc, CURLOPT_RETURNTRANSFER, false);
		curl_setopt($rc, CURLOPT_HTTPHEADER, array('Cache-Control: no-store, no-cache, no-transform', 'Pragma: no-cache', 'Connection: close'));
		curl_setopt($rc, CURLOPT_USERAGENT, 'RBSChange/3.0');
		curl_setopt($rc, CURLOPT_POST, 1);
		curl_setopt($rc, CURLOPT_POSTFIELDS, null);
		curl_setopt($rc, CURLOPT_FRESH_CONNECT, 1); // don't use a cached version of the url 	
		curl_setopt($rc, CURLOPT_TIMEOUT, 1);
		curl_setopt($rc, CURLOPT_FOLLOWLOCATION, 0);
		curl_setopt($rc, CURLOPT_URL, $pingURL);
		curl_exec($rc);
		curl_close($rc);
	}	
	
	/**
	 * @param string $start
	 */
	public static function setChangeCronToken($start)
	{
		$tokenPath = f_util_FileUtils::buildCachePath('changecron.pid');
		f_util_FileUtils::writeAndCreateContainer($tokenPath, $start, f_util_FileUtils::OVERRIDE);
	}

	/**
	 * @return string
{'fraction_non_alphanumeric': 0.1555413196668802, 'fraction_numerical': 0.0023062139654067906, 'mean_word_length': 4.656521739130435, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '890908', 'n_tokens_mistral': 3127, 'n_tokens_neox': 2654, 'n_words': 467}