Class: Tengine::Core::Config::Core

Inherits:
Support::Config::Definition::Suite
  • Object
show all
Defined in:
lib/tengine/core/config/core.rb

Defined Under Namespace

Classes: AmqpConnection, Heartbeat, LoggerConfig, Process, Tengined

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash_or_filepath = nil) ⇒ Core

Returns a new instance of Core.



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tengine/core/config/core.rb', line 53

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

Class Method Details

.[](hash_or_suite) ⇒ Object

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



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tengine/core/config/core.rb', line 20

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_hashObject Also known as: skelton_hash



32
33
34
# File 'lib/tengine/core/config/core.rb', line 32

def default_hash
  new.to_hash
end

.parse(args) ⇒ Object



45
46
47
48
49
# File 'lib/tengine/core/config/core.rb', line 45

def parse(args)
  config = new
  config.parse!(args)
  config
end

.parse_to_hash(args) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/tengine/core/config/core.rb', line 37

def parse_to_hash(args)
  config = new
  config.parse!(args)
  result = new
  result.config = config.config
  result.to_hash
end

Instance Method Details

#activation_dirObject



288
289
290
# File 'lib/tengine/core/config/core.rb', line 288

def activation_dir
  @activation_dir ||= self[:tengined][:activation_dir]
end

#buildObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/tengine/core/config/core.rb', line 77

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_thresholdObject



292
293
294
# File 'lib/tengine/core/config/core.rb', line 292

def confirmation_threshold
  @confirmation_threshold ||= Tengine::Event::LEVELS_INV[ self[:tengined][:confirmation_threshold].to_sym ]
end

#dsl_dir_pathObject



253
254
255
256
# File 'lib/tengine/core/config/core.rb', line 253

def dsl_dir_path
  prepare_dir_and_paths
  @dsl_dir_path
end

#dsl_file_pathsObject



258
259
260
261
# File 'lib/tengine/core/config/core.rb', line 258

def dsl_file_paths
  prepare_dir_and_paths
  @dsl_file_paths
end

#dsl_load_path(clear_cache = false) ⇒ Object



224
225
226
227
228
229
230
231
232
# File 'lib/tengine/core/config/core.rb', line 224

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_versionObject



271
272
273
274
275
276
277
# File 'lib/tengine/core/config/core.rb', line 271

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_pathObject



263
264
265
266
267
268
269
# File 'lib/tengine/core/config/core.rb', line 263

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?Boolean

Returns:

  • (Boolean)


301
302
303
# File 'lib/tengine/core/config/core.rb', line 301

def heartbeat_enabled?
  heartbeat_period > 0
end

#heartbeat_periodObject



296
297
298
299
# File 'lib/tengine/core/config/core.rb', line 296

def heartbeat_period
  # [:][:heartbeat_period].to_i
  heartbeat.core.interval.to_i
end

#load_file(filepath) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/tengine/core/config/core.rb', line 66

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) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/tengine/core/config/core.rb', line 234

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) ⇒ Object



279
280
281
282
# File 'lib/tengine/core/config/core.rb', line 279

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_loggersObject



305
306
307
308
309
310
311
312
313
314
# File 'lib/tengine/core/config/core.rb', line 305

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_dirObject



284
285
286
# File 'lib/tengine/core/config/core.rb', line 284

def status_dir
  @status_dir ||= self[:tengined][:status_dir]
end