Class: RightScale::ShutdownRequest

Inherits:
Object
  • Object
show all
Includes:
ShutdownRequestMixin
Defined in:
lib/instance/shutdown_request.rb

Overview

Represents outstanding request(s) for reboot, stop or terminate instance. Requests are cumulative and implicitly non-decreasing in level (e.g. reboot never superceeds terminate).

Constant Summary

Constants included from ShutdownRequestMixin

RightScale::ShutdownRequestMixin::CONTINUE, RightScale::ShutdownRequestMixin::LEVELS, RightScale::ShutdownRequestMixin::REBOOT, RightScale::ShutdownRequestMixin::STOP, RightScale::ShutdownRequestMixin::TERMINATE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ShutdownRequestMixin

#continue?, #immediately!, #immediately?, #level, #level=, #to_s

Class Method Details

.init(scheduler) ⇒ Object

Class initializer.

Parameters

scheduler(InstanceScheduler)

scheduler for shutdown requests

Return

always true



102
103
104
105
106
# File 'lib/instance/shutdown_request.rb', line 102

def self.init(scheduler)
  @@instance = ShutdownRequest.new
  @@scheduler = scheduler
  true
end

.instanceObject

Factory method

Return

(ShutdownRequest)

the singleton for this class

Raises:



112
113
114
115
# File 'lib/instance/shutdown_request.rb', line 112

def self.instance
  raise NotInitialized.new("ShutdownRequest.init has not been called") unless defined?(@@instance)
  return @@instance
end

.submit(request) ⇒ Object

Submits a new shutdown request state which may be superceded by a previous, higher-priority shutdown level.

Parameters

request(String)

shutdown level

request(Boolean)

shutdown immediacy or nil

Returns

result(ShutdownRequest)

the updated instance



126
127
128
129
130
131
132
133
# File 'lib/instance/shutdown_request.rb', line 126

def self.submit(request)
  # RightNet protocols use kind instead of level, so be a little flexible.
  result = instance
  result.level = request[:kind] || request[:level]
  result.immediately! if request[:immediately]
  @@scheduler.schedule_shutdown unless result.continue?
  return result
end

Instance Method Details

#process(errback = nil, audit = nil, &block) ⇒ Object

Processes shutdown requests by communicating the need to shutdown an instance with the core agent, if necessary.

Parameters

errback(Proc)

error handler or nil

audit(Audit)

audit for shutdown action, if needed, or nil.

Block

block(Proc)

continuation block for successful handling of shutdown or nil

Return

always true



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/instance/shutdown_request.rb', line 147

def process(errback = nil, audit = nil, &block)
  # yield if not shutting down (continuing) or if already requested shutdown.
  if continue? || @shutdown_scheduled
    block.call if block
    return true
  end

  # ensure we have an audit, creating a temporary audit if necessary.
  sender = Sender.instance
  agent_identity = sender.identity
  if audit
    case @level
    when REBOOT, STOP, TERMINATE
      operation = "/forwarder/shutdown"
      payload = {:agent_identity => agent_identity, :kind => @level}
    else
      raise InvalidLevel.new("Unexpected shutdown level: #{@level.inspect}")
    end

    # request shutdown (kind indicated by operation and/or payload).
    audit.append_info("Shutdown requested: #{self}")
    sender.send_persistent_request(operation, payload) do |r|
      res = OperationResult.from_results(r)
      if res.success?
        @shutdown_scheduled = true
        block.call if block
      else
        fail(errback, audit, "Failed to shutdown instance", res)
      end
    end
  else
    AuditProxy.create(agent_identity, "Shutdown requested: #{self}") do |new_audit|
      process(errback, new_audit, &block)
    end
  end
  true
rescue Exception => e
  fail(errback, audit, e)
end