【问题标题】:How to ensure a process is running in Ruby如何确保进程在 Ruby 中运行
【发布时间】:2013-04-18 07:34:04
【问题描述】:

我在 rails 中调用 ffmpeg 命令来 grep 流并将其推送到 Wowza 以便苹果设备也可以看到它,命令是这样的

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

有时进程会因为网络中断或其他原因而被杀死,我想知道有没有办法可以监控进程,以便在它停止后立即重新启动它

我目前的想法是记录进程的pid,让resque之类的人检查给定pid的进程是否每60秒运行一次,但我有很多这样的进程,所以我想知道它会不会稍后会遇到性能问题。

在 Ruby 中管理这些流程有更好的解决方案吗?

【问题讨论】:

    标签: ruby-on-rails ruby process monitor


    【解决方案1】:

    你可以这样做:

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

    管理失败前的尝试次数:

    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
    

    输出:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-24
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-21
      • 2015-02-14
      • 2013-02-01
      相关资源
      最近更新 更多