我有一个应用程序,它存储一个动作电缆通道的所有连接的计数,在subscribed
上递增它,并在unsubscribed
递减它。但是我发现了一个问题,当服务器(Puma)出现故障时,部署到Heroku会赢得unsubscribed
活动连接。因此,当新版本的应用程序旋转时,连接数将高于应有的数量。
我的特定频道的代码:
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 :(得分:0)
您可以定义一个rake任务断开所有客户端,然后使用heroku release dynos运行它(因此每次部署时都会运行):
procfile:
release: bundle exec rake reset_action_cable_consumers
rake任务:
# reset_action_cable_consumers.rake
# This should work
ActionCable.server.remote_connections.disconnect
# Other solution
App.cable.subscriptions.each{|subscription| subscription.unsubscribe()}