. * * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco, * California 94120-7775, or email info@knowledgetree.com. * * The interactive user interfaces in modified source and object code versions * of this program must display Appropriate Legal Notices, as required under * Section 5 of the GNU General Public License version 3. * * In accordance with Section 7(b) of the GNU General Public License version 3, * these Appropriate Legal Notices must retain the display of the "Powered by * KnowledgeTree" logo and retain the original copyright notice. If the display of the * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices * must display the words "Powered by KnowledgeTree" and retain the original * copyright notice. * * @copyright 2008-2009, KnowledgeTree Inc. * @license GNU General Public License version 3 * @author KnowledgeTree Team * @package Migrater * @version Version 0.1 */ class unixScheduler extends unixService { private $schedulerDir; private $schedulerSource; private $schedulerSourceLoc; private $systemDir; public function __construct() { $this->name = "KTSchedulerTest"; $this->util = new MigrateUtil(); } public function load() { $this->setSystemDir(SYSTEM_ROOT."bin".DS); $this->setSchedulerDir(SYSTEM_DIR."bin".DS); $this->setSchedulerSource('schedulerTask.sh'); $this->setSchedulerSourceLoc('schedulerTask.sh'); } function setSystemDir($systemDir) { $this->systemDir = $systemDir; } function getSystemDir() { if(file_exists($this->systemDir)) return $this->systemDir; return false; } function setSchedulerDir($schedulerDir) { $this->schedulerDir = $schedulerDir; } function getSchedulerDir() { return $this->schedulerDir; } function setSchedulerSource($schedulerSource) { $this->schedulerSource = $schedulerSource; } function getSchedulerSource() { return $this->schedulerSource; } function setSchedulerSourceLoc($schedulerSourceLoc) { $this->schedulerSourceLoc = $this->getSchedulerDir().$schedulerSourceLoc; } function getSchedulerSourceLoc() { if(file_exists($this->schedulerSourceLoc)) return $this->schedulerSourceLoc; return false; } function writeSchedulerTask() { $fLoc = $this->getSchedulerDir().$this->getSchedulerSource(); $fp = @fopen($fLoc, "w+"); $content = "#!/bin/sh\n"; $content .= "cd ".$this->getSchedulerDir()."\n"; $content .= "while true; do\n"; // TODO : This will not work without CLI $content .= "php -Cq scheduler.php\n"; $content .= "sleep 30\n"; $content .= "done"; @fwrite($fp, $content); @fclose($fp); @chmod($fLoc, '0644'); } function migrate() { $status = $this->status(); if($status == '') { return $this->start(); } else { return $status; } } function unmigrate() { $this->stop(); } function stop() { $cmd = "pkill -f ".$this->schedulerSource; $response = $this->util->pexec($cmd); return $response; } function status() { $cmd = "ps ax | grep ".$this->getSchedulerSource(); $response = $this->util->pexec($cmd); if(is_array($response['out'])) { if(count($response['out']) > 1) { foreach ($response['out'] as $r) { preg_match('/grep/', $r, $matches); // Ignore grep if(!$matches) { return 'STARTED'; } } } else { return ''; } } return ''; } function start() { // TODO : Write sh on the fly? Not sure the reasoning here $source = $this->getSchedulerSourceLoc(); return ; if($source) { // Source $cmd = "nohup ".$source." > ".SYS_LOG_DIR."scheduler.log 2>&1 & echo $!"; $response = $this->util->pexec($cmd); return $response; } else { // Could be Stack $source = SYS_BIN_DIR.$this->schedulerSource; if(!file_exists($source)) { // Write it $this->writeSchedulerTask(); } $cmd = "nohup ".$source." > ".SYS_LOG_DIR."scheduler.log 2>&1 & echo $!"; $response = $this->util->pexec($cmd); return $response; } return false; } } ?>