Rails,ActionController :: Live,Puma:ThreadError

时间:2014-11-27 07:29:51

标签: ruby-on-rails multithreading redis live-streaming puma

我想将通知流式传输到客户端。为此,我使用Redis pup/subActionController::Live。这是我StreamingController的样子:

class StreamingController < ActionController::Base
  include ActionController::Live

  def stream
    response.headers['Content-Type'] = 'text/event-stream'

    $redis.psubscribe("user-#{params[:user_id]}:*") do |on|
      on.pmessage do |subscription, event, data|
        response.stream.write "data: #{data}\n\n"
      end
    end
  rescue IOError
    logger.info "Stream closed"
  ensure
    response.stream.close
  end
end

这里是听取流的JS部分:

var source = new EventSource("/stream?user_id=" + user_id);
source.addEventListener("message", function(e) {
  data = jQuery.parseJSON(e.data);

  switch(data.type) {
    case "unread_receipts":
      updateUnreadReceipts(data);
      break;
  }
}, false);

现在如果我推送一些东西到redis,客户端会获得推送通知。所以这很好。但是,当我点击链接时,没有任何事情发生。在使用 Ctrl + C 取消rails服务器(我使用puma)后,我收到以下错误:

ThreadError: Attempt to unlock a mutex which is locked by another thread

config.middleware.delete Rack::Lock添加到development.rb后可以解决问题,但是在推送到客户端后我看不到任何控制台输出。 config.cache_classes = trueconfig.eager_load = true不是选项,因为我不希望每次开发时都重新启动我的服务器。

还有其他解决方案吗?

1 个答案:

答案 0 :(得分:0)

如果您想避免重新启动服务器以获取更改,那么我认为您需要运行多个进程。

相关问题