【问题标题】:Rails, ActionController::Live, Puma: ThreadErrorRails,ActionController::Live,彪马:ThreadError
【发布时间】:2015-01-25 17:36:32
【问题描述】:

我想向客户端发送通知。为此,我使用 Redis pup/sub 和 ActionController::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 = true 和 config.eager_load = true 没有选择,因为我不想在每次开发时都重新启动服务器。

还有其他解决办法吗?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-05
      相关资源
      最近更新 更多