Sidekiq perform_later 无法使用 Action Cable

Sidekiq perform_later not working with Action Cable

我已经在我的应用程序中设置了一个基本的有线聊天功能。我有一个 after_create_commit 回调,它将消息发送到要广播到适当频道的作业。当它设置为 perform_now 时它工作正常,但当它设置为 perform_later 时它不起作用。 Sidekiq执行任务并广播到正确的频道,但是频道订阅没有收到任何东西。

这是正在设置的频道:

PodsChannel is transmitting the subscription confirmation
PodsChannel is streaming from pods_channel_310
PodsChannel#speak({"message"=>"word", "pod_slug"=>"310", "user_id"=>"1", "msg_type"=>"pod_message"})

这是传入并发送到 sidekiq 的消息:

[ActiveJob] Enqueued MessageBroadcastJob (Job ID: c9cc59ce-2202-400d-92fb-15b80a9ece67) to Sidekiq(development_default) with arguments: #<GlobalID:0x007fa2e63526a0 @uri=#<URI::GID gid://sutra/PodMessage/1390>>

这里是 sidekiq 处理作业并向正确的频道广播:

[ActiveJob] [MessageBroadcastJob] [546222f6-9a57-493d-a3bb-b4886e0ad708]   Rendered pod_messages/_pod_message.html.erb (61.0ms)
[ActiveJob] [MessageBroadcastJob] [546222f6-9a57-493d-a3bb-b4886e0ad708] [ActionCable] Broadcasting to pods_channel_310: {:message=>"<div class='msg-wrap' id='msg_1389'><div class='msg-avatar'><a href=\"http://example.org/lorenzsell\"><img class=\"avatar-img\" alt=\"Lorenz\" src=\"/uploads/user/avatar/1/thumb_Lorenz2-square.jpg\" /></a></div><div class='msg-details'><div class='msg-meta'><div class='msg-sender'>Lorenz</div><div class='msg-timestamp'>Friday, August 12 at 12:09 AM</div><div class='msg-delete'><a data-remote=\"true\" rel=\"nofollow\" data-method=\"delete\" href=\"/delete_pod_message/1389\"><span class='glyphicon glyphicon-remove-circle' aria-hidden='true'></span></a></div></div><div class='msg-text'>word</div></div></div>\n"}
[ActiveJob] [MessageBroadcastJob] [546222f6-9a57-493d-a3bb-b4886e0ad708] Performed MessageBroadcastJob from Sidekiq(development_default) in 143.57ms

但随后频道实际上并没有获得任何数据。正如我所说,当它设置为 perform_now 时效果很好,但设置为 perform_later 时会出现问题。有什么想法吗?

将sidekiq更新到4.2,支持rails 5

我刚遇到同样的问题。看起来它是由默认的 cable.yml 文件引起的,该文件的设置如下:

development:
  adapter: async

test:
  adapter: async

production:
  adapter: redis
  url: redis://localhost:6379/1

但看起来异步适配器不能用于从单独的进程进行广播。要启用此功能,您可以将 cable.yml 文件修改为:

default: &default
adapter: redis
url: redis://localhost:6379/1

development:
  <<: *default

test:
  <<: *default

production:
  <<: *default

这也将允许您从控制台进行广播。