Class: ASIR::Main
- Inherits:
-
Object
show all
- Defined in:
- lib/asir/main.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize ⇒ Main
Returns a new instance of Main.
27
28
29
30
31
|
# File 'lib/asir/main.rb', line 27
def initialize
self.env = ASIR::Environment.new
self.progname = File.basename($0)
self.exit_code = 0
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sel, *args) ⇒ Object
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
|
# File 'lib/asir/main.rb', line 102
def method_missing sel, *args
log "method_missing #{sel}" if verbose >= 3
case sel.to_s
when /^start_([^_]+)_worker!$/
_start_worker!
when /^status_([^_]+)_([^_]+)!$/
pid = server_pid
puts "#{log_str} pid #{pid}"
system("ps -fw -p #{pid}")
when /^log_([^_]+)_([^_]+)!$/
puts log_file
when /^taillog_([^_]+)_([^_]+)!$/
exec "tail -f #{log_file.inspect}"
when /^pid_([^_]+)_([^_]+)!$/
pid = server_pid rescue nil
alive = process_running? pid
puts "#{pid_file} #{pid || :NA} #{alive}"
when /^alive_([^_]+)_([^_]+)!$/
pid = server_pid rescue nil
alive = process_running? pid
puts "#{pid_file} #{pid || :NA} #{alive}" if @verbose
self.exit_code += 1 unless alive
when /^stop_([^_]+)_([^_]+)!$/
kill_server!
else
super
end
end
|
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
7
8
9
|
# File 'lib/asir/main.rb', line 7
def args
@args
end
|
#env ⇒ Object
Returns the value of attribute env.
7
8
9
|
# File 'lib/asir/main.rb', line 7
def env
@env
end
|
#exit_code ⇒ Object
Returns the value of attribute exit_code.
7
8
9
|
# File 'lib/asir/main.rb', line 7
def exit_code
@exit_code
end
|
#force ⇒ Object
22
23
24
|
# File 'lib/asir/main.rb', line 22
def force
@force
end
|
#progname ⇒ Object
Returns the value of attribute progname.
20
21
22
|
# File 'lib/asir/main.rb', line 20
def progname
@progname
end
|
#transport ⇒ Object
Transport selected from asir.phase = :transport.
25
26
27
|
# File 'lib/asir/main.rb', line 25
def transport
@transport
end
|
Instance Method Details
#_create_transport(default_class) ⇒ Object
275
276
277
278
279
280
281
282
283
|
# File 'lib/asir/main.rb', line 275
def _create_transport default_class
config!(:environment)
case transport = config!(:transport)
when default_class
self.transport = transport
else
raise "Expected config to return a #{default_class}, not a #{transport.class}"
end
end
|
#_run_transport_server!(wid = 0) ⇒ Object
311
312
313
314
315
316
317
318
319
320
321
322
323
|
# File 'lib/asir/main.rb', line 311
def _run_transport_server! wid = 0
log "running transport worker #{transport.class} #{wid}"
config!(:start)
$0 += " #{wid} #{transport.uri rescue nil}"
old_arg0 = $0.dup
after_receive_message = transport.after_receive_message || lambda { | transport, message | nil }
transport.after_receive_message = lambda do | transport, message |
$0 = "#{old_arg0} #{transport.message_count} #{message.identifier}"
after_receive_message.call(transport, message)
end
transport.run_server!
self
end
|
#_run_verb! ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/asir/main.rb', line 86
def _run_verb!
sel = :"#{verb}_#{adjective}_#{object}!"
if verbose >= 3
$stderr.puts " verb = #{verb.inspect}"
$stderr.puts " adjective = #{adjective.inspect}"
$stderr.puts " object = #{object.inspect}"
$stderr.puts " sel = #{sel.inspect}"
end
send(sel)
rescue ::Exception => exc
$stderr.puts "#{log_str} ERROR\n#{exc.inspect}\n #{exc.backtrace * "\n "}"
self.exit_code += 1
raise
nil
end
|
#_run_workers! ⇒ Object
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
# File 'lib/asir/main.rb', line 289
def _run_workers!
$0 = "#{progname} #{adjective} #{object} #{identifier}"
worker_id = 0
transport.prepare_server!
worker_processes = transport[:worker_processes] || 1
(worker_processes - 1).times do
wid = worker_id += 1
pid = Process.fork do
_run_transport_server! wid
end
Process.setgprp(pid, 0) rescue nil
worker_pids[wid] = pid
log "forked #{wid} pid #{pid}"
end
_run_transport_server!
ensure
log "worker 0 stopped"
_stop_workers!
end
|
#_start_conduit! ⇒ Object
185
186
187
188
189
190
191
|
# File 'lib/asir/main.rb', line 185
def _start_conduit!
config!(:environment)
self.transport = config!(:transport)
fork_server! do
transport.start_conduit! :fork => false
end
end
|
#_start_worker!(type = adjective) ⇒ Object
193
194
195
196
197
198
199
200
201
202
203
|
# File 'lib/asir/main.rb', line 193
def _start_worker! type = adjective
log "start_worker! #{type}"
type = type.to_s
fork_server! do
transport_file = "asir/transport/#{type}"
log "loading #{transport_file}"
require transport_file
_create_transport ASIR::Transport.const_get(type[0..0].upcase + type[1..-1])
_run_workers!
end
end
|
#_stop_workers! ⇒ Object
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
# File 'lib/asir/main.rb', line 325
def _stop_workers!
workers = worker_pids.dup
worker_pids.clear
workers.each do | wid, pid |
config!(:stop)
stop_pid! pid, "wid #{wid} "
end
workers.each do | wid, pid |
wr = Process.waitpid(pid) rescue nil
log "stopped #{wid} pid #{pid} => #{wr.inspect}", :stderr
end
ensure
worker_pids.clear
end
|
#config!(*args) ⇒ Object
51
52
53
|
# File 'lib/asir/main.rb', line 51
def config! *args
@env.config! *args
end
|
#fork_server!(cmd = nil, &blk) ⇒ Object
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
# File 'lib/asir/main.rb', line 205
def fork_server! cmd = nil, &blk
pid = Process.fork do
run_server! cmd, &blk
end
log "forked pid #{pid}"
Process.detach(pid)
File.open(pid_file, "w+") { | o | o.puts pid }
File.chmod(0666, pid_file) rescue nil
sleep 3
unless process_running? pid
raise "Server process #{pid} died to soon?"
end
self
end
|
#kill_server! ⇒ Object
252
253
254
255
256
257
258
259
|
# File 'lib/asir/main.rb', line 252
def kill_server!
log "#{log_str} kill"
pid = server_pid
stop_pid! pid
rescue ::Exception => exc
log "#{log_str} ERROR\n#{exc.inspect}\n #{exc.backtrace * "\n "}", :stderr
raise
end
|
#log(msg, to_stderr = false) ⇒ Object
261
262
263
264
265
266
267
268
|
# File 'lib/asir/main.rb', line 261
def log msg, to_stderr = false
if to_stderr
$stderr.puts "#{log_str_no_time} #{msg}"
end
File.open(log_file, "a+") do | log |
log.puts "#{log_str} #{msg}"
end
end
|
#log_str ⇒ Object
55
56
57
|
# File 'lib/asir/main.rb', line 55
def log_str
"#{Time.now.gmtime.iso8601(4)} #{$$} #{log_str_no_time}"
end
|
#log_str_no_time ⇒ Object
59
60
61
|
# File 'lib/asir/main.rb', line 59
def log_str_no_time
"#{progname} #{verb} #{adjective} #{object} #{identifier}"
end
|
#parse_args!(args = ARGV.dup) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/asir/main.rb', line 33
def parse_args! args = ARGV.dup
self.args = args
until args.empty?
case args.first
when /^([a-z0-9_]+=)(.*)/i
k, v = $1.to_sym, $2
args.shift
v = v.to_i if v == v.to_i.to_s
send(k, v)
else
break
end
end
self.verb, self.adjective, self.object, self.identifier = args.map{|x| x.to_sym}
self.identifier ||= :'0'
self
end
|
#process_running?(pid) ⇒ Boolean
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
# File 'lib/asir/main.rb', line 358
def process_running? pid
case pid
when false, nil
pid
when Integer
Process.kill(0, pid)
else
raise TypeError, "expected false, nil, Integer; given #{pid.inspect}"
end
true
rescue ::Errno::ESRCH
false
rescue ::Exception => exc
$stderr.puts " DEBUG: process_running? #{pid} => #{exc.inspect}"
false
end
|
#run! ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/asir/main.rb', line 63
def run!
unless verb && adjective && object
self.exit_code = 1
return usage!
end
config!(:configure)
case self.verb
when :restart
self.verb = :stop
_run_verb! && sleep(1)
self.verb = :start
_run_verb!
else
_run_verb!
end
self
rescue ::Exception => exc
$stderr.puts "#{log_str} ERROR\n#{exc.inspect}\n #{exc.backtrace * "\n "}"
self.exit_code += 1
self
end
|
#run_server!(cmd = nil) ⇒ Object
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
# File 'lib/asir/main.rb', line 223
def run_server! cmd = nil
lf = File.open(log_file, "a+")
File.chmod(0666, log_file) rescue nil
$stdin.close rescue nil
STDIN.close rescue nil
STDOUT.reopen(lf)
$stdout.reopen(lf) if $stdout.object_id != STDOUT.object_id
STDERR.reopen(lf)
$stderr.reopen(lf) if $stderr.object_id != STDERR.object_id
lf.puts "#{log_str} starting pid #{$$}"
begin
if cmd
exec(cmd)
else
yield
end
ensure
lf.puts "#{log_str} finished pid #{$$}"
File.unlink(pid_file) rescue nil
end
self
rescue ::Exception => exc
msg = "ERROR pid #{$$}\n#{exc.inspect}\n #{exc.backtrace * "\n "}"
log msg, :stderr
raise
self
end
|
#server_pid ⇒ Object
270
271
272
273
|
# File 'lib/asir/main.rb', line 270
def server_pid
pid = File.read(pid_file).chomp!
pid.to_i
end
|
#start_beanstalk_conduit! ⇒ Object
177
178
179
|
# File 'lib/asir/main.rb', line 177
def start_beanstalk_conduit!
_start_conduit!
end
|
#start_resque_conduit! ⇒ Object
181
182
183
|
# File 'lib/asir/main.rb', line 181
def start_resque_conduit!
_start_conduit!
end
|
#stop_pid!(pid, msg = nil) ⇒ Object
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
# File 'lib/asir/main.rb', line 340
def stop_pid! pid, msg = nil
log "stopping #{msg}pid #{pid}", :stderr
if process_running? pid
log "TERM pid #{pid}"
Process.kill('TERM', pid) rescue nil
sleep 3
if force or process_running? pid
log "KILL pid #{pid}", :stderr
Process.kill('KILL', pid) rescue nil
end
if process_running? pid
log "cant-stop pid #{pid}", :stderr
end
else
log "not-running? pid #{pid}", :stderr
end
end
|
#usage! ⇒ Object
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/asir/main.rb', line 131
def usage!
$stderr.puts "SYNOPSIS:\nasir [ <<options>> ... ] <<verb>> <<adjective>> <<object>> [ <<identifier>> ]\n\nOPTIONS:\nconfig_rb=file.rb ($ASIR_LOG_DIR)\npid_dir=dir/ ($ASIR_PID_DIR)\nlog_dir=dir/ ($ASIR_LOG_DIR)\nverbose=[0-9]\n\nVERBS:\nstart\nstop\nrestart\nstatus\nlog\npid\nalive\n\nADJECTIVE-OBJECTs:\nbeanstalk conduit\nbeanstalk worker\nzmq worker\nwebrick worker\nresque conduit\nresque worker\n\nEXAMPLES:\n\nexport ASIR_CONFIG_RB=\"some_system/asir_config.rb\"\nasir start beanstalk conduit\nasir status beanstalk conduit\n\nasir start webrick worker\nasir pid webrick worker\n\nasir start beanstalk worker 1\nasir start beanstalk worker 2\n\nasir start zmq worker\nasir start zmq worker 1\nasir start zmq worker 2\n"
end
|
#worker_pids ⇒ Object
285
286
287
|
# File 'lib/asir/main.rb', line 285
def worker_pids
(@worker_pids ||= { })[adjective] ||= { }
end
|