Class: Tengine::Core::Bootstrap

Inherits:
Object
  • Object
show all
Extended by:
MethodTraceable
Defined in:
lib/tengine/core/bootstrap.rb

Constant Summary collapse

DEBUG_CONFIG_ATTRS =
[:dsl_dir_path, :dsl_file_paths, :dsl_version_path, :dsl_version].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MethodTraceable

method_trace

Constructor Details

#initialize(hash) ⇒ Bootstrap

Returns a new instance of Bootstrap.



13
14
15
16
# File 'lib/tengine/core/bootstrap.rb', line 13

def initialize(hash)
  @config = Tengine::Core::Config::Core[hash]
  prepare_trap
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



10
11
12
# File 'lib/tengine/core/bootstrap.rb', line 10

def config
  @config
end

#kernelObject



51
52
53
# File 'lib/tengine/core/bootstrap.rb', line 51

def kernel
  @kernel ||= Tengine::Core::Kernel.new(config)
end

Instance Method Details

#bootObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tengine/core/bootstrap.rb', line 22

def boot
  Tengine::Core.stdout_logger.debug(DEBUG_CONFIG_ATTRS.map{|attr| "#{attr}: " << config.send(attr).inspect}.join(", "))
  case config[:action]
  when "load" then load_dsl
  when "start" then
    load_dsl # unless config[:tengined][:skip_load]
    start_kernel do
      # ここでイベントを待つ
    end
  when "test" then test_connection
  when "enable" then enable_drivers
  else
    raise ArgumentError, "config[:action] in boot method must be test|load|start|enable but was #{config[:action]} "
  end
end

#enable_driversObject



67
68
69
70
# File 'lib/tengine/core/bootstrap.rb', line 67

def enable_drivers
  drivers = Tengine::Core::Driver.where(:version => config.dsl_version, :enabled_on_activation => true)
  drivers.each{ |d| d.update_attribute(:enabled, true) }
end

#load_dslObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tengine/core/bootstrap.rb', line 38

def load_dsl
  if dsl_version_document = Tengine::Core::Setting.where(:name => "dsl_version").first
    dsl_version_document.value = config.dsl_version
    dsl_version_document.save!
  else
    Tengine::Core::Setting.create!(:name => "dsl_version", :value => config.dsl_version)
  end
  Tengine.plugins.notify(self, :load_dsl) do
    context = kernel.dsl_context
    context.__evaluate__
  end
end

#prepare_trapObject



18
# File 'lib/tengine/core/bootstrap.rb', line 18

def prepare_trap; Signal.trap(:HUP) { kernel.stop } end

#start_connection_test(mq) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/tengine/core/bootstrap.rb', line 94

def start_connection_test(mq)
  require 'timeout'
  timeout(10) do
    connection_test_completed = false
    Tengine.callback_for_test = lambda do |event_type_name|
      case event_type_name
      when :foo then
        Tengine::Core.stdout_logger.info("handing :foo successfully.")
      when :bar then
        Tengine::Core.stdout_logger.info("handing :bar successfully.")
        connection_test_completed = true
      else
        Tengine::Core.stderr_logger.error("Unexpected event: #{event_type_name}")
      end
    end
    Tengine::Event.instance_variable_set(:@mq_suite, mq)
    Tengine::Event.fire(:foo, :level_key => :info, :keep_connection => true)
    loop do
      sleep(0.1)
      return if connection_test_completed
    end
  end
end

#start_kernel(&block) ⇒ Object



55
56
57
58
59
# File 'lib/tengine/core/bootstrap.rb', line 55

def start_kernel(&block)
  Tengine.plugins.notify(self, :start_kernel) do
    kernel.start(&block)
  end
end

#stop_kernelObject



61
62
63
64
65
# File 'lib/tengine/core/bootstrap.rb', line 61

def stop_kernel
  Tengine.plugins.notify(self, :stop_kernel) do
    kernel.stop
  end
end

#test_connectionObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tengine/core/bootstrap.rb', line 72

def test_connection
  config[:tengined][:load_path] = File.expand_path("connection_test/fire_bar_on_foo.rb", File.dirname(__FILE__))
  config.prepare_dir_and_paths(true)

  begin
    load_dsl
    start_kernel do |mq| # このブロックは Tengine::Core::Kernel#activateのEM.runに渡されたブロックから呼び出されます。
      teardown = lambda do |result|
        EM.next_tick do
          Tengine::Core.stdout_logger.info(result)
          stop_kernel
        end
      end
      # http://keijinsonyaban.blogspot.com/2010/12/eventmachine.html のEM.defer(op, callback)を参照
      EM.defer(lambda{start_connection_test(mq)}, teardown)
    end
    Tengine::Core::stdout_logger.info("Connection test success.")
  rescue Exception => e
    Tengine::Core::stderr_logger.error("Connection test failure: [#{e.class.name}] #{e.message}")
  end
end