2017-10-16 6 views
1

私は、actioncableチャネルへのすべての接続の数を保存し、subscribedにインクリメントし、unsubscribedでデクリメントするアプリケーションを持っています。しかし、私は、サーバ(Puma)がダウンしたときに、Herokuへの展開がアクティブな接続ではないunsubscribedという問題を発見しました。したがって、新しいバージョンのアプリケーションがスピンアップすると、接続数は必要な値よりも高くなります。Heokuに新しいコードをデプロイするときにActionCableがすべてのクライアントを切断する方法

私の特定のチャネルのためのコード:

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 

どのように私はHerokuのに展開上のすべてのアクティブな接続を解除するActionCableを強制できますか?

答えて

0

あなたはすべてのクライアントを切断rakeタスクを定義し、Herokuのリリース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()} 
+0

これは私が考えていた解決策でした。私はそれにショット – JeremyKirkham

+0

を与えますが、ページを更新しない限りユーザーは再接続しません。 –

+1

@NicolasMaloeuvreこれは質問で尋ねられなかったので、それはできません。 –

関連する問題