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
26
27
28
29
30
|
# File 'lib/asir/main.rb', line 26
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
101
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
|
# File 'lib/asir/main.rb', line 101
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
21
22
23
|
# File 'lib/asir/main.rb', line 21
def force
@force
end
|
#progname ⇒ Object
Returns the value of attribute progname.
19
20
21
|
# File 'lib/asir/main.rb', line 19
def progname
@progname
end
|
#transport ⇒ Object
Transport selected from asir.phase = :transport.
24
25
26
|
# File 'lib/asir/main.rb', line 24
def transport
@transport
end
|
Instance Method Details
#_create_transport(default_class) ⇒ Object
274
275
276
277
278
279
280
281
282
|
# File 'lib/asir/main.rb', line 274
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
310
311
312
313
314
315
316
317
318
319
320
321
322
|
# File 'lib/asir/main.rb', line 310
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/asir/main.rb', line 85
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
# File 'lib/asir/main.rb', line 288
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
184
185
186
187
188
189
190
|
# File 'lib/asir/main.rb', line 184
def _start_conduit!
config!(:environment)
self.transport = config!(:transport)
fork_server! do
transport.start_conduit! :fork => false
end
end
|
#_start_worker!(type = adjective) ⇒ Object
192
193
194
195
196
197
198
199
200
201
202
|
# File 'lib/asir/main.rb', line 192
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
# File 'lib/asir/main.rb', line 324
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
50
51
52
|
# File 'lib/asir/main.rb', line 50
def config! *args
@env.config! *args
end
|
#fork_server!(cmd = nil, &blk) ⇒ Object
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# File 'lib/asir/main.rb', line 204
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
251
252
253
254
255
256
257
258
|
# File 'lib/asir/main.rb', line 251
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
260
261
262
263
264
265
266
267
|
# File 'lib/asir/main.rb', line 260
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
54
55
56
|
# File 'lib/asir/main.rb', line 54
def log_str
"#{Time.now.gmtime.iso8601(4)} #{$$} #{log_str_no_time}"
end
|
#log_str_no_time ⇒ Object
58
59
60
|
# File 'lib/asir/main.rb', line 58
def log_str_no_time
"#{progname} #{verb} #{adjective} #{object} #{identifier}"
end
|
#parse_args!(args = ARGV.dup) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/asir/main.rb', line 32
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
# File 'lib/asir/main.rb', line 357
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/asir/main.rb', line 62
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
222
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
|
# File 'lib/asir/main.rb', line 222
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
269
270
271
272
|
# File 'lib/asir/main.rb', line 269
def server_pid
pid = File.read(pid_file).chomp!
pid.to_i
end
|
#start_beanstalk_conduit! ⇒ Object
176
177
178
|
# File 'lib/asir/main.rb', line 176
def start_beanstalk_conduit!
_start_conduit!
end
|
#start_resque_conduit! ⇒ Object
180
181
182
|
# File 'lib/asir/main.rb', line 180
def start_resque_conduit!
_start_conduit!
end
|
#stop_pid!(pid, msg = nil) ⇒ Object
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
# File 'lib/asir/main.rb', line 339
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
130
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
|
# File 'lib/asir/main.rb', line 130
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
end
|
#worker_pids ⇒ Object
284
285
286
|
# File 'lib/asir/main.rb', line 284
def worker_pids
(@worker_pids ||= { })[adjective] ||= { }
end
|