def monitor
      while connections
        sleep 1 # Pause momentarily

        # On occasion, check our connection statuses
        next unless Time.now.to_i % check_interval == 0

        # Get tokens and connection statuses
        tokens_status = storage.get_all(tokens_key)
        rtm_status = storage.get_all(teams_key)

        # Manage connections
        connections.each do |cid, conn|
          id, _s = cid.split(':')

          # Remove/continue if connection is/will close or no longer connected
          if !conn
            warning("Removing: #{id} (Reason: rtm_not_connection)")
            destroy(cid: cid)

          elsif !conn.connected?
            warning("Removing: #{conn.id} (Reason: #{conn.status})")
            to_remove = ['token_revoked'].include?((conn.status || '').to_s)
            destroy(cid: cid, remove_token: to_remove)

          # Team is no longer valid, remove
          elsif tokens_status[conn.id].nil? || tokens_status[conn.id].empty?
            warning("Removing: #{conn.id} (Reason: token_missing)")
            destroy(cid: cid, remove_token: true)

          elsif rtm_status[conn.id] == 'destroy'
            warning("Removing: #{conn.id} (Reason: token_destroy)")
            destroy(cid: cid)

          # Kill connection if token has changed, will re-create below
          elsif tokens_status[conn.id] != conn.token
            warning("Removing: #{conn.id} (Reason: new_token)")
            destroy(cid: cid)

          # Connection should re-create unless active, will update below
          elsif rtm_status[conn.id] != 'active'
            reason = rtm_status[conn.id] || '(unknown)'
            warning("Restarting: #{conn.id} (Reason: #{reason})")
            destroy(cid: cid)
            storage.set(tokens_key, conn.id, tokens_status[conn.id])
          end
        end

        # Pause before reconnects, as destroy might be processing in threads
        sleep 1

        # Check for new tokens / reconnections
        # (reload keys since we might modify if bad). Kill and recreate
        tokens_status = storage.get_all(tokens_key)
        rtm_status = storage.get_all(teams_key)
        tokens_diff = (tokens_status.keys - rtm_status.keys) + (rtm_status.keys - tokens_status.keys)

        unless tokens_diff.empty?
          tokens_diff.uniq.each do |id|
            warning("Starting: #{id}")
            destroy(id: id)
            create(id, tokens_status[id])
          end
        end

        info("Active Connections: [#{connections.count}]")
      end
    end
def monitor
      while connections
        sleep 1 # Pause momentarily

        # On occasion, check our connection statuses
        next unless Time.now.to_i % check_interval == 0

        # Get tokens and connection statuses
        tokens_status = storage.get_all(tokens_key)
        rtm_status = storage.get_all(teams_key)

        # Manage connections
        connections.each do |cid, conn|
          id, _s = cid.split(':')

          # Remove/continue if connection is/will close or no longer connected
          if !conn
            warning("Removing: #{id} (Reason: rtm_not_connection)")
            destroy(cid: cid)

          elsif !conn.connected?
            warning("Removing: #{conn.id} (Reason: #{conn.status})")
            to_remove = ['token_revoked'].include?((conn.status || '').to_s)
            destroy(cid: cid, remove_token: to_remove)

          # Team is no longer valid, remove
          elsif tokens_status[conn.id].nil? || tokens_status[conn.id].empty?
            warning("Removing: #{conn.id} (Reason: token_missing)")
            destroy(cid: cid, remove_token: true)

          elsif rtm_status[conn.id] == 'destroy'
            warning("Removing: #{conn.id} (Reason: token_destroy)")
            destroy(cid: cid)

          # Kill connection if token has changed, will re-create below
          elsif tokens_status[conn.id] != conn.token
            warning("Removing: #{conn.id} (Reason: new_token)")
            destroy(cid: cid)

          # Connection should re-create unless active, will update below
          elsif rtm_status[conn.id] != 'active'
            reason = rtm_status[conn.id] || '(unknown)'
            warning("Restarting: #{conn.id} (Reason: #{reason})")
            destroy(cid: cid)
            storage.set(tokens_key, conn.id, tokens_status[conn.id])
          end
        end

        # Pause before reconnects, as destroy might be processing in threads
        sleep 1

        # Check for new tokens / reconnections
        # (reload keys since we might modify if bad). Kill and recreate
        tokens_status = storage.get_all(tokens_key)
        rtm_status = storage.get_all(teams_key)
        tokens_diff = (tokens_status.keys - rtm_status.keys) + (rtm_status.keys - tokens_status.keys)

        unless tokens_diff.empty?
          tokens_diff.uniq.each do |id|
            warning("Starting: #{id}")
            destroy(id: id)
            create(id, tokens_status[id])
          end
        end

        info("Active Connections: [#{connections.count}]")
      end
    end
Monitor our connections, while connections is {...}