protected function execute($arguments = array(), $options = array()) { // Context $context = sfContext::createInstance(sfProjectConfiguration::getApplicationConfiguration('app', $options['env'], true)); // initialize the database connection $databaseManager = new sfDatabaseManager($this->configuration); $connection = $databaseManager->getDatabase($options['connection'])->getConnection(); // add your code here // server id $server = $arguments['server']; $this->log("[INFO] Shutdown server with '{$server}'"); $etva_server = EtvaServerPeer::retrieveByPK($server); if (!$etva_server) { $etva_server = EtvaServerPeer::retrieveByUuid($server); } if (!$etva_server) { $etva_server = EtvaServerPeer::retrieveByName($server); } if (!$etva_server) { $msg_i18n = $context->getI18N()->__(EtvaServerPeer::_ERR_NOTFOUND_, array('name' => $server)); $error = array('success' => false, 'agent' => sfConfig::get('config_acronym'), 'error' => $msg_i18n, 'info' => $msg_i18n); $this->log("[ERROR] " . $error['error']); return $error; } else { if ($nid = $options['node']) { $etva_node = EtvaNodePeer::retrieveByPK($nid); } else { $etva_node = $etva_server->getEtvaNode(); } if (!$etva_node) { //notify event log $msg_i18n = Etva::makeNotifyLogMessage(sfConfig::get('config_acronym'), EtvaNodePeer::_ERR_NOTFOUND_ID_, array('id' => $nid), EtvaServerPeer::_ERR_STOP_, array('name' => $server)); $error = array('success' => false, 'agent' => sfConfig::get('config_acronym'), 'error' => $msg_i18n, 'info' => $msg_i18n); $this->log("[ERROR] " . $error['error']); return $error; } $destroy = $options['destroy'] ? 1 : 0; $force = $options['force'] ? 1 : 0; $extra = array('destroy' => $destroy, 'force' => $force); $server_va = new EtvaServer_VA($etva_server); $response = $server_va->send_stop($etva_node, $extra); if ($response['success']) { $this->log("[INFO] " . $response['response']); } else { $this->log("[ERROR] " . $response['error']); } return $response; } }
protected function execute($arguments = array(), $options = array()) { $context = sfContext::createInstance(sfProjectConfiguration::getApplicationConfiguration('app', $options['env'], true)); parent::execute($arguments, $options); // initialize the database connection $databaseManager = new sfDatabaseManager($this->configuration); $con = $databaseManager->getDatabase($options['connection'])->getConnection(); // add your code here $this->log('[INFO] Checking Servers heartbeat...' . "\n"); $servers_timeout = EtvaServerQuery::create()->filterByVmState(EtvaServer::RUNNING)->filterByGaState(EtvaServerPeer::_GA_RUNNING_)->filterByHasha(1)->where('hbtimeout>0 AND UNIX_TIMESTAMP(heartbeat) < (UNIX_TIMESTAMP(now()) - hbtimeout)')->useEtvaServerAssignQuery('ServerAssign', 'RIGHT JOIN')->useEtvaNodeQuery()->filterByState(EtvaNode::NODE_ACTIVE)->endUse()->endUse()->find(); if (count($servers_timeout) > 0) { foreach ($servers_timeout as $etva_server) { $message = sprintf(' The server %s with id %s get heartbeat timed out (last heartbeat at %s and heartbeat timeout %s secods)', $etva_server->getName(), $etva_server->getId(), $etva_server->getHeartbeat(), $etva_server->getHbtimeout()); $this->log($message); // add log message Etva::makeNotifyLogMessage($this->name, $message); $etva_node = $etva_server->getEtvaNode(); $server_va = new EtvaServer_VA($etva_server); $response_ga = $etva_server->getGAInfo($etva_node); if (!$response_ga['success']) { $msg = sprintf(' Something wrong with node %s agent, can\'t get GA state of the server %s with id %s run.', $etva_node->getName(), $etva_server->getName(), $etva_server->getId()); $this->log($msg); // add log message Etva::makeNotifyLogMessage($this->name, $msg); $etva_server->setGaState(EtvaServerPeer::_GA_STOPPED_); // mark GA as stopped $etva_server->save(); } else { if ($reponse_ga['ga_state'] == EtvaServerPeer::_GA_RUNNING_) { $message_ga = sprintf(' But the explicit check GA state of the server %s with id %s run with success.', $etva_server->getName(), $etva_server->getId()); $this->log($message_ga); // add log message Etva::makeNotifyLogMessage($this->name, $message_ga, array(), null, array(), EtvaEventLogger::INFO); } else { // go restart $starttime = sfConfig::get('app_server_heartbeat_starttime'); $starttime_date = date("c", time() - $starttime); if ($etva_server->getHblaststart() && $etva_server->getHblaststart() > $starttime_date) { $msg = sprintf(' the server %s with id %s is in starttime.', $etva_server->getName(), $etva_server->getId()); $this->log($msg); // add log message Etva::makeNotifyLogMessage($this->name, $msg, array(), null, array(), EtvaEventLogger::INFO); } else { $last_nrestarts = $etva_server->getHbnrestarts(); if ($last_nrestarts >= sfConfig::get('app_server_heartbeat_number_of_restart')) { $msg = sprintf(' the server %s with id %s exceed number of restart.', $etva_server->getName(), $etva_server->getId()); $this->log($msg); // add log message Etva::makeNotifyLogMessage($this->name, $msg); } else { $msg = sprintf(' the server %s with id %s is heartbeat out of date and will be restart.', $etva_server->getName(), $etva_server->getId()); $this->log($msg); // add log message Etva::makeNotifyLogMessage($this->name, $msg, array(), null, array(), EtvaEventLogger::INFO); // force to stop $response_stop = $server_va->send_stop($etva_node, array('force' => 1, 'destroy' => 1)); sleep(5); // wait a few seconds $response_start = $server_va->send_start($etva_node); if (!$response_start['success']) { // start fail... sleep(10); // wait a few seconds $response_start = $server_va->send_start($etva_node); // start again } $etva_server->resetHeartbeat(EtvaServerPeer::_GA_STOPPED_); // reset heartbeat and mark GA as stopped $etva_server->setHbnrestarts($last_nrestarts + 1); // inc number of restart $etva_server->save(); } } } } } } else { $this->log("[INFO] No servers with heartbeat timed out."); } // log the message! $this->log("[INFO] The check servers heartbeat task ran!"); }
/** * * * stops virtual machine * * request object is like this; * <code> * $request['nid'] = $nid; //node ID * $request['server'] = $name // server name * </code> * * @param sfWebRequest $request A request object * @return string json string representation of array('success'=>true,'agent'=>$agent, 'response'=>$response) * */ public function executeJsonStop(sfWebRequest $request) { $nid = $request->getParameter('nid'); $server = $request->getParameter('server'); if (!($etva_node = EtvaNodePeer::retrieveByPK($nid))) { //notify event log $msg_i18n = Etva::makeNotifyLogMessage(sfConfig::get('config_acronym'), EtvaNodePeer::_ERR_NOTFOUND_ID_, array('id' => $nid), EtvaServerPeer::_ERR_STOP_, array('name' => $server)); $error = array('success' => false, 'agent' => sfConfig::get('config_acronym'), 'error' => $msg_i18n, 'info' => $msg_i18n); // if is a CLI soap request return json encoded data if (sfConfig::get('sf_environment') == 'soap') { return json_encode($error); } // if is browser request return text renderer $error = $this->setJsonError($error); return $this->renderText($error); } if (!($etva_server = $etva_node->retrieveServerByName($server))) { //notify event log $msg_i18n = Etva::makeNotifyLogMessage($etva_node->getName(), EtvaServerPeer::_ERR_NOTFOUND_, array('name' => $server), EtvaServerPeer::_ERR_STOP_, array('name' => $server)); $error = array('agent' => $etva_node->getName(), 'success' => false, 'error' => $msg_i18n); // if is a CLI soap request return json encoded data if (sfConfig::get('sf_environment') == 'soap') { return json_encode($error); } // if is browser request return text renderer $error = $this->setJsonError($error); return $this->renderText($error); } $destroy = $request->getParameter('destroy') ? 1 : 0; $force = $request->getParameter('force') ? 1 : 0; $extra = array('destroy' => $destroy, 'force' => $force); $server_va = new EtvaServer_VA($etva_server); $response = $server_va->send_stop($etva_node, $extra); if ($response['success']) { $return = json_encode($response); // if the request is made throught soap request... if (sfConfig::get('sf_environment') == 'soap') { return $return; } // if is browser request return text renderer $this->getResponse()->setHttpHeader('Content-type', 'application/json'); return $this->renderText($return); } else { if (sfConfig::get('sf_environment') == 'soap') { return json_encode($response); } $return = $this->setJsonError($response); return $this->renderText($return); } }