class Citrus::Application

Application

Attributes

base[R]
components[R]
cur_server[R]
env[R]
master[R]
modules_registered[R]
server_id[R]
server_type[R]
servers_map[R]
start_id[R]
type[R]

Public Class Methods

new(args={}) click to toggle source

Initialize the application

@param [Hash] args Option

# File lib/citrus/application.rb, line 23
def initialize args={}
  @env = nil
  @type = nil
  @start_id = nil

  @base = args[:base] || Dir.pwd
  @settings = {}

  # current server info
  @cur_server = nil
  @server_id = nil
  @server_type = nil
  @start_time = nil

  # global server info
  @master = nil
  @servers = {}
  @servers_map = {}
  @server_types = []
  @server_type_maps = {}

  # lifecycle callbacks
  @lifecycle_cbs = {}

  @components = {}
  @modules_registered = {}

  default_configuration

  @state = :state_inited

  # singleton pattern
  eval %Q{
    class ::Citrus::Application
      private_class_method :new
    end
  }
end

Public Instance Methods

add_servers(sinfos) click to toggle source

Add new servers at runtime

@param [Array] sinfos

# File lib/citrus/application.rb, line 187
def add_servers sinfos
  return unless sinfos && !sinfos.empty?
  sinfos.each { |sinfo|
    # update global server map
    @servers[sinfo[:server_id]] = sinfo

    # update global server type map
    slist = @server_type_maps[sinfo[:server_type]] ||= []
    replace_server slist, sinfo

    # update global server type list
    if !@server_types.member? sinfo[:server_type]
      @server_types << sinfo[:server_type]
    end
  }
  emit 'add_servers', sinfos
end
after_start() { |runtime_error 'application is not running now'| ... } click to toggle source

Lifecycle callback for after start

# File lib/citrus/application.rb, line 106
def after_start &block
  if @state != :state_started
    block_given? and yield RuntimeError.new 'application is not running now'
    return
  end

  opt_components(@components.values, :after_start) { |err|
    if after_cb = @lifecycle_cbs[:after_start]
      after_cb.call self, &proc{
        block_given? and yield err
      }
    else
      block_given? and yield err
    end
  }
  puts used_time = Time.now.to_f - @start_time
end
backend?(server=nil) click to toggle source

Check whether a server is a backend

@param [Object] server

# File lib/citrus/application.rb, line 175
def backend? server=nil
  not frontend? server
end
configure(*args, &block) click to toggle source

Configure callback for the specified env and server type

# File lib/citrus/application.rb, line 147
def configure *args, &block
  args.length > 0 ? env = args[0].to_s : env = 'all'
  args.length > 1 ? server_type = args[1].to_s : server_type = 'all'
  if env == 'all' || contains(@env.to_s, env)
    if server_type == 'all' || contains(@server_type, server_type)
      instance_eval &block if block
    end
  end
  return self
end
frontend?(server=nil) click to toggle source

Check whether a server is a frontend

@param [Object] server

# File lib/citrus/application.rb, line 168
def frontend? server=nil
  server ? server[:frontend] == true : @cur_server[:frontend] == true
end
get(setting) click to toggle source

Get property from setting

@param [Symbol] setting

# File lib/citrus/application.rb, line 142
def get setting
  @settings[setting]
end
get_servers_by_type(type) click to toggle source

Get server infos by server type

@param [String] type

# File lib/citrus/application.rb, line 161
def get_servers_by_type type
  @server_type_maps[type]
end
master?() click to toggle source

Check whether a server is a master

# File lib/citrus/application.rb, line 180
def master?
  @server_type == 'master'
end
remove_server(server_id) click to toggle source

Remove server at runtime

@param [String] server_id

# File lib/citrus/application.rb, line 220
def remove_server server_id
end
remove_servers(server_ids) click to toggle source

Remove servers at runtime

@param [Array] server_ids

# File lib/citrus/application.rb, line 208
def remove_servers server_ids
end
replace_server(slist, sinfo) click to toggle source

Replace server at runtime

@param [Array] slist @param [Hash] server_info

# File lib/citrus/application.rb, line 227
def replace_server slist, sinfo
  slist.each_with_index { |s, index|
    if s[:server_id] == sinfo[:server_id]
      slist[index] = sinfo 
      return
    end
  }
  slist << sinfo
end
replace_servers(sinfos) click to toggle source

Replace servers at runtime

@param [Array] sinfos

# File lib/citrus/application.rb, line 214
def replace_servers sinfos
end
set(setting, value) click to toggle source

Assign ‘setting` to `value`

@param [Symbol] setting @param [Object] value

# File lib/citrus/application.rb, line 134
def set setting, value
  @settings[setting] = value
  return self
end
start() { |exception 'application double start'| ... } click to toggle source

Start the application

# File lib/citrus/application.rb, line 63
def start &block
  @start_time = Time.now.to_f
  if @state != :state_inited
    block_given? and yield Exception.new 'application double start'
    return
  end

  if @start_id
    if @start_id != 'master'
      Starter.run_servers self
      return
    end
  else
    if @type && @type != :all && @type != :master
      Starter.run_servers self
      return
    end
  end

  load_default_components

  if before_cb = @lifecycle_cbs[:before_startup]
    before_cb.call self, &proc{
      start_up &block
    }
  else
    start_up &block
  end
end
start_up() { |err| ... } click to toggle source

Start up

# File lib/citrus/application.rb, line 94
def start_up &block
  opt_components(@components.values, :start) { |err|
    @state = :state_started
    if err
      block_given? and yield err
    else
      after_start &block
    end
  }
end
stop(force=false) click to toggle source

Stop components

# File lib/citrus/application.rb, line 127
def stop force=false
end