asir 1.2.3 → 1.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,3 +1,17 @@
1
+ 2012-03-12 Kurt A. Stephens <ks.github@kurtstephens.com>
2
+
3
+ * v1.2.5: New Version: bin/asir.
4
+ * Do not log error if pid_file does not exist.
5
+ * Handle fork_process= option.
6
+ * export ASIR_WORKER=instance:wid for asir start KIND worker.
7
+ * export ASIR_CONDUIT=1 for asir start KIND conduit.
8
+ * Remove pid_file if process is not running after asir stop.
9
+
10
+ 2012-02-01 Kurt A. Stephens <ks.github@kurtstephens.com>
11
+
12
+ * v1.2.4: New Version: Bug fixes.
13
+ * Handle STDIO properly in potentially daemonized processes.
14
+
1
15
  2013-01-16 Kurt A. Stephens <ks.github@kurtstephens.com>
2
16
 
3
17
  * v1.2.3: New Version: Bug fixes.
@@ -10,12 +10,14 @@ when :configure
10
10
  when :environment
11
11
  require 'rubygems'
12
12
 
13
+ $:.unshift File.expand_path('../..', __FILE__)
14
+ $:.unshift File.expand_path('../../../lib', __FILE__)
15
+
13
16
  require 'asir'
14
17
  require 'asir/transport/file'
15
18
  require 'asir/coder/marshal'
16
19
  require 'asir/coder/yaml'
17
20
 
18
- $:.unshift File.expand_path('..')
19
21
  require 'example_helper'
20
22
  require 'sample_service'
21
23
  require 'unsafe_service'
@@ -5,6 +5,7 @@ module ASIR; class Environment
5
5
  attr_accessor :verb, :adjective, :object, :identifier
6
6
  attr_accessor :config_rb, :config
7
7
  attr_accessor :log_dir, :log_file, :pid_dir
8
+ attr_accessor :fork_process
8
9
  attr_accessor :options
9
10
  attr_accessor :verbose
10
11
 
@@ -30,6 +31,14 @@ module ASIR; class Environment
30
31
  '/tmp'
31
32
  end
32
33
 
34
+ def fork_process
35
+ case @fork_process
36
+ when nil, String, Integer
37
+ @fork_process = (@fork_process || ENV['ASIR_FORK_PROCESS'] || '1').to_i > 0
38
+ end
39
+ @fork_process
40
+ end
41
+
33
42
  def find_writable_directory kind, *list
34
43
  list.
35
44
  reject { | p | ! p }.
@@ -9,7 +9,7 @@ class Main
9
9
  [ :verb, :adjective, :object, :identifier,
10
10
  :config_rb,
11
11
  :verbose,
12
- :options,
12
+ :options, :fork_process,
13
13
  :log_dir, :log_file,
14
14
  :pid_dir, :pid_file,
15
15
  ].
@@ -113,16 +113,18 @@ class Main
113
113
  when /^status_([^_]+)_([^_]+)!$/
114
114
  pid = server_pid
115
115
  puts "#{log_str} pid #{pid}"
116
- system("ps -fw -p #{pid}")
116
+ system("ps -fw -p #{pid}") if pid
117
117
  when /^log_([^_]+)_([^_]+)!$/
118
118
  puts log_file
119
119
  when /^taillog_([^_]+)_([^_]+)!$/
120
120
  exec "tail -f #{log_file.inspect}"
121
121
  when /^pid_([^_]+)_([^_]+)!$/
122
- pid = _alive?
122
+ pid = server_pid
123
+ alive = !! _alive?
123
124
  puts "#{pid_file} #{pid || :NA} #{alive}"
124
125
  when /^alive_([^_]+)_([^_]+)!$/
125
- pid = _alive?
126
+ pid = server_pid
127
+ alive = !! _alive?
126
128
  puts "#{pid_file} #{pid || :NA} #{alive}" if @verbose
127
129
  self.exit_code += 1 unless alive
128
130
  when /^stop_([^_]+)_([^_]+)!$/
@@ -147,6 +149,7 @@ OPTIONS:
147
149
  pid_dir=dir/ ($ASIR_PID_DIR)
148
150
  log_dir=dir/ ($ASIR_LOG_DIR)
149
151
  verbose=[0-9]
152
+ fork_process=[01] ($ASIR_FORK_PROCESS)
150
153
 
151
154
  VERBS:
152
155
  start
@@ -201,6 +204,7 @@ END
201
204
  config!(:environment)
202
205
  self.transport = config!(:transport)
203
206
  fork_server! do
207
+ ENV['ASIR_CONDUIT'] = '1'
204
208
  transport.start_conduit! :fork => false
205
209
  end
206
210
  end
@@ -228,29 +232,51 @@ END
228
232
  end
229
233
 
230
234
  def fork_server! cmd = nil, &blk
231
- pid = Process.fork do
235
+ fork_process! do
232
236
  run_server! cmd, &blk
233
237
  end
234
- log "forked pid #{pid}"
235
- Process.detach(pid) # Forks a Thread? We are gonna exit anyway.
236
- File.open(pid_file, "w+") { | o | o.puts pid }
237
- File.chmod(0666, pid_file) rescue nil
238
238
 
239
- # Wait and check if process still exists.
240
- sleep 3
241
- unless process_running? pid
242
- raise "Server process #{pid} died to soon?"
239
+ self
240
+ end
241
+
242
+ def fork_process! &blk
243
+ if fork_process
244
+ pid = Process.fork &blk
245
+ log "forked pid #{pid}"
246
+ Process.detach(pid) # Forks a Thread? We are gonna exit anyway.
247
+ write_pid_file! pid
248
+
249
+ # Wait and check if process still exists.
250
+ sleep 3
251
+ unless process_running? pid
252
+ raise "Server process #{pid} died to soon?"
253
+ end
254
+ else
255
+ pid = $$
256
+ log "running pid #{pid}"
257
+ write_pid_file! pid
258
+ yield
243
259
  end
260
+ pid
261
+ end
244
262
 
245
- self
263
+ def write_pid_file! pid
264
+ File.open(pid_file, "w+") { | o | o.puts pid }
265
+ File.chmod(0666, pid_file) rescue nil
246
266
  end
247
267
 
248
268
  def run_server! cmd = nil
269
+ nf = File.open("/dev/null")
270
+ nf.sync = true
271
+ STDIN.reopen(nf)
272
+ STDIN.sync = true
273
+ $stdin.reopen(nf) if $stdin.object_id != STDIN.object_id
274
+ $stdin.sync = true
275
+
249
276
  lf = File.open(log_file, "a+")
250
277
  lf.sync = true
251
278
  File.chmod(0666, log_file) rescue nil
252
- $stdin.close rescue nil
253
- STDIN.close rescue nil
279
+
254
280
  STDOUT.reopen(lf)
255
281
  STDOUT.sync = true
256
282
  $stdout.reopen(lf) if $stdout.object_id != STDOUT.object_id
@@ -259,6 +285,7 @@ END
259
285
  STDERR.sync = true
260
286
  $stderr.reopen(lf) if $stderr.object_id != STDERR.object_id
261
287
  $stderr.sync = true
288
+
262
289
  # Process.daemon rescue nil # Ruby 1.9.x only.
263
290
  lf.puts "#{log_str} starting pid #{$$}"
264
291
  begin
@@ -282,7 +309,10 @@ END
282
309
  def kill_server!
283
310
  log "#{log_str} kill"
284
311
  pid = server_pid
285
- stop_pid! pid
312
+ case stop_pid! pid
313
+ when :not_running
314
+ File.unlink(pid_file) rescue nil
315
+ end
286
316
  rescue ::Exception => exc
287
317
  log "#{log_str} ERROR\n#{exc.inspect}\n #{exc.backtrace * "\n "}", :stderr
288
318
  raise
@@ -290,7 +320,7 @@ END
290
320
 
291
321
  def log msg, to_stderr = false
292
322
  if to_stderr
293
- $stderr.puts "#{log_str_no_time} #{msg}"
323
+ $stderr.puts "#{log_str_no_time} #{msg}" rescue nil
294
324
  end
295
325
  File.open(log_file, "a+") do | log |
296
326
  log.puts "#{log_str} #{msg}"
@@ -298,8 +328,8 @@ END
298
328
  end
299
329
 
300
330
  def server_pid
301
- pid = File.read(pid_file).chomp!
302
- pid.to_i
331
+ File.exist?(pid_file) &&
332
+ File.read(pid_file).chomp!.to_i
303
333
  end
304
334
 
305
335
  def _create_transport default_class
@@ -318,7 +348,6 @@ END
318
348
 
319
349
  def _run_workers!
320
350
  $0 = "#{progname} #{adjective} #{object} #{identifier}"
321
-
322
351
  worker_id = 0
323
352
  transport.prepare_server!
324
353
  worker_processes = transport[:worker_processes] || 1
@@ -332,13 +361,14 @@ END
332
361
  log "forked #{wid} pid #{pid}"
333
362
  end
334
363
 
335
- _run_transport_server!
364
+ _run_transport_server! 0
336
365
  ensure
337
366
  log "worker 0 stopped"
338
367
  _stop_workers!
339
368
  end
340
369
 
341
370
  def _run_transport_server! wid = 0
371
+ ENV['ASIR_WORKER'] = "#{identifier}:#{wid}"
342
372
  log "running transport worker #{transport.class} #{wid}"
343
373
  config!(:start)
344
374
  $0 += " #{wid} #{transport.uri rescue nil}"
@@ -380,9 +410,11 @@ END
380
410
  end
381
411
  if process_running? pid
382
412
  log "cant-stop pid #{pid}", :stderr
413
+ :cant_stop
383
414
  end
384
415
  else
385
416
  log "not-running? pid #{pid}", :stderr
417
+ :not_running
386
418
  end
387
419
  end
388
420
 
@@ -15,7 +15,8 @@ module Asir
15
15
  raise "already running #{@conduit_pid} #{@conduit_cmd}" if @conduit_pid
16
16
  if in_fork
17
17
  @conduit_pid = ::Process.fork do
18
- _log { "start_conduit! #{self} starting pid=#{$$.inspect}" } if @verbose >= 2
18
+ _log { "start_conduit! #{self} starting pid=#{$$.inspect}" } if @verbose >= 2
19
+ _close_stdio!
19
20
  _start_conduit!
20
21
  raise "Could not exec"
21
22
  end
@@ -24,11 +25,19 @@ module Asir
24
25
  ::File.open(pid_file, "w") { | fh | fh.puts @conduit_pid }
25
26
  end
26
27
  else
28
+ _close_stdio!
27
29
  _start_conduit!
28
30
  end
29
31
  self
30
32
  end
31
33
 
34
+ def _close_stdio!
35
+ n = ::File.open("/dev/null", "w+")
36
+ [ STDIN, STDOUT, STDERR, $stdin, $stdout, $stderr ].each do | io |
37
+ io.reopen(n) rescue nil
38
+ end
39
+ end
40
+
32
41
  def conduit_pid
33
42
  if ! @conduit_pid and pid_file = (@conduit_options || EMPTY_HASH)[:pid_file]
34
43
  @conduit_pid = (::File.read(pid_file).to_i rescue nil)
@@ -1,3 +1,3 @@
1
1
  module ASIR
2
- VERSION = "1.2.3"
2
+ VERSION = "1.2.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asir
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.2.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-16 00:00:00.000000000 Z
12
+ date: 2013-03-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: uuid
@@ -270,7 +270,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
270
270
  version: '0'
271
271
  segments:
272
272
  - 0
273
- hash: 418469004383493429
273
+ hash: 1538140158003957969
274
274
  required_rubygems_version: !ruby/object:Gem::Requirement
275
275
  none: false
276
276
  requirements:
@@ -279,10 +279,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
279
279
  version: '0'
280
280
  segments:
281
281
  - 0
282
- hash: 418469004383493429
282
+ hash: 1538140158003957969
283
283
  requirements: []
284
284
  rubyforge_project:
285
- rubygems_version: 1.8.24
285
+ rubygems_version: 1.8.25
286
286
  signing_key:
287
287
  specification_version: 3
288
288
  summary: Abstracting Services in Ruby