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
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
|
# File 'lib/asir/main.rb', line 108
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}") if pid
when /^log_([^_]+)_([^_]+)!$/
puts log_file
when /^taillog_([^_]+)_([^_]+)!$/
exec "tail -f #{log_file.inspect}"
when /^pid_([^_]+)_([^_]+)!$/
pid = server_pid
alive = !! _alive?
puts "#{pid_file} #{pid || :NA} #{alive}"
when /^alive_([^_]+)_([^_]+)!$/
pid = server_pid
alive = !! _alive?
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
#_alive? ⇒ Boolean
137
138
139
140
|
# File 'lib/asir/main.rb', line 137
def _alive?
pid = server_pid rescue nil
process_running? pid
end
|
#_create_transport(default_class) ⇒ Object
335
336
337
338
339
340
341
342
343
|
# File 'lib/asir/main.rb', line 335
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
# File 'lib/asir/main.rb', line 370
def _run_transport_server! wid = 0
ENV['ASIR_WORKER'] = "#{identifier}:#{wid}"
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, state | nil }
transport.after_receive_message = lambda do | transport, state |
message = state.message
$0 = "#{old_arg0} #{transport.message_count} #{message.identifier}"
after_receive_message.call(transport, state)
end
transport.run_server!
self
end
|
#_run_verb! ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/asir/main.rb', line 92
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
# File 'lib/asir/main.rb', line 349
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! 0
ensure
log "worker 0 stopped"
_stop_workers!
end
|
#_start_conduit!(type = adjective) ⇒ Object
198
199
200
201
202
203
204
205
206
207
208
209
210
|
# File 'lib/asir/main.rb', line 198
def _start_conduit! type = adjective
if (pid = _alive?) && ! force
log "already-running pid #{pid}", :stderr
return
end
log "start_conduit! #{type}"
config!(:environment)
self.transport = config!(:transport)
fork_server! do
ENV['ASIR_CONDUIT'] = '1'
transport.start_conduit! :fork => false
end
end
|
#_start_worker!(type = adjective) ⇒ Object
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
# File 'lib/asir/main.rb', line 212
def _start_worker! type = adjective
if (pid = _alive?) && ! force
log "already-running pid #{pid}", :stderr
return
end
log "start_worker! #{type}"
type = type.to_s
config!(:environment)
self.transport = config!(:transport)
transport_file = "asir/transport/#{type}"
log "loading #{transport_file}"
require transport_file
transport_class = ASIR::Transport.const_get(type[0..0].upcase + type[1..-1])
fork_server! do
_create_transport transport_class
_run_workers!
end
end
|
#_stop_workers! ⇒ Object
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
# File 'lib/asir/main.rb', line 386
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
54
55
56
|
# File 'lib/asir/main.rb', line 54
def config! *args
@env.config! *args
end
|
#fork_process!(&blk) ⇒ Object
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
# File 'lib/asir/main.rb', line 242
def fork_process! &blk
if fork_process
pid = Process.fork &blk
log "forked pid #{pid}"
Process.detach(pid) write_pid_file! pid
sleep 3
unless process_running? pid
raise "Server process #{pid} died to soon?"
end
else
pid = $$
log "running pid #{pid}"
write_pid_file! pid
yield
end
pid
end
|
#fork_server!(cmd = nil, &blk) ⇒ Object
234
235
236
237
238
239
240
|
# File 'lib/asir/main.rb', line 234
def fork_server! cmd = nil, &blk
fork_process! do
run_server! cmd, &blk
end
self
end
|
#kill_server! ⇒ Object
309
310
311
312
313
314
315
316
317
318
319
|
# File 'lib/asir/main.rb', line 309
def kill_server!
log "#{log_str} kill"
pid = server_pid
case stop_pid! pid
when :not_running
File.unlink(pid_file) rescue nil
end
rescue ::Exception => exc
log "#{log_str} ERROR\n#{exc.inspect}\n #{exc.backtrace * "\n "}", :stderr
raise
end
|
#log(msg, to_stderr = false) ⇒ Object
321
322
323
324
325
326
327
328
|
# File 'lib/asir/main.rb', line 321
def log msg, to_stderr = false
if to_stderr
$stderr.puts "#{log_str_no_time} #{msg}" rescue nil
end
File.open(log_file, "a+") do | log |
log.puts "#{log_str} #{msg}"
end
end
|
#log_str ⇒ Object
58
59
60
|
# File 'lib/asir/main.rb', line 58
def log_str
"#{Time.now.gmtime.iso8601(4)} #{$$} #{log_str_no_time}"
end
|
#log_str_no_time ⇒ Object
62
63
64
|
# File 'lib/asir/main.rb', line 62
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
50
51
52
|
# File 'lib/asir/main.rb', line 33
def parse_args! args = ARGV.dup
self.args = args
until args.empty?
case args.first
when /^--?h(elp)/
@help = true
return self
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
# File 'lib/asir/main.rb', line 421
def process_running? pid
case pid
when false, nil
when Integer
Process.kill(0, pid)
else
raise TypeError, "expected false, nil, Integer; given #{pid.inspect}"
end
pid
rescue ::Errno::ESRCH
false
rescue ::Exception => exc
$stderr.puts " DEBUG: process_running? #{pid} => #{exc.inspect}"
false
end
|
#run! ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/asir/main.rb', line 66
def run!
if @help
return usage!
end
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
# File 'lib/asir/main.rb', line 268
def run_server! cmd = nil
nf = File.open("/dev/null")
nf.sync = true
STDIN.reopen(nf)
STDIN.sync = true
$stdin.reopen(nf) if $stdin.object_id != STDIN.object_id
$stdin.sync = true
lf = File.open(log_file, "a+")
lf.sync = true
File.chmod(0666, log_file) rescue nil
STDOUT.reopen(lf)
STDOUT.sync = true
$stdout.reopen(lf) if $stdout.object_id != STDOUT.object_id
$stdout.sync = true
STDERR.reopen(lf)
STDERR.sync = true
$stderr.reopen(lf) if $stderr.object_id != STDERR.object_id
$stderr.sync = true
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
330
331
332
333
|
# File 'lib/asir/main.rb', line 330
def server_pid
File.exist?(pid_file) &&
File.read(pid_file).chomp!.to_i
end
|
#start_beanstalk_conduit! ⇒ Object
190
191
192
|
# File 'lib/asir/main.rb', line 190
def start_beanstalk_conduit!
_start_conduit!
end
|
#start_resque_conduit! ⇒ Object
194
195
196
|
# File 'lib/asir/main.rb', line 194
def start_resque_conduit!
_start_conduit!
end
|
#stop_pid!(pid, msg = nil) ⇒ Object
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
# File 'lib/asir/main.rb', line 401
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
:cant_stop
end
else
log "not-running? pid #{pid}", :stderr
:not_running
end
end
|
#usage! ⇒ Object
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
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/asir/main.rb', line 142
def usage!
$stderr.puts <<"END"
SYNOPSIS:
asir [ <<options>> ... ] <<verb>> <<adjective>> <<object>> [ <<identifier>> ]
OPTIONS:
config_rb=file.rb ($ASIR_LOG_DIR)
pid_dir=dir/ ($ASIR_PID_DIR)
log_dir=dir/ ($ASIR_LOG_DIR)
verbose=[0-9]
fork_process=[01] ($ASIR_FORK_PROCESS)
VERBS:
start
stop
restart
status
log
pid
alive
ADJECTIVE-OBJECTs:
beanstalk conduit
beanstalk worker
zmq worker
webrick worker
resque conduit
resque worker
EXAMPLES:
export ASIR_CONFIG_RB="some_system/asir_config.rb"
asir start beanstalk conduit
asir status beanstalk conduit
asir start webrick worker
asir pid webrick worker
asir start beanstalk worker 1
asir start beanstalk worker 2
asir start zmq worker
asir start zmq worker 1
asir start zmq worker 2
END
self
end
|
#worker_pids ⇒ Object
345
346
347
|
# File 'lib/asir/main.rb', line 345
def worker_pids
(@worker_pids ||= { })[adjective] ||= { }
end
|
#write_pid_file!(pid) ⇒ Object
263
264
265
266
|
# File 'lib/asir/main.rb', line 263
def write_pid_file! pid
File.open(pid_file, "w+") { | o | o.puts pid }
File.chmod(0666, pid_file) rescue nil
end
|