class Tengine::Core::Config::Core

Public Class Methods

[](hash_or_suite) click to toggle source

この辺は以前のTengine::Core::Configとの互換のために用意してあります

# File lib/tengine/core/config/core.rb, line 19
def [](hash_or_suite)
  case hash_or_suite
  when Tengine::Core::Config::Core then hash_or_suite
  when Hash then
    result = Tengine::Core::Config::Core.new
    result.load(hash_or_suite)
    result
  else
    raise "unsupported class: #{hash_or_suite.class.inspect}"
  end
end
default_hash() click to toggle source
# File lib/tengine/core/config/core.rb, line 31
def default_hash
  new.to_hash
end
Also aliased as: skelton_hash
new(hash_or_filepath = nil) click to toggle source
# File lib/tengine/core/config/core.rb, line 52
def initialize(hash_or_filepath = nil)
  build if respond_to?(:build)
  case hash_or_filepath
  when Hash then
    if config = hash_or_filepath[:config]
      load_file(config)
    else
      load(hash_or_filepath)
    end
  when String then load_file(hash_or_filepath)
  end
end
parse(args) click to toggle source
# File lib/tengine/core/config/core.rb, line 44
def parse(args)
  config = new
  config.parse!(args)
  config
end
parse_to_hash(args) click to toggle source
# File lib/tengine/core/config/core.rb, line 36
def parse_to_hash(args)
  config = new
  config.parse!(args)
  result = new
  result.config = config.config
  result.to_hash
end
skelton_hash()
Alias for: default_hash

Public Instance Methods

activation_dir() click to toggle source
# File lib/tengine/core/config/core.rb, line 287
def activation_dir
  @activation_dir ||= self[:tengined][:activation_dir]
end
build() click to toggle source
# File lib/tengine/core/config/core.rb, line 76
  def build
    banner <<EOS
Usage: tengined [-k action] [-f path_to_config] [-T path/to/file_or_dir]
         [-o mq_conn_host] [-p mq_conn_port] [-u mq_conn_user]
         [-s mq_conn_pass] [-e mq_exchange_name] [-q mq_queue_name]
EOS

    field(:action, "test|load|start|enable|stop|force-stop|status|activate", :type => :string, :default => "start")
    load_config(:config, "path/to/config_file", :type => :string)

    add(:process, Tengine::Core::Config::Core::Process)
    add(:tengined, Tengine::Core::Config::Core::Tengined)
    field(:db, "settings to connect to db", :type => :hash,
      :default => Tengine::Core::Config::DB::DEFAULT_SETTINGS)

    group(:event_queue, :hidden => true) do
      add(:connection, AmqpConnection)
      add(:exchange  , Tengine::Support::Config::Amqp::Exchange, :defaults => {:name => 'tengine_event_exchange'})
      add(:queue     , Tengine::Support::Config::Amqp::Queue   , :defaults => {:name => 'tengine_event_queue'})
    end

    add(:log_common, Tengine::Support::Config::Logger,
      :defaults => {
        :rotation      => 3          ,
        :rotation_size => 1024 * 1024,
        :level         => 'info'     ,
      })
    add(:application_log, Tengine::Core::Config::Core::LoggerConfig,
      :parameters => {:logger_name => "application"},
      :dependencies => { :process_config => :process, :log_common => :log_common,}){
      self.formatter = lambda{|level, t, prog, msg| "#{t.iso8601} #{level} #{@process_identifier} #{msg}\n"}
    }
    add(:process_stdout_log, Tengine::Core::Config::Core::LoggerConfig,
      :parameters => {:logger_name => "#{File.basename($PROGRAM_NAME)}_#{::Process.pid}_stdout"},
      :dependencies => { :process_config => :process, :log_common => :log_common,}){
      self.formatter = lambda{|level, t, prog, msg| "#{t.iso8601} STDOUT #{@process_identifier} #{msg}\n"}
    }
    add(:process_stderr_log, Tengine::Core::Config::Core::LoggerConfig,
      :parameters => {:logger_name => "#{File.basename($PROGRAM_NAME)}_#{::Process.pid}_stderr"},
      :dependencies => { :process_config => :process, :log_common => :log_common,},
      :defaults => {
        :output => proc{ process_config.daemon ? "./log/#{logger_name}.log" : "STDERR" }}){
      self.formatter = lambda{|level, t, prog, msg| "#{t.iso8601} STDERR #{@process_identifier} #{msg}\n"}
    }

    group(:heartbeat) do
      add(:core     , Tengine::Core::Config::Core::Heartbeat)
#       add(:job      , Tengine::Core::Config::Core::Heartbeat, :defaults => {:interval => 5, :expire => 20})
#       add(:hbw      , Tengine::Core::Config::Core::Heartbeat)
#       add(:resourcew, Tengine::Core::Config::Core::Heartbeat)
#       add(:atd      , Tengine::Core::Config::Core::Heartbeat)
    end

    separator("\nGeneral:")
    field(:verbose, "Show detail to this command", :type => :boolean)
    __action__(:version, "show version"){ STDOUT.puts Tengine::Core.version.to_s; exit }
    __action__(:dump_skelton, "dump skelton of config"){ STDOUT.puts YAML.dump(root.to_hash); exit }
    __action__(:help   , "show this help message"){ STDOUT.puts option_parser.help; exit }

    mapping({
        [:action] => :k,
        [:config] => :f,
        [:tengined, :daemon] => :D,
        [:tengined, :load_path] => :T,
        [:tengined, :cache_drivers] => :c,
        # [:tengined, :heartbeat_period] => :G,
        [:tengined, :confirmation_threshold] => :C,

        [:event_queue, :connection, :host] => :o,
        [:event_queue, :connection, :port] => :p,
        [:event_queue, :connection, :user] => :u,
        [:event_queue, :connection, :pass] => :s,
        [:event_queue, :exchange  , :name] => :e,
        [:event_queue, :queue     , :name] => :q,

        [:heartbeat, :core, :interval] => :G,

        [:verbose] => :V,
        [:version] => :v,
        [:help] => :h
      })
  end
confirmation_threshold() click to toggle source
# File lib/tengine/core/config/core.rb, line 291
def confirmation_threshold
  @confirmation_threshold ||= Tengine::Event::LEVELS_INV[ self[:tengined][:confirmation_threshold].to_sym ]
end
dsl_dir_path() click to toggle source
# File lib/tengine/core/config/core.rb, line 252
def dsl_dir_path
  prepare_dir_and_paths
  @dsl_dir_path
end
dsl_file_paths() click to toggle source
# File lib/tengine/core/config/core.rb, line 257
def dsl_file_paths
  prepare_dir_and_paths
  @dsl_file_paths
end
dsl_load_path(clear_cache = false) click to toggle source
# File lib/tengine/core/config/core.rb, line 223
def dsl_load_path(clear_cache = false)
  @dsl_load_path = nil if clear_cache
  unless @dsl_load_path
    original = self[:tengined][:load_path]
    # 本来は指定する必要はありませんが、specでDir.pwdをstubで返すようにするために、明示的に第2引数にDir.pwdを指定しています
    @dsl_load_path = original ? File.expand_path(original, Dir.pwd) : nil
  end
  @dsl_load_path
end
dsl_version() click to toggle source
# File lib/tengine/core/config/core.rb, line 270
def dsl_version
  unless @dsl_version
    path = dsl_version_path
    @dsl_version = (path && File.exist?(dsl_version_path)) ? File.read(dsl_version_path).strip : Time.now.strftime("%Y%m%d%H%M%S")
  end
  @dsl_version
end
dsl_version_path() click to toggle source
# File lib/tengine/core/config/core.rb, line 262
def dsl_version_path
  unless @dsl_version_path
    path = dsl_dir_path
    @dsl_version_path = path ? File.expand_path("VERSION", path) : nil
  end
  @dsl_version_path
end
heartbeat_enabled?() click to toggle source
# File lib/tengine/core/config/core.rb, line 300
def heartbeat_enabled?
  heartbeat_period > 0
end
heartbeat_period() click to toggle source
# File lib/tengine/core/config/core.rb, line 295
def heartbeat_period
  # [:][:heartbeat_period].to_i
  heartbeat.core.interval.to_i
end
load_file(filepath) click to toggle source
Calls superclass method
# File lib/tengine/core/config/core.rb, line 65
def load_file(filepath)
  super
rescue Exception => e
  msg = e.message
  unless msg.include?(filepath)
    msg = "#{msg} in #{filepath}"
  end
  raise Tengine::Core::ConfigError, msg
end
prepare_dir_and_paths(force = false) click to toggle source
# File lib/tengine/core/config/core.rb, line 233
def prepare_dir_and_paths(force = false)
  return if !force && @prepare_dir_and_paths_done
  path = dsl_load_path(true) # キャッシュをクリア
  if path.nil?
    @dsl_dir_path = nil
    @dsl_file_paths = []
  elsif Dir.exist?(path)
    @dsl_dir_path = File.expand_path(path)
    @dsl_file_paths = Dir.glob("#{@dsl_dir_path}/**/*.rb")
  elsif File.exist?(path)
    @dsl_dir_path = File.expand_path(File.dirname(path))
    @dsl_dir_path.force_encoding(@dsl_dir_path.encoding)
    @dsl_file_paths = [dsl_load_path]
  else
    raise Tengine::Core::ConfigError, "file or directory doesn't exist. #{path}"
  end
  @prepare_dir_and_paths_done = true
end
relative_path_from_dsl_dir(filepath) click to toggle source
# File lib/tengine/core/config/core.rb, line 278
def relative_path_from_dsl_dir(filepath)
  path = Pathname.new(filepath)
  path.relative? ? path.to_s : path.relative_path_from(Pathname.new(dsl_dir_path)).to_s
end
setup_loggers() click to toggle source
# File lib/tengine/core/config/core.rb, line 304
def setup_loggers
  Tengine.logger = application_log.new_logger
  Tengine::Core.stdout_logger = process_stdout_log.new_logger
  Tengine::Core.stderr_logger = process_stderr_log.new_logger

  Tengine::Core.stdout_logger.info("#{self.class.name}#setup_loggers complete")
rescue Exception
  Tengine::Core.stderr_logger.info("#{self.class.name}#setup_loggers failure")
  raise
end
status_dir() click to toggle source
# File lib/tengine/core/config/core.rb, line 283
def status_dir
  @status_dir ||= self[:tengined][:status_dir]
end