Ruby on rails 如何在向Heroku部署新代码时强制ActionCable断开所有客户端的连接
我有一个应用程序,它存储到actioncable频道的所有连接计数,在Ruby on rails 如何在向Heroku部署新代码时强制ActionCable断开所有客户端的连接,ruby-on-rails,heroku,actioncable,Ruby On Rails,Heroku,Actioncable,我有一个应用程序,它存储到actioncable频道的所有连接计数,在订阅时递增,在取消订阅时递减。然而,我发现了一个问题,即当服务器(Puma)停机时,部署到Heroku不会取消订阅活动连接。因此,当应用程序的新版本启动时,连接计数将高于其应有的值 我的特定频道的代码: class PostChannel < ApplicationCable::Channel attr_reader :subscribers def subscribed channel_name =
订阅时递增,在取消订阅时递减。然而,我发现了一个问题,即当服务器(Puma)停机时,部署到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
class PostChannel 用户ID您可以定义一个断开所有客户端连接的rake任务,然后使用heroku release dynos运行它(因此它在每次部署时都会运行):
程序文件:
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()}
是的,这就是我所想的解决方案,我会试一试,但它不会重新连接用户,除非他们刷新页面?@NicolasMaloeuvre问题中没有问到这一点,所以不会。实际上@NicolasMaloeuvre我正在使用的客户端库(ActionCable npm包),如果它错过服务器的2次心跳ping,它会重新连接,所以它应该会