Class: Tengine::Core::Kernel
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
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
method_trace
#report_on_exception, to_reporter
#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_delegate ⇒ Object
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_delegate ⇒ Object
Returns the value of attribute before_delegate.
15
16
17
|
# File 'lib/tengine/core/kernel.rb', line 15
def before_delegate
@before_delegate
end
|
#config ⇒ Object
Returns the value of attribute config.
14
15
16
|
# File 'lib/tengine/core/kernel.rb', line 14
def config
@config
end
|
#status ⇒ Object
Returns the value of attribute status.
14
15
16
|
# File 'lib/tengine/core/kernel.rb', line 14
def status
@status
end
|
Class Method Details
.top ⇒ Object
63
64
65
|
# File 'lib/tengine/core/kernel.rb', line 63
def self.top
@top ||= eval("self", TOPLEVEL_BINDING)
end
|
Instance Method Details
#activate ⇒ Object
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? em_setup_blocks.each{|block| block.call }
end
end
end
|
#dsl_context ⇒ Object
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_heartbeat ⇒ Object
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
|
#fire(*args, &block) ⇒ Object
262
263
264
|
# File 'lib/tengine/core/kernel.rb', line 262
def fire(*args, &block)
sender.fire(*args, &block)
end
|
#mq ⇒ Object
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, それ以外
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(, msg)
safety_processing_event() do
raw_event = parse_event(msg)
if raw_event.nil?
.ack
return false
end
if raw_event.key.blank?
Tengine.logger.warn("invalid event which has blank key: #{raw_event.inspect}")
.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$/ 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
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)
.ack
else
Tengine.logger.info("requeue an event #{raw_event.inspect}")
.reject(:requeue => true)
end
return false
end
event.kernel = self
begin
ack_policy = ack_policy_for(event)
(, 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 all_submitted?
end
.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}")
.reject(:requeue => true)
end
end
end
|
#sender ⇒ Object
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
|
#start ⇒ Object
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)
end
end
|
#subscribe_queue ⇒ Object
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 |*|
update_status(:running)
yield if block_given?
end
mq.subscribe(:ack => true, :nowait => false, :confirm => confirm) do |, msg|
process_message(, 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(&block)
else
update_status(:shutting_down)
raise Tengine::Core::ActivationTimeoutError, "activation file found timeout error."
end
end
|