Class: ASIR::Application::Spawn

Inherits:
Object
  • Object
show all
Defined in:
lib/asir/application.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#appObject

Returns the value of attribute app.



23
24
25
# File 'lib/asir/application.rb', line 23

def app
  @app
end

#blkObject

Returns the value of attribute blk.



23
24
25
# File 'lib/asir/application.rb', line 23

def blk
  @blk
end

#cmdObject

Returns the value of attribute cmd.



23
24
25
# File 'lib/asir/application.rb', line 23

def cmd
  @cmd
end

#nameObject

Returns the value of attribute name.



23
24
25
# File 'lib/asir/application.rb', line 23

def name
  @name
end

#pidObject

Returns the value of attribute pid.



23
24
25
# File 'lib/asir/application.rb', line 23

def pid
  @pid
end

#tmpObject

Returns the value of attribute tmp.



23
24
25
# File 'lib/asir/application.rb', line 23

def tmp
  @tmp
end

#tmp_outObject

Returns the value of attribute tmp_out.



23
24
25
# File 'lib/asir/application.rb', line 23

def tmp_out
  @tmp_out
end

Instance Method Details

#go!Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/asir/application.rb', line 30

def go!
  case RUBY_PLATFORM
  when /java/i
    @tmp = "/tmp/#{$$}.#{name}"
    @tmp_out = "#{tmp}.out"
    @tmp_run = "#{tmp}.run"
    File.unlink(@tmp_out) rescue nil
    File.unlink(@tmp_run) rescue nil
    begin
      require 'spoon'
      ruby = ASIR.ruby_path
      # ruby = "/usr/bin/ruby"
      inc = app.inc.map{|x| "-I#{x}"}
      @cmd = [ ruby ]
      @cmd.concat(inc)
      @cmd.concat([ $0, "--asir-spawn=#{name}", @tmp ])
      $stderr.puts "#{self} cmd #{cmd * ' '}" if verbose
      @pid = Spoon.spawnp(*cmd)
      # Wait until started.
      until File.exist?(@tmp_run)
        sleep 0.1
      end
    ensure
      File.unlink(@tmp_run) rescue nil
    end
  else
    @pid = Process.fork(&blk)
  end
  @pid
end

#killObject



61
62
63
64
# File 'lib/asir/application.rb', line 61

def kill
  Process.kill 9, pid
  wait
end

#to_sObject



26
27
28
# File 'lib/asir/application.rb', line 26

def to_s
  "#<#{self.class.name} #{name.inspect} #{@pid} >"
end

#verboseObject



24
# File 'lib/asir/application.rb', line 24

def verbose; app.verbose; end

#waitObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/asir/application.rb', line 66

def wait
  # Wait until finished and collect output.
  begin
    Process.waitpid(pid)
    while true
      Process.kill(0, pid)
      sleep 1
    end
  rescue Errno::ECHILD, Errno::ESRCH
  ensure
    $stderr.puts "Spawn #{self} finished" if verbose
    @pid = nil
    if tmp_out && File.exist?(tmp_out)
      $stdout.write(File.read(tmp_out))
      File.unlink(tmp_out)
    end
    @tmp_out = nil
  end
end