Class: Spool::Spawner

Inherits:
Object
  • Object
show all
Defined in:
lib/spool/spawner.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Spawner



6
7
8
# File 'lib/spool/spawner.rb', line 6

def initialize(configuration)
  @configuration = configuration
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



4
5
6
# File 'lib/spool/spawner.rb', line 4

def configuration
  @configuration
end

Class Method Details

.spawn(configuration) ⇒ Object



31
32
33
# File 'lib/spool/spawner.rb', line 31

def self.spawn(configuration)
  new(configuration).spawn
end

Instance Method Details

#spawnObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/spool/spawner.rb', line 10

def spawn
  base_file = File.join Dir.tmpdir, SecureRandom.uuid
  out_file = "#{base_file}.out"
  command = configuration.command.strip

  pid = Process.spawn configuration.env, 
                      "exec #{command}", 
                      chdir: configuration.dir, 
                      out: out_file, 
                      err: out_file

  Process.detach pid

  Datacenter::Process.new(pid).tap do |process|
    raise "Invalid command: #{command}\n#{IO.read(out_file)}" unless process.alive?
  end

ensure
  File.delete out_file if File.exist? out_file
end