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}")
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
288
289
290
291
292
293
294
295
296
|
# File 'lib/asir/main.rb', line 288
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
324
325
326
327
328
329
330
331
332
333
334
335
336
|
# File 'lib/asir/main.rb', line 324
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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
# File 'lib/asir/main.rb', line 302
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
192
193
194
195
196
197
198
|
# File 'lib/asir/main.rb', line 192
def _start_conduit!
config!(:environment)
self.transport = config!(:transport)
fork_server! do
transport.start_conduit! :fork => false
end
end
|
#_start_worker!(type = adjective) ⇒ Object
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
# File 'lib/asir/main.rb', line 200
def _start_worker! type = adjective
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
# File 'lib/asir/main.rb', line 338
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_server!(cmd = nil, &blk) ⇒ Object
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/asir/main.rb', line 218
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
265
266
267
268
269
270
271
272
|
# File 'lib/asir/main.rb', line 265
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
274
275
276
277
278
279
280
281
|
# File 'lib/asir/main.rb', line 274
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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
# File 'lib/asir/main.rb', line 371
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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
# File 'lib/asir/main.rb', line 236
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
283
284
285
286
|
# File 'lib/asir/main.rb', line 283
def server_pid
pid = File.read(pid_file).chomp!
pid.to_i
end
|
#start_beanstalk_conduit! ⇒ Object
184
185
186
|
# File 'lib/asir/main.rb', line 184
def start_beanstalk_conduit!
_start_conduit!
end
|
#start_resque_conduit! ⇒ Object
188
189
190
|
# File 'lib/asir/main.rb', line 188
def start_resque_conduit!
_start_conduit!
end
|
#stop_pid!(pid, msg = nil) ⇒ Object
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
# File 'lib/asir/main.rb', line 353
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
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
176
177
178
179
180
181
182
|
# File 'lib/asir/main.rb', line 137
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]
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
298
299
300
|
# File 'lib/asir/main.rb', line 298
def worker_pids
(@worker_pids ||= { })[adjective] ||= { }
end
|