class Trinidad::Lifecycle::Host

Host listener - monitors deployed applications (re-invented HostConfig with Ruby/Rack semantics).

Constants

RELOAD_STRATEGIES

Attributes

app_holders[R]
contexts[R]
server[R]

Public Class Methods

new(server, *app_holders) click to toggle source

server current server instance app_holders deployed web application holders

# File lib/trinidad/lifecycle/host.rb, line 19
def initialize(server, *app_holders)
  app_holders.map! do |app_holder|
    if app_holder.is_a?(Hash) # backwards compatibility
      Trinidad::WebApp::Holder.new(app_holder[:app], app_holder[:context])
    else
      app_holder
    end
  end
  @server, @app_holders = server, app_holders
end

Public Instance Methods

before_start(event) click to toggle source
# File lib/trinidad/lifecycle/host.rb, line 43
def before_start(event)
  init_monitors
end
periodic(event) click to toggle source
# File lib/trinidad/lifecycle/host.rb, line 49
def periodic(event)
  check_changes event.lifecycle
end

Protected Instance Methods

check_changes(host) click to toggle source
# File lib/trinidad/lifecycle/host.rb, line 59
def check_changes(host)
  check_monitors
end
check_monitors() click to toggle source
# File lib/trinidad/lifecycle/host.rb, line 78
def check_monitors
  app_holders.each do |app_holder|
    # double check monitor, capistrano removes it temporarily
    unless File.exist?(monitor = app_holder.monitor)
      sleep(0.5)
      next unless File.exist?(monitor)
    end
    
    mtime = File.mtime(monitor)
    if mtime > app_holder.monitor_mtime && app_holder.try_lock
      app_holder.monitor_mtime = mtime
      app_holder.unlock if reload_application!(app_holder)
    end
  end
end
init_monitors() click to toggle source
# File lib/trinidad/lifecycle/host.rb, line 63
def init_monitors
  app_holders.each do |app_holder|
    monitor = app_holder.monitor
    opts = 'w+'
    if ! File.exist?(dir = File.dirname(monitor))
      Dir.mkdir dir
    elsif File.exist?(monitor)
      opts = 'r'
    end
    File.open(monitor, opts) do |file|
      app_holder.monitor_mtime = file.mtime
    end
  end
end
reload_application!(app_holder) click to toggle source
# File lib/trinidad/lifecycle/host.rb, line 103
def reload_application!(app_holder)
  strategy = (app_holder.web_app.reload_strategy || :default).to_sym
  strategy = RELOAD_STRATEGIES[ strategy ]
  strategy = strategy ? self.class.const_get(strategy) : RestartReload
  new_args = []
  new_args << server if strategy.instance_method(:initialize).arity != 0
  strategy.new(*new_args).reload!(app_holder)
end