2017-10-16 15 views
1

모든 작업 연결 수를 저장하고 subscribed에 증가시키고 unsubscribed에서 감소시키는 응용 프로그램이 있습니다. 그러나 서버 (푸마)가 다운되었을 때 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 

어떻게 ActionCable이에게 Heroku에 배치의 모든 활성 연결을 탈퇴 강요하는 걸까?

답변

0

당신은 모든 클라이언트 연결을 해제 갈퀴 작업을 정의하고 Heroku가 해제 동력계와 실행 (그래서 배포 할 때마다 실행) 수 :

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 질문에서 질문하지 않았으므로 그렇지 않습니다. –