让行动电缆在Sidekiq Job中播出

时间:2017-02-21 20:39:12

标签: ruby-on-rails sidekiq actioncable

我在Docker环境中使用Rails 5,我可以使用worker.new.perform让Action Cable在Sidekiq worker上完全正常播放。

但对于我的生活,我在使用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,电缆就不会广播,对日志中的动作电缆没有任何帮助。我在这里缺少什么?

3 个答案:

答案 0 :(得分:15)

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

development:
  adapter: async

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

我可以从控制台,模型,Sidekiq工人类等广播

答案 1 :(得分:0)

答案 2 :(得分:0)

在开发环境中 默认情况下,ActionCable仅在rails服务器上运行。而且在Sidekiq,控制台,cron作业等中将无法使用。 因为,在 config / cable.yml 开发服务器适配器中将其设置为异步

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

config / cable.yml

names = ["raja","laxman","jay","ajay","lopo", "tost","lola","manish", "Vishal"]

def get_rand():
    if not len(names):
        print("No more names to pick from")
        exit()
    idx = random.randint(0, len(names)-1)
    name = names[idx]
    del names[idx]
    return name
    
for i in range(10):
    print(get_rand())