Class: Bj::Runner::Background

Inherits:
Object
  • Object
show all
Defined in:
lib/bj/runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ Background



10
11
12
13
# File 'lib/bj/runner.rb', line 10

def initialize command
  @command = command
  @thread = new_thread
end

Class Method Details

.for(*a, &b) ⇒ Object



4
# File 'lib/bj/runner.rb', line 4

def self.for(*a, &b) new(*a, &b) end

Instance Method Details

#inspectObject



15
16
17
18
19
20
# File 'lib/bj/runner.rb', line 15

def inspect
  {
    "command" => command,
    "pid" => pid,
  }.inspect
end

#new_threadObject

TODO - auto start runner?



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bj/runner.rb', line 25

def new_thread 
  this = self
  Thread.new do
    Thread.current.abort_on_exception = true
    loop do
      cleanup = lambda{}

      IO.popen command, "r+" do |pipe|
        this.pid = pid = pipe.pid
        cleanup = lambda do
          cleanup = lambda{}
          begin; Process.kill(Runner.kill_signal, pid); rescue Exception; 42; end
        end
        at_exit &cleanup
        Process.wait
      end

      Bj.logger.error{ "#{ command } failed with #{ $?.inspect }" } unless
        [0, 42].include?($?.exitstatus)

      cleanup.call

      sleep 42
    end
  end
end