5

Is there a spawn equivalent for ruby 1.8.7?

It appears as though it was introduced in 1.9.1 http://apidock.com/ruby/Kernel/spawn

I need the following to work in ruby 1.8.7:

def run_worker(queue, count = 1)
  puts "Starting #{count} worker(s) with QUEUE: #{queue}"
  ops = {:pgroup => true, :err => [(Rails.root + "log/resque_err").to_s, "a"], 
                          :out => [(Rails.root + "log/resque_stdout").to_s, "a"]}
  env_vars = {"QUEUE" => queue.to_s}
  count.times {
    ## Using Kernel.spawn and Process.detach because regular system() call would
    ## cause the processes to quit when capistrano finishes
    pid = spawn(env_vars, "rake resque:work", ops)
    Process.detach(pid)
  }
end
Caleb
  • 3,692
  • 3
  • 24
  • 28
  • possible duplicate of [Spawn a background process in Ruby](http://stackoverflow.com/questions/2504445/spawn-a-background-process-in-ruby) – Paul Sweatte Mar 31 '14 at 16:04
  • If you think this is a duplicate of the question you referenced, you did not understand the question. The question you referenced is asking a general question about known/documented functionality that was introduced in a later version of ruby. I am asking how to do the same thing with an earlier version of ruby (i.e. without upgrading ruby, since that is not an option for the app which needs to spawn a process) – Caleb May 06 '14 at 00:34
  • 1
    `Process.spawn method, but that seems to be a 1.9+ feature, and I'm limited to Ruby 1.8.` – Paul Sweatte May 06 '14 at 01:26

1 Answers1

0

You can achieve this in 1.8.7 using this gem - https://github.com/rtomayko/posix-spawn. It does not support "full" 1.9's Process::spawn interface, but your example should work with it just by changing Process::spawn to POSIX::Spawn::spawn (https://github.com/rtomayko/posix-spawn#status).

sinjed
  • 906
  • 7
  • 10