【问题标题】:Getting ActiveJob and Sidekiq working with ActionCable for Chat Room让 ActiveJob 和 Sidekiq 与 ActionCable 一起用于聊天室
【发布时间】:2016-12-23 02:46:03
【问题描述】:

所以我一直在关注 DHH 的关于实现基本聊天室的教程。

在我尝试集成 ActiveJob 组件之前,该实现对我来说已经足够好了。

我在 rails 或 sidekiq 控制台中没有看到任何错误消息。但是 _message 部分只是没有被渲染。当我在 ActiveJob 方法中放入 binding.pry 时,所有元素看起来都很好,所以我猜这与作业执行有关?

为了进一步支持这种怀疑,当我打电话给

ActionCable.server.broadcast('room_channel', message: data['message']) 

来自 room_channel.rb 它工作得很好!

我已经为此花费了数小时,但无法弄清楚如何进一步调试。我想知道是否需要使用 Sidekiq 进行任何不同的 redis 配置才能使其正常工作。到目前为止,我只是说:

config.active_job.queue_adapter = :sidekiq

在 application.rb 中

我还需要一个 redis 初始化程序吗?

对此或任何其他见解的任何建议将不胜感激!

Rails 服务器:

// ♥ rails s
=> Booting Puma
=> Rails 5.0.0.1 application starting in development on     http://localhost:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.6.0 (ruby 2.2.3-p173), codename: Sleepy Sunday Serenity
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
Started GET "/cable" for ::1 at 2016-08-16 16:25:21 +0100    
  ActiveRecord::SchemaMigration Load (0.4ms)  SELECT   "schema_migrations".* FROM "schema_migrations"
Started GET "/cable/" [WebSocket] for ::1 at 2016-08-16 16:25:21 +0100
Successfully upgraded to WebSocket (REQUEST_METHOD: GET,     HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
Finished "/cable/" [WebSocket] for ::1 at 2016-08-16 16:25:21 +0100
Started GET "/cable" for ::1 at 2016-08-16 16:25:22 +0100
Started GET "/cable/" [WebSocket] for ::1 at 2016-08-16 16:25:22 +0100
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket) 
RoomChannel is transmitting the subscription confirmation
RoomChannel is streaming from room_channel
RoomChannel#speak({"message"=>"new message"})
   (0.1ms)  BEGIN
      SQL (0.5ms)  INSERT INTO "messages" ("content", "created_at",     "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["content", "new     message"], ["created_at", 2016-08-16 15:25:25 UTC], ["updated_at", 2016-    08-16 15:25:25 UTC]]
(1.3ms)  COMMIT
[ActiveJob] Enqueued MessageBroadcastJob (Job ID: 5981c48d-afbc-4fad-a595-198b56aa90f4) to Sidekiq(default) with arguments: #<GlobalID:0x007f82aa837b90 @uri=#<URI::GID gid://dragonfly/Message/39>>

Sidekiq:

bundle exec sidekiq -q default -q mailers

2016-08-16T15:23:32.583Z 70743 TID-ow892ejts INFO: Running in ruby     2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin15]
2016-08-16T15:23:32.583Z 70743 TID-ow892ejts INFO: See LICENSE and  the LGPL-3.0 for licensing details.
2016-08-16T15:23:32.583Z 70743 TID-ow892ejts INFO: Upgrade to Sidekiq Pro for more features and support: http://sidekiq.org
2016-08-16T15:23:32.584Z 70743 TID-ow892ejts INFO: Booting Sidekiq 4.1.2 with redis options {:url=>nil}
2016-08-16T15:23:32.587Z 70743 TID-ow892ejts INFO: Starting processing, hit Ctrl-C to stop
2016-08-16T15:24:00.652Z 70743 TID-ow89s527k MessageBroadcastJob JID-4ae621ca049eb428ced1c7d2 INFO: start
2016-08-16T15:24:01.153Z 70743 TID-ow89s527k MessageBroadcastJob JID- 4ae621ca049eb428ced1c7d2 INFO: done: 0.501 sec
2016-08-16T15:24:14.351Z 70743 TID-ow89s52rk MessageBroadcastJob JID-fe94b25a9100e83f874ea785 INFO: start

app/jobs/message_broadcast_job.rb

    class MessageBroadcastJob < ApplicationJob
      queue_as :default

      def perform(message)
        ActionCable.server.broadcast "room_channel", {message: render_message(message)}
  end

  private

  def render_message(message)
    ApplicationController.renderer.render(partial: 'messages/message', locals: { message: message })
  end

end

app/assets/javascripts/channels/room.coffee

App.room = App.cable.subscriptions.create "RoomChannel",
  connected: ->
    # Called when the subscription is ready for use on the server

  disconnected: ->
    # Called when the subscription has been terminated by the server

  received: (data) ->
    alert data['message']

  speak: (message) ->
    @perform 'speak', message: message

$(document).on 'keypress', '[data-behaviour~=room_speaker]', (event) ->
  if event.keyCode is 13
    App.room.speak event.target.value
    event.target.value = ''
    event.preventDefault()

【问题讨论】:

  • 您在将 actioncable 与 sidekiq 结合使用方面取得了进展吗?

标签: ruby-on-rails redis sidekiq rails-activejob actioncable


【解决方案1】:

这与 ActiveJob 或 Sidekiq 无关。我想渲染部分的时候肯定有一些错误。

尝试在 Rails 控制台中执行该行,

ApplicationController.renderer.render(partial: 'messages/message', locals: { message: message })

希望你得到一些有意义的错误,例如未定义的方法或类似的东西。

【讨论】:

  • 谢谢!调试这种东西的好主意。因此,我向 Rails 专家询问了这个问题,他们对它进行了一天的黑客攻击,他们认为这是 Sidekiq(或任何排队服务)的问题,因为他们没有将控制权交还给 Action Cable。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-31
  • 1970-01-01
  • 2015-04-09
  • 2016-10-06
  • 2012-01-29
  • 1970-01-01
相关资源
最近更新 更多