oxserversmanager.php

Go to the documentation of this file.
00001 <?php
00002 
00009 class oxServersManager
00010 {
00011 
00015     const NODE_AVAILABILITY_CHECK_PERIOD = 86400;
00016 
00020     const INACTIVE_NODE_STORAGE_PERIOD = 259200;
00021 
00027     private $_aServersData = array();
00028 
00032     public function __construct()
00033     {
00034         $this->_aServersData = (array) oxRegistry::getConfig()->getConfigParam('aServersData');
00035     }
00036 
00044     public function getServer($sServerId)
00045     {
00046         $aServerData = $this->_getServerData($sServerId);
00047 
00048         return $this->_createServer($sServerId, $aServerData);
00049     }
00050 
00056     public function saveServer($oServer)
00057     {
00058         $aServersData = $this->_getServersData();
00059         $aServersData[$oServer->getId()] = array(
00060             'id'                => $oServer->getId(),
00061             'timestamp'         => $oServer->getTimestamp(),
00062             'ip'                => $oServer->getIp(),
00063             'lastFrontendUsage' => $oServer->getLastFrontendUsage(),
00064             'lastAdminUsage'    => $oServer->getLastAdminUsage(),
00065             'isValid'           => $oServer->isValid()
00066         );
00067 
00068         $this->_save($aServersData);
00069     }
00070 
00076     protected function _getServersData()
00077     {
00078         return $this->_aServersData;
00079     }
00080 
00088     protected function _getServerData($sServerId)
00089     {
00090         $aServers = $this->_getServersData();
00091 
00092         return array_key_exists($sServerId, $aServers) ? $aServers[$sServerId] : array();
00093     }
00094 
00103     protected function _createServer($sServerId, $aData = array())
00104     {
00106         $oAppServer = oxNew('oxApplicationServer');
00107 
00108         $oAppServer->setId($sServerId);
00109         $oAppServer->setTimestamp($this->_getServerParameter($aData, 'timestamp'));
00110         $oAppServer->setIp($this->_getServerParameter($aData, 'serverIp'));
00111         $oAppServer->setLastFrontendUsage($this->_getServerParameter($aData, 'lastFrontendUsage'));
00112         $oAppServer->setLastAdminUsage($this->_getServerParameter($aData, 'lastAdminUsage'));
00113         $oAppServer->setIsValid($this->_getServerParameter($aData, 'isValid'));
00114 
00115         return $oAppServer;
00116     }
00117 
00126     protected function _getServerParameter($aData, $sName)
00127     {
00128         return array_key_exists($sName, $aData) ? $aData[$sName] : null;
00129     }
00130 
00136     public function getServers()
00137     {
00138         $this->markInActiveServers();
00139         $this->deleteInActiveServers();
00140 
00141         $aServers = $this->_getServersData();
00142         $aValidServers = array();
00143 
00144         foreach ($aServers as $aServer) {
00145             if ($aServer['isValid']) {
00146                 unset($aServer['isValid']);
00147                 unset($aServer['timestamp']);
00148                 $aValidServers[] = $aServer;
00149             }
00150         }
00151 
00152         return $aValidServers;
00153     }
00154 
00160     public function deleteServer($sServerId)
00161     {
00162         $aServersData = $this->_getServersData();
00163         unset($aServersData[$sServerId]);
00164         $this->_save($aServersData);
00165     }
00166 
00170     public function markInActiveServers()
00171     {
00172         $aServersData = $this->_getServersData();
00173 
00174         foreach ($aServersData as $sServerId => $aServerData) {
00175             if ($aServerData['timestamp'] < oxRegistry::get("oxUtilsDate")->getTime() - self::NODE_AVAILABILITY_CHECK_PERIOD) {
00176                 $oServer = $this->getServer($sServerId);
00177                 $oServer->setIsValid(false);
00178                 $this->saveServer($oServer);
00179             }
00180         }
00181     }
00182 
00186     public function deleteInActiveServers()
00187     {
00188         $aServersData = $this->_getServersData();
00189 
00190         foreach ($aServersData as $sServerId => $aServerData) {
00191             if ($aServerData['timestamp'] < oxRegistry::get("oxUtilsDate")->getTime() - self::INACTIVE_NODE_STORAGE_PERIOD) {
00192                 $this->deleteServer($sServerId);
00193             }
00194         }
00195     }
00196 
00202     protected function _save($aServersData)
00203     {
00204         oxRegistry::getConfig()->saveSystemConfigParameter('arr', 'aServersData', $aServersData);
00205         $this->_aServersData = $aServersData;
00206     }
00207 }