滑轨挂在动作电缆上时/睡眠循环

时间:2019-08-12 23:41:55

标签: ruby-on-rails ruby ruby-on-rails-5

我想知道为什么我的动作电缆正在寻找文件更改并将事件发送到客户端后,在页面上工作了一段时间(3-4分钟)后为什么会挂起。这是我的代码:

class MonitorChannel < ApplicationCable::Channel
  def subscribed
    stream_from "monitor_channel"

    options = OpenStruct.new(
      verbose: false,
      follow: true,
      num_lines: 10,
      bytes: false
    )

    @tail = ::Tail.new('/home/sandric/.test.log')
    original_entries = @tail.read(options[:num_lines], options[:bytes])
    ActionCable.server.broadcast 'monitor_channel', message: original_entries

    if options[:follow]
      while !@tail.stopped?
        if @tail.file_changed?
          new_entries = @tail.read_all
          ActionCable.server.broadcast 'monitor_channel', message: new_entries
        end
        sleep(1)
      end
    end
  end

  def unsubscribed
    @tail.stop
  end
end

我省略了Tail类实现,它是用于IO的简单ruby类。 我知道while truesleep(1)并不是正确的选择,但我想知道为什么这行不通,服务器仅在几分钟内开始挂起,并且我的服务器是puma-因此应该是利用线程,特别是对于ActionCable的否?

您还建议使用什么来代替while / sleep解决方案,纯红宝石线程或诸如EventMachine之类的框架?谢谢。

0 个答案:

没有答案