/** * Constructor. * Initializes the process manager and lauches the action if all went well, then delete the PIDFile * NOTE : SCRIPT_CODENAME is a constant that must be defined, and unique accross all usage of the background scripts * (i.e. One background script for one application should have the same, but two applications having the same script shouldn't collate) * * @param boolean $debug Set to true if you want a debug of what the script does * @return void * @access public */ function backgroundScript($debug = false, $scriptID = 'Master') { $this->_debug = $debug; $this->_processManager = new processManager(SCRIPT_CODENAME . '_' . $scriptID); // Cleans previous PIDs if (isset($_SERVER['argv']['3']) && $_SERVER['argv']['3'] == '-F') { if (!APPLICATION_IS_WINDOWS) { $tmpDir = dir($this->_processManager->getTempPath()); while (false !== ($file = $tmpDir->read())) { if (io::strpos($file, SCRIPT_CODENAME) !== false) { @unlink($this->_processManager->getTempPath() . '/' . $file); } } } else { $files = glob(realpath($this->_processManager->getTempPath()) . '/' . SCRIPT_CODENAME . '*.*', GLOB_NOSORT); if (is_array($files)) { foreach ($files as $file) { if (!CMS_file::deleteFile($file)) { $this->raiseError("Can't delete file " . $file); } } } } } //write script process PID File if ($this->_processManager->writePIDFile()) { if ($this->_debug) { $this->raiseError("PID file successfully written (" . $this->_processManager->getPIDFileName() . ")."); } //start script process $this->activate($this->_debug); //delete script process PID File if ($this->_processManager->deletePIDFile()) { if ($this->_debug) { $this->raiseError("PID file successfully deleted (" . $this->_processManager->getPIDFileName() . ")."); } } else { $this->raiseError("Can not delete PID file (" . $this->_processManager->getPIDFileName() . ")."); } exit; } else { if ($this->_debug) { $this->raiseError("PID file already exists or impossible to write (" . $this->_processManager->getPIDFileName() . ")."); } exit; } }
* Regenerates pages stored in the 'regenerator' table * * @package Automne * @subpackage scripts * @author Sébastien Pauchet <*****@*****.**> * @author Antoine Pouch <*****@*****.**> */ //must calculate the document root first (for compatibility with old scripts) $_SERVER["DOCUMENT_ROOT"] = realpath(substr(dirname(__FILE__), 0, strlen(dirname(__FILE__)) - strpos(strrev(dirname(__FILE__)), "enmotua") - strlen("automne") - 1)); //include required file require_once dirname(__FILE__) . '/../../../cms_rc_admin.php'; /** * Script codename */ //clean the application label $appCode = processManager::getAppCode(); define("SCRIPT_CODENAME", "bgscript_" . $appCode . "_regenerator"); //time out in second for scripts if (!defined('SUB_SCRIPT_TIME_OUT')) { define("SUB_SCRIPT_TIME_OUT", 300); //5 minutes } if (!defined('MASTER_SCRIPT_TIME_OUT')) { define("MASTER_SCRIPT_TIME_OUT", 43200); //12 hours } //duration in seconds between each cycles of checking of sub-scripts if (!defined('SLEEP_TIME')) { define("SLEEP_TIME", 1); } //for script debug - verbose in cms_error_log
/** * Start the scripts process queue. * Remove the lock file then relaunch the script if force is true * * @param boolean $force Set to true if you wish to remove the lock file before launch * @return void * @access public * @static */ static function startScript($force = false) { if (USE_BACKGROUND_REGENERATOR) { $forceRestart = ''; if ($force) { $forceRestart = ' -F'; } elseif (processManager::hasRunningScript()) { return false; } //test if we're on windows or linux, for the output redirection if (APPLICATION_IS_WINDOWS) { if (realpath(PATH_PHP_CLI_WINDOWS) === false) { CMS_grandFather::raiseError("Unknown CLI location : " . PATH_PHP_CLI_WINDOWS . ", please check your configuration."); return false; } // Create the BAT file $command = '@echo off' . "\r\n" . 'start /B /LOW ' . realpath(PATH_PHP_CLI_WINDOWS) . ' ' . realpath(PATH_PACKAGES_FS . '\\scripts\\script.php') . ' -m ' . REGENERATION_THREADS . $forceRestart; $replace = array('program files (x86)' => 'progra~2', 'program files' => 'progra~1', 'documents and settings' => 'docume~1'); $command = str_ireplace(array_keys($replace), $replace, $command); if (!@touch(PATH_WINDOWS_BIN_FS . "/script.bat")) { CMS_grandFather::_raiseError("CMS_scriptsManager : startScript : Create file error : " . PATH_WINDOWS_BIN_FS . "/script.bat"); return false; } $fh = @fopen(PATH_WINDOWS_BIN_FS . "/script.bat", "wb"); if (is_resource($fh)) { if (!@fwrite($fh, $command, io::strlen($command))) { CMS_grandFather::raiseError("Save file error : script.bat"); } @fclose($fh); } $WshShell = new COM("WScript.Shell"); $oExec = $WshShell->Run(str_ireplace(array_keys($replace), $replace, realpath(PATH_WINDOWS_BIN_FS . '\\script.bat')), 0, false); } else { $error = ''; if (!defined('PATH_PHP_CLI_UNIX') || !PATH_PHP_CLI_UNIX) { $return = CMS_patch::executeCommand('which php 2>&1', $error); if ($error) { CMS_grandFather::raiseError('Error when finding php CLI with command "which php", please check your configuration : ' . $error); return false; } if (io::substr($return, 0, 1) != '/') { CMS_grandFather::raiseError('Can\'t find php CLI with command "which php", please check your configuration.'); return false; } $return = CMS_patch::executeCommand("cd " . PATH_REALROOT_FS . "; php " . PATH_PACKAGES_FS . "/scripts/script.php -m " . REGENERATION_THREADS . $forceRestart . " > /dev/null 2>&1 &", $error); if ($error) { CMS_grandFather::raiseError('Error during execution of script command (cd ' . PATH_REALROOT_FS . '; php ' . PATH_PACKAGES_FS . '/scripts/script.php -m ' . REGENERATION_THREADS . $forceRestart . '), please check your configuration : ' . $error); return false; } } else { $return = CMS_patch::executeCommand(PATH_PHP_CLI_UNIX . ' -v 2>&1', $error); if ($error) { CMS_grandFather::raiseError('Error when testing php CLI with command "' . PATH_PHP_CLI_UNIX . ' -v", please check your configuration : ' . $error); return false; } if (io::strpos(io::strtolower($return), '(cli)') === false) { CMS_grandFather::raiseError(PATH_PHP_CLI_UNIX . ' is not the CLI version'); return false; } $return = CMS_patch::executeCommand("cd " . PATH_REALROOT_FS . "; " . PATH_PHP_CLI_UNIX . " " . PATH_PACKAGES_FS . "/scripts/script.php -m " . REGENERATION_THREADS . $forceRestart . " > /dev/null 2>&1 &", $error); if ($error) { CMS_grandFather::raiseError('Error during execution of script command (cd ' . PATH_REALROOT_FS . '; ' . PATH_PHP_CLI_UNIX . ' ' . PATH_PACKAGES_FS . '/scripts/script.php -m ' . REGENERATION_THREADS . $forceRestart . '), please check your configuration : ' . $error); return false; } } //CMS_grandFather::log($return); //CMS_grandFather::log("cd ".PATH_REALROOT_FS."; php ".PATH_PACKAGES_FS."/scripts/script.php -m ".REGENERATION_THREADS.$forceRestart." > /dev/null 2>&1 &"); //@system("cd ".PATH_REALROOT_FS."; php ".PATH_PACKAGES_FS."/scripts/script.php -m ".REGENERATION_THREADS.$forceRestart." > /dev/null 2>&1 &"); } } else { CMS_session::setSessionVar('start_script', true); } }
/** * select all running scripts from scriptsStatuses Table and check PID files. * * @return array * @access public */ function getRunningScript() { //check temporary dir for orchan PID files //get temporary path $tempPath = CMS_file::getTmpPath(); //computes the directory to put files in $tempDir = @dir($tempPath); if (!is_object($tempDir)) { return array(); } //script application label $scriptAppLbl = processManager::getAppCode(); //Automatic list of directory content //Displayed in alphabetical order (noted on Windows platforms) $PIDFiles = array(); while (false !== ($file = $tempDir->read())) { if (stripos($file, $scriptAppLbl) !== false && io::strpos($file, ".ok") === false) { $PIDFiles[] = $file; } } //check the table $sql = "\n\t\t\tselect\n\t\t\t\t*\n\t\t\tfrom\n\t\t\t\tscriptsStatuses\n\t\t\torder by launchDate_ss\n\t\t\t"; $q = new CMS_query($sql); $scripts = array(); $modules = array(); while ($data = $q->getArray()) { $PIDFileStatus = 0; if (array_search($data["scriptName_ss"], $PIDFiles) !== false) { $process = new processManager($data["scriptName_ss"]); if (@is_file($process->getPIDFilePath() . ".ok")) { $PIDFileStatus = 3; } else { $PIDFileStatus = 1; } $key = array_search($data["scriptName_ss"], $PIDFiles); unset($PIDFiles[$key]); } $scriptTitle = ''; //instanciate module if not exists if (isset($data['module_ss']) && $data['module_ss'] != self::MASTER_SCRIPT_NAME) { if (!isset($modules[$data['module_ss']])) { $modules[$data['module_ss']] = CMS_modulesCatalog::getByCodename($data['module_ss']); } if (is_object($modules[$data['module_ss']])) { $scriptTitle = $modules[$data['module_ss']]->scriptInfo(unserialize($data['parameters_ss'])); } else { $scriptTitle = 'Error : script module not set'; } } elseif ($data['module_ss'] == self::MASTER_SCRIPT_NAME) { $scriptTitle = self::MASTER_SCRIPT_NAME; } else { $scriptTitle = 'Error : script module not set'; } $script = array("Title" => $scriptTitle, "Date" => $data["launchDate_ss"], "PIDFile" => $PIDFileStatus); $scripts[] = $script; } //add orphan PIDFiles to the report foreach ($PIDFiles as $anOrphanPIDFile) { $script = array("Title" => str_replace('_', ' ', str_replace('bgscript_', '', $anOrphanPIDFile)), "Date" => '', "PIDFile" => '2'); $scripts[] = $script; } return $scripts; }
if (!USE_BACKGROUND_REGENERATOR) { CMS_scriptsManager::runQueuedScripts(); } define("MESSAGE_PAGE_ACTION_SCRIPTS_LEFT_NONE", 10); define("MESSAGE_PAGE_SCRIPTS_IN_PROGRESS", 735); define("MESSAGE_PAGE_SCRIPTS_IN_PROGRESS_PID_OK", 736); define("MESSAGE_PAGE_NO_SCRIPTS_PID_OK", 737); define("MESSAGE_PAGE_SCRIPTS_END_PID_OK", 738); define("MESSAGE_PAGE_NO_SCRIPTS_IN_PROGRESS", 739); define("MESSAGE_PAGE_NO_SCRIPTS_QUEUED", 740); //Controler vars $details = sensitiveIO::request('details') == 'true' ? true : false; $queue = sensitiveIO::request('queue') == 'true' ? true : false; $xmlcontent = $detailsContent = $queueContent = ''; if ($details) { $runningScripts = processManager::getRunningScript(); if (is_array($runningScripts) && sizeof($runningScripts)) { $detailsContent = '<ul class="atm-server">'; foreach ($runningScripts as $runningScript) { $date = new CMS_date(); $date->setFromDBValue($runningScript["Date"]); switch ($runningScript["PIDFile"]) { case '0': $detailsContent .= '<li class="atm-pic-question" ext:qtip="' . $cms_language->getMessage(MESSAGE_PAGE_SCRIPTS_IN_PROGRESS) . '">' . $runningScript["Title"] . ' (' . $date->getLocalizedDate($cms_language->getDateFormat() . " H:i:s") . ')</li>'; break; case '1': $detailsContent .= '<li class="atm-pic-ok" ext:qtip="' . $cms_language->getMessage(MESSAGE_PAGE_SCRIPTS_IN_PROGRESS_PID_OK) . '">' . $runningScript["Title"] . ' (' . $date->getLocalizedDate($cms_language->getDateFormat() . " H:i:s") . ')</li>'; break; case '2': $detailsContent .= '<li class="atm-pic-cancel" ext:qtip="' . $cms_language->getMessage(MESSAGE_PAGE_NO_SCRIPTS_PID_OK) . '">' . $runningScript["Title"] . ' (' . $date->getLocalizedDate($cms_language->getDateFormat() . " H:i:s") . ')</li>'; break;