Class: Tengine::Core::Kernel

Inherits:
Object
  • Object
show all
Extended by:
MethodTraceable
Includes:
SelectableAttr::Base, EventExceptionReportable, KernelRuntime
Defined in:
lib/tengine/core/kernel.rb

Constant Summary collapse

HEARTBEAT_EVENT_TYPE_NAME =
"core.heartbeat.tengine".freeze
HEARTBEAT_ATTRIBUTES =
{
  :key => UUID.new.generate,
  :level => Tengine::Event::LEVELS_INV[:debug],
  :source_name => sprintf("process:%s/%d", ENV["MM_SERVER_NAME"], Process.pid),
  :sender_name => sprintf("process:%s/%d", ENV["MM_SERVER_NAME"], Process.pid),
  :retry_count => 0,
}.freeze

Constants included from EventExceptionReportable

EventExceptionReportable::EVENT_EXCEPTION_REPORTERS, EventExceptionReportable::FIRE_ALL, EventExceptionReportable::FIRE_EXCEPT_TESTING_ERROR, EventExceptionReportable::RAISE_ALL

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MethodTraceable

method_trace

Methods included from EventExceptionReportable

#report_on_exception, to_reporter

Methods included from KernelRuntime

#ack, #ack?, #ack_policies, #ack_policy_for, #add_ack_policy, #all_submitted?, #em_setup_blocks, #processing_event?, #safety_processing_headers, #submit

Constructor Details

#initialize(config) ⇒ Kernel

Returns a new instance of Kernel.



17
18
19
20
21
# File 'lib/tengine/core/kernel.rb', line 17

def initialize(config)
  @status = :initialized
  @config = config
  @processing_event = false
end

Instance Attribute Details

#after_delegateObject

Returns the value of attribute after_delegate.



15
16
17
# File 'lib/tengine/core/kernel.rb', line 15

def after_delegate
  @after_delegate
end

#before_delegateObject

Returns the value of attribute before_delegate.



15
16
17
# File 'lib/tengine/core/kernel.rb', line 15

def before_delegate
  @before_delegate
end

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

Class Method Details

.topObject



63
64
65
# File 'lib/tengine/core/kernel.rb', line 63

def self.top
  @top ||= eval("self", TOPLEVEL_BINDING)
end

Instance Method Details

#activateObject



115
116
117
118
119
120
121
122
123
124
# File 'lib/tengine/core/kernel.rb', line 115

def activate
  EM.run do
    setup_mq_connection
    subscribe_queue do
      enable_heartbeat
      yield(mq) if block_given? # このyieldは接続テストのための処理をTengine::Core:Bootstrapが定義するのに使われます。
      em_setup_blocks.each{|block| block.call }
    end
  end
end

#bindObject



86
87
88
89
90
91
# File 'lib/tengine/core/kernel.rb', line 86

def bind
  # dsl_context.__evaluate__
  # Tengine::Core::stdout_logger.debug("Hanlder bindings:\n" << dsl_context.to_a.inspect)
  # Tengine::Core::HandlerPath.default_driver_version = config.dsl_version
  Tengine::Core::HandlerPath.default_driver_version = config.dsl_version
end

#dsl_contextObject Also known as: context



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tengine/core/kernel.rb', line 67

def dsl_context
  unless @dsl_context
    top = self.class.top
    top.singleton_class.module_eval do
      include Tengine::Core::DslLoader
    end
    top.__kernel__ = self
    top.config = config
    @dsl_context = top
  end
  @dsl_context
end

#enable_heartbeatObject



250
251
252
253
254
255
256
257
258
259
260
# File 'lib/tengine/core/kernel.rb', line 250

def enable_heartbeat
  n = config[:heartbeat][:core][:interval].to_i
  if n and n > 0
    EM.defer do
      @heartbeat_timer = EM.add_periodic_timer(n) do
        Tengine::Core.stdout_logger.debug("sending heartbeat") if config[:verbose]
        sender.fire(HEARTBEAT_EVENT_TYPE_NAME, HEARTBEAT_ATTRIBUTES.dup)
      end
    end
  end
end

#evaluateObject



81
82
83
84
# File 'lib/tengine/core/kernel.rb', line 81

def evaluate
  Tengine::Core::Driver.delete_all_with_handler_paths(dsl_context.config.dsl_version)
  dsl_context.__evaluate__
end

#fire(*args, &block) ⇒ Object



262
263
264
# File 'lib/tengine/core/kernel.rb', line 262

def fire(*args, &block)
  sender.fire(*args, &block)
end

#mqObject



274
275
276
# File 'lib/tengine/core/kernel.rb', line 274

def mq
  @mq ||= Tengine::Mq::Suite.new(config[:event_queue])
end

#process_message(headers, msg) ⇒ true, それ以外

Returns:

  • (true)

    メッセージはイベントストアに保存された

  • (それ以外)

    メッセージは保存されなかった。



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/tengine/core/kernel.rb', line 143

def process_message(headers, msg)
  safety_processing_event(headers) do
    raw_event = parse_event(msg)
    if raw_event.nil?
      headers.ack
      return false
    end
    if raw_event.key.blank?
      Tengine.logger.warn("invalid event which has blank key: #{raw_event.inspect}")
      headers.ack
      return
    end

    delay = ((ENV['TENGINED_EVENT_DEBUG_DELAY'] || '0').to_f || 0.0)
    sleep delay

    begin
      # ハートビートは *保存より前に* 特別扱いが必要
      event = case raw_event.event_type_name
              when /finished\.process\.([^.]+)\.tengine$/
                save_heartbeat_ok(raw_event)
              when /expired\.([^.]+)\.heartbeat\.tengine$/
                save_heartbeat_ng(raw_event)
              when /heartbeat\.tengine$/ # when の順番に注意
                save_heartbeat_beat(raw_event)
              when /(alert|stop)\.execution\.job\.tengine$/
                save_scheduling_event(raw_event)
              when /\.failed\.tengined$/
                save_failed_event(raw_event)
              else
                save_event(raw_event)
              end

    rescue Moped::Errors::OperationFailure, Mongoid::Errors::Validations => e
      Tengine.logger.warn("failed to store an event.\n[#{e.class.name}] #{e.message}")
      event = nil
    rescue Exception => e
      Tengine.logger.error("failed to save an event #{raw_event.inspect}\n[#{e.class.name}] #{e.message}")
      event = nil
    end

    unless event
      # Model.exists?だと上手くいかない時があるのでModel.whereを使っています
      # fire_failed_event(raw_event) if Tengine::Core::Event.exists?(confitions: { key: raw_event.key, sender_name: raw_event.sender_name })

      begin
        n = Tengine::Core::Event.where(:key => raw_event.key, :sender_name => raw_event.sender_name).count
      rescue Moped::Errors::ConnectionFailure, Moped::Errors::OperationFailure => e
        Tengine.logger.error("giving up processing an event due to #{e} (#{e.class.name})")
        n = 0
      end

      if n > 0
        fire_failed_event(raw_event)
        headers.ack
      else
        Tengine.logger.info("requeue an event #{raw_event.inspect}")
        headers.reject(:requeue => true)
      end
      return false
    end
    event.kernel = self

    begin
      ack_policy = ack_policy_for(event)
      safety_processing_headers(headers, event, ack_policy) do
        ack if ack_policy == :at_first

        # ドライバを再ロードするならハンドラを検索する前に行います。
        unless config.tengined.cache_drivers
          Tengine::Core::Driveable.__remember_session_ids_for_drivers__(config.dsl_version)
          begin
            Tengine::Core::Driver.delete_all_with_handler_paths(dsl_context.config.dsl_version)
            ActiveSupport::Dependencies.clear
            evaluate
          ensure
            Tengine::Core::Driveable.__forget_session_ids_for_drivers__
          end
        end

        handlers = find_handlers(event)
        safty_handlers(handlers) do
          delegate(event, handlers)
          ack if 
        end
        headers.reject(:requeue => true) unless ack?
      end
      close_if_shutting_down
      true
    rescue Moped::Errors::ConnectionFailure, Moped::Errors::OperationFailure => e
      Tengine.logger.error("giving up processing an event due to #{e} (#{e.class.name})")
      Tengine.logger.info("requeue an event #{raw_event.inspect}")
      headers.reject(:requeue => true)
    end
  end
end

#senderObject



266
267
268
269
270
271
272
# File 'lib/tengine/core/kernel.rb', line 266

def sender
  unless @sender
    @sender = Tengine::Event::Sender.new(mq)
    @sender.default_keep_connection = true
  end
  @sender
end

#startObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tengine/core/kernel.rb', line 23

def start
  if block_given?
    block = Proc.new
  else
    block = Proc.new do
      self.stop
    end
  end
  update_status(:starting)
  bind
  if config[:tengined][:wait_activation]
    update_status(:waiting_activation)
    wait_for_activation(&block)
  else
    require 'profile' if ENV['tengined_profile']
    activate(&block)
  end
end

#stop(force = false) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tengine/core/kernel.rb', line 42

def stop(force = false)
  if self.status == :running
    update_status(:shutting_down)
    mq.initiate_termination do
      mq.unsubscribe do
        EM.cancel_timer(@heartbeat_timer) if @heartbeat_timer
        send_last_event do
          close_if_shutting_down do
            update_status(:terminated)
            EM.stop
            yield if block_given?
          end
        end
      end
    end
  else
    update_status(:shutting_down)
    # wait_for_actiontion中の処理を停止させる必要がある
  end
end

#subscribe_queueObject

subscribe to messages in the queue



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/tengine/core/kernel.rb', line 127

def subscribe_queue
  confirm = proc do |*|
    # queueへの接続までできたら稼働中
    # self.status_key = :running if mq.queue
    update_status(:running)
    yield if block_given?
  end
  mq.subscribe(:ack => true, :nowait => false, :confirm => confirm) do |headers, msg|
    process_message(headers, msg)
    # headers.ack
    # Tengine.logger.debug("headers: #{headers.inspect}\n#{msg}")
  end
end

#wait_for_activation(&block) ⇒ Object



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

def wait_for_activation(&block)
  activated = false
  activation_file_name = "#{config[:tengined][:activation_dir]}\/tengined_#{Process.pid}.activation"
  start_time = Time.now
  while((Time.now - start_time).to_i <= config[:tengined][:activation_timeout].to_i) do
    if File.exist?(activation_file_name)
      # ファイルが見つかった
      activated = true
      break
    end
    sleep 1
  end
  if activated
    File.delete(activation_file_name)
    # activate開始
    activate(&block)
  else
    update_status(:shutting_down)
    raise Tengine::Core::ActivationTimeoutError, "activation file found timeout error."
  end
end