2017-10-16 51 views
1

我有一个应用程序,它存储了一个到一个actioncable通道的所有连接的计数,在subscribed上递增,并在unsubscribed上递减。然而,我发现当服务器(Puma)关闭时,部署到Heroku的问题不会成为活动连接unsubscribed。因此,当新版本的应用程序启动时,连接计数比应该高。如何强制ActionCable在Heroku中部署新代码时断开所有客户端

代码为我的特殊通道:

class PostChannel < ApplicationCable::Channel 
    attr_reader :subscribers 

    def subscribed 
    channel_name = "TestChannel#{params[:post_id]}" 
    stream_from channel_name 
    user_ids = active_users channel_name 
    user_ids << connection.current_user.id 
    update_users channel_name, user_ids 
    end 

    def unsubscribed 
    channel_name = "TestChannel#{params[:post_id]}" 
    user_ids = active_users channel_name 
    user_ids.delete_at(user_ids.index(connection.current_user.id) || 
         user_ids.length) 
    update_users channel_name, user_ids 
    end 

    def active_users(channel_name) 
    JSON.parse(Redis.current.hget('actioncable', channel_name) || '[]') 
    end 

    def update_users(channel_name, user_ids) 
    Redis.current.hset('actioncable', channel_name, user_ids.to_json) 
    ActionCable.server.broadcast(
     channel_name, 
     users: user_ids, 
     action: 'UsersChanged' 
    ) 
    end 
end 

如何强制ActionCable退订上部署到Heroku的所有活动连接?

回答

0

你可以定义一个rake任务断开所有客户端,然后用Heroku的释放DYNOS运行(所以它会在您每次部署时间):

procfile:

release: bundle exec rake reset_action_cable_consumers 

耙子任务:

# reset_action_cable_consumers.rake 

# This should work 
ActionCable.server.remote_connections.disconnect 

# Other solution 
App.cable.subscriptions.each{|subscription| subscription.unsubscribe()} 
+0

是的,这是我想到的解决方案,我会给它一个 – JeremyKirkham

+0

但它不会重新连接用户,除非他们刷新页面? –

+1

@NicolasMaloeuvre这个问题没有问,所以不会。 –