DBServer::getTerminateReason PHP Method

getTerminateReason() public static method

Gets terminate reason
See also: Scalr\Model\Entity\Server::getTerminateReason()
Deprecation:
public static getTerminateReason ( integer $reasonId ) : string
$reasonId integer Reason id
return string
    public static function getTerminateReason($reasonId)
    {
        $reasons = [1 => 'Shutting-down %s cluster.', 2 => 'Removing replica set from %s cluster.', 3 => 'Farm role does not exist.', 4 => 'Role removed from farm.', 5 => 'Server did not send %s event in %s seconds after launch.', 6 => 'Terminating temporary server.', 7 => 'Terminating role builder temporary server.', 8 => 'Scaling down.', 9 => 'Snapshot cancellation.', 10 => 'Manually terminated by %s.', 11 => 'Terminated through the Scalr API by %s.', 12 => 'Farm was in "%s" state. Server terminated when bundle task has been finished. Bundle task #%s.', 13 => 'Terminating server because the farm has been terminated.', 14 => 'Server replaced with new one after snapshotting.', 15 => 'Server launch was canceled', 16 => 'Server was terminated in cloud or from within an OS'];
        if ($reasonId && !isset($reasons[$reasonId])) {
            throw new Exception(sprintf('Terminate reason %d doesn\'t have message', $reasonId));
        }
        return $reasonId ? $reasons[$reasonId] : '';
    }

Usage Example

Example #1
0
 /**
  * Marks server as to be terminated.
  *
  * @param   integer|array           $reason      The reason possibly with the format parameters.
  * @param   bool                   $forcefully  optional Method: forcefully (true) | gracefully (false)
  * @param   Scalr_Account_User|int $user        optional The user object or its unique identifier
  */
 public function terminate($reason, $forcefully = null, $user = null)
 {
     if (in_array($this->status, array(SERVER_STATUS::PENDING_TERMINATE, SERVER_STATUS::TERMINATED))) {
         return;
     }
     $forcefully = $forcefully === null ? true : (bool) $forcefully;
     //Ensures handling identifier of the user instead of the object
     if ($user !== null && !$user instanceof \Scalr_Account_User) {
         try {
             $user = Scalr_Account_User::init()->loadById(intval($user));
         } catch (\Exception $e) {
         }
     }
     $fnGetReason = function ($reasonId) {
         $args = func_get_args();
         $args[0] = DBServer::getTerminateReason($reasonId);
         return [call_user_func_array('sprintf', $args), $reasonId];
     };
     list($reason, $reasonId) = is_array($reason) ? call_user_func_array($fnGetReason, $reason) : $fnGetReason($reason);
     //Set who does terminate the server
     if ($user instanceof \Scalr_Account_User) {
         $this->SetProperties(array(\SERVER_PROPERTIES::TERMINATED_BY_ID => $user->id, \SERVER_PROPERTIES::TERMINATED_BY_EMAIL => $user->getEmail()));
     }
     $this->SetProperties([SERVER_PROPERTIES::REBOOTING => 0]);
     $this->update(['status' => SERVER_STATUS::PENDING_TERMINATE, 'dateShutdownScheduled' => date("Y-m-d H:i:s", $forcefully ? time() : strtotime(Scalr::config('scalr.system.server_terminate_timeout')))]);
     $this->getServerHistory()->markAsTerminated($reason, $reasonId);
     if (isset($this->farmId)) {
         Scalr::FireEvent($this->farmId, new BeforeHostTerminateEvent($this, false));
         // If instance was terminated outside scalr, we need manually fire HostDown
         if ($reasonId == self::TERMINATE_REASON_CRASHED) {
             Scalr::FireEvent($this->farmId, new HostDownEvent($this, false));
         }
     }
 }