【问题标题】:Getting Action Cable to broadcast in Sidekiq Job让动作电缆在 Sidekiq 作业中广播
【发布时间】:2017-07-11 16:15:29
【问题描述】:

我在 docker 环境中使用 Rails 5,我可以使用 worker.new.perform 让 Action Cable 在 Sidekiq 工作人员上完美播放。

但对于我来说,在使用 worker.perform_async 时我无法让它广播。

这是我的 cable.yml:

default: &default
 adapter: redis
 url: <%= ENV['REDIS_PROVIDER'] %>

development:
 <<: *default

test:
  <<: *default

production:
  <<: *default

这是我的工人:

class AdminWorker
  include Sidekiq::Worker

  def perform
    ActionCable.server.broadcast 'admin_channel', content: 'hello'
  end
 end

我的管理员频道:

class AdminChannel < ApplicationCable::Channel
 def subscribed
  stream_from "admin_channel"
 end

 def unsubscribed
  # Any cleanup needed when channel is unsubscribed
 end
end

正如我之前提到的,这在调用 AdminWorker.new.perform 时工作得很好。一旦我尝试运行 AdminWorker.perform_async,电缆将不会广播,并且对于日志中的操作电缆没有任何帮助。我在这里错过了什么?

【问题讨论】:

  • 我也面临同样的问题。你找到解决办法了吗?

标签: ruby-on-rails sidekiq actioncable


【解决方案1】:

我遇到了同样的问题。遇到了这个答案:ActionCable.server.broadcast from the console - 为我工作。基本上只是改了cable.yml

development:
  adapter: async

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

我能够从控制台、模型、Sidekiq 工作人员类等进行广播。

【讨论】:

  • 谢谢,这正是我的问题,无法让我的工作人员广播和同步我的观点,这解决了问题,特别是提供的链接
【解决方案2】:

【讨论】:

    【解决方案3】:

    在开发环境中 默认情况下,ActionCable 只能在 rails 服务器上运行。它不适用于 Sidekiq、控制台、cron 作业等。 因为config/cable.yml 开发服务器适配器设置为异步

    解决方案: 对于 ActionCable 进程间广播,您必须将适配器设置为 Redis

    config/cable.yml

    development:
     adapter: redis
     url: redis://localhost:6379/1
     channel_prefix: project_name_development
    

    【讨论】:

      猜你喜欢
      • 2023-04-07
      • 1970-01-01
      • 2013-10-15
      • 2018-04-20
      • 2018-07-29
      • 2016-11-23
      • 2016-08-04
      • 1970-01-01
      相关资源
      最近更新 更多