S
S
Splite2014-05-28 18:32:28
ruby
Splite, 2014-05-28 18:32:28

Ruby - what are the specifics of how threads work?

Hello! There is a certain process which should work constantly, but can hang. I want to write a Ruby script that will launch this process and check its work in a separate thread at a certain interval (in a special way)

class PingWatchdog
  def initialize(server)
    Thread.new {
      while true do
        unless LaLaLa.test?(IP, PORT, PING_TIMEOUT)
          server.stop
        end
        sleep WATCHDOG_DELAY
      end
    }
  end
end

class ServerProcess
  def initialize(*opts)
    @proc_pid = spawn(CMD)
    @proc_thr = Process.detach(@proc_pid)
    @watchdog = PingWatchdog.new self
  end
  def wait
    @proc_thr.join
  end
  def command(data)
    $stdout << "#{data}\n"
  end
  def kill
    Process.kill('TERM', @proc_pid)
  end
end


while true
  server = ServerProcess.new
  server.wait
end

So, is it possible to launch a new PingWatchdog every time? When the server variable is reassigned, won't the thread in PingWatchdog still be running? If it is launched several times, it will disrupt the operation of the process.
And in general, maybe it's possible to implement this somehow more correctly?
I will be glad to any advice!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2014-05-28
Protko @Fesor

supervisord
In general, isn't it better to find the cause of the freezes? Let it crash rather than hang.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question