OXID eShop CE  4.9.8
 All Classes Files Functions Variables Pages
oxserversmanager.php
Go to the documentation of this file.
1 <?php
2 
10 {
11 
16 
21 
27  private $_aServersData = array();
28 
32  public function __construct()
33  {
34  $this->_aServersData = (array) oxRegistry::getConfig()->getConfigParam('aServersData');
35  }
36 
44  public function getServer($sServerId)
45  {
46  $aServerData = $this->_getServerData($sServerId);
47 
48  return $this->_createServer($sServerId, $aServerData);
49  }
50 
56  public function saveServer($oServer)
57  {
58  $aServersData = $this->_getServersData();
59  $aServersData[$oServer->getId()] = array(
60  'id' => $oServer->getId(),
61  'timestamp' => $oServer->getTimestamp(),
62  'ip' => $oServer->getIp(),
63  'lastFrontendUsage' => $oServer->getLastFrontendUsage(),
64  'lastAdminUsage' => $oServer->getLastAdminUsage(),
65  'isValid' => $oServer->isValid()
66  );
67 
68  $this->_save($aServersData);
69  }
70 
76  protected function _getServersData()
77  {
78  return $this->_aServersData;
79  }
80 
88  protected function _getServerData($sServerId)
89  {
90  $aServers = $this->_getServersData();
91 
92  return array_key_exists($sServerId, $aServers) ? $aServers[$sServerId] : array();
93  }
94 
103  protected function _createServer($sServerId, $aData = array())
104  {
106  $oAppServer = oxNew('oxApplicationServer');
107 
108  $oAppServer->setId($sServerId);
109  $oAppServer->setTimestamp($this->_getServerParameter($aData, 'timestamp'));
110  $oAppServer->setIp($this->_getServerParameter($aData, 'serverIp'));
111  $oAppServer->setLastFrontendUsage($this->_getServerParameter($aData, 'lastFrontendUsage'));
112  $oAppServer->setLastAdminUsage($this->_getServerParameter($aData, 'lastAdminUsage'));
113  $oAppServer->setIsValid($this->_getServerParameter($aData, 'isValid'));
114 
115  return $oAppServer;
116  }
117 
126  protected function _getServerParameter($aData, $sName)
127  {
128  return array_key_exists($sName, $aData) ? $aData[$sName] : null;
129  }
130 
136  public function getServers()
137  {
138  $this->markInActiveServers();
139  $this->deleteInActiveServers();
140 
141  $aServers = $this->_getServersData();
142  $aValidServers = array();
143 
144  foreach ($aServers as $aServer) {
145  if ($aServer['isValid']) {
146  unset($aServer['isValid']);
147  unset($aServer['timestamp']);
148  $aValidServers[] = $aServer;
149  }
150  }
151 
152  return $aValidServers;
153  }
154 
160  public function deleteServer($sServerId)
161  {
162  $aServersData = $this->_getServersData();
163  unset($aServersData[$sServerId]);
164  $this->_save($aServersData);
165  }
166 
170  public function markInActiveServers()
171  {
172  $aServersData = $this->_getServersData();
173 
174  foreach ($aServersData as $sServerId => $aServerData) {
175  if ($aServerData['timestamp'] < oxRegistry::get("oxUtilsDate")->getTime() - self::NODE_AVAILABILITY_CHECK_PERIOD) {
176  $oServer = $this->getServer($sServerId);
177  $oServer->setIsValid(false);
178  $this->saveServer($oServer);
179  }
180  }
181  }
182 
186  public function deleteInActiveServers()
187  {
188  $aServersData = $this->_getServersData();
189 
190  foreach ($aServersData as $sServerId => $aServerData) {
191  if ($aServerData['timestamp'] < oxRegistry::get("oxUtilsDate")->getTime() - self::INACTIVE_NODE_STORAGE_PERIOD) {
192  $this->deleteServer($sServerId);
193  }
194  }
195  }
196 
202  protected function _save($aServersData)
203  {
204  oxRegistry::getConfig()->saveSystemConfigParameter('arr', 'aServersData', $aServersData);
205  $this->_aServersData = $aServersData;
206  }
207 }