Scalr\Model\Entity\Server::terminate PHP Method

terminate() public method

Marks server as to be terminated.
public terminate ( integer | array $reason, boolean $forcefully = null, User $user = null ) : boolean
$reason integer | array The reason possibly with the format parameters.
$forcefully boolean optional Method: forcefully (true) | gracefully (false)
$user Scalr\Model\Entity\Account\User optional The user entity
return boolean
    public function terminate($reason, $forcefully = null, $user = null)
    {
        if (in_array($this->status, [Server::STATUS_PENDING_TERMINATE, Server::STATUS_TERMINATED])) {
            return false;
        }
        $forcefully = $forcefully === null ? true : (bool) $forcefully;
        $fnGetReason = function ($reasonId) {
            $args = func_get_args();
            $args[0] = Server::getTerminateReason($reasonId);
            return [call_user_func_array('sprintf', $args), $reasonId];
        };
        list($reason, $reasonId) = is_array($reason) ? call_user_func_array($fnGetReason, $reason) : $fnGetReason($reason);
        $properties = $this->properties;
        if ($user instanceof User) {
            $properties[self::TERMINATED_BY_ID] = $user->getId();
            $properties[self::TERMINATED_BY_EMAIL] = $user->getEmail();
        }
        $properties[self::REBOOTING] = 0;
        $properties->save();
        $this->update(['status' => Server::STATUS_PENDING_TERMINATE, 'shutdownScheduled' => new DateTime($forcefully ? 'now' : Scalr::config('scalr.system.server_terminate_timeout'))]);
        $this->getHistory()->markAsTerminated($reason, $reasonId);
        if (isset($this->farmId)) {
            $DBServer = $this->__getDBServer();
            Scalr::FireEvent($this->farmId, new BeforeHostTerminateEvent($DBServer, false));
            // If instance was terminated outside scalr, we need manually fire HostDown
            if ($reasonId == self::TERMINATE_REASON_CRASHED) {
                Scalr::FireEvent($this->farmId, new HostDownEvent($DBServer, false));
            }
        }
        return true;
    }