Question

I call a ffmpeg command in rails to grep a stream and push it to Wowza so that a apple device can see it also, the command is something like this

Thread.new do
  `/usr/local/bin/ffmpeg -re -i http://10.191.255.90:30080/#{user.phone} -vcodec libx264 -vpre ipod640 -g 15 -acodec libvo_aacenc -ar 8000 -ac 1 -f flv rtmp://127.0.0.1/live/#{user.phone}`
end

sometimes the process will be killed because of a break of network or other reasons, I'm wondering is there a way I can monitor the process so that as soon as it stop I can restart it again

My current idea is to record the pid of the process, and let somebody like resque to check whether a process of the given pid is running every 60 sencods, but I has a lot of such process so I wondering it's will get into performance problem later.

Is there a better solution to manage these process in Ruby?

Was it helpful?

Solution

You can do something like this:

Thread.new do
  begin
    Process.wait Process.spawn 'find /oeinfsroif'
    raise unless $?.exitstatus == 0
  rescue
    retry
  end
end.join

To manage the number of attempts before the failing:

Thread.new do
  max_attempts = 10
  attempts = 0
  begin
    Process.wait Process.spawn 'find /oeinfsroif'
    raise unless $?.exitstatus == 0
  rescue
    attempts += 1
    attempts < max_attempts ? retry : raise 
  end
end.join

Output:

find: `/oeinfsroif': No such file or directory
find: `/oeinfsroif': No such file or directory
find: `/oeinfsroif': No such file or directory
find: `/oeinfsroif': No such file or directory
find: `/oeinfsroif': No such file or directory
find: `/oeinfsroif': No such file or directory
find: `/oeinfsroif': No such file or directory
find: `/oeinfsroif': No such file or directory
find: `/oeinfsroif': No such file or directory
find: `/oeinfsroif': No such file or directory
rb:6:in `block in <main>': unhandled exception
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top