<commit_before>require 'cgi'
require 'socket'
require_relative 'logging'
require_relative 'rpc_server'

module Kontena
  class WebsocketClient
    include Kontena::Logging

    LOG_NAME = 'WebsocketClient'
    KEEPALIVE_TIME = 30

    attr_reader :api_uri, :api_token, :ws
    delegate :on, to: :ws

    ##
    # @param [String] api_uri
    # @param [String] api_token
    def initialize(api_uri, api_token)
      @api_uri = api_uri
      @api_token = api_token.to_s
      logger.info(LOG_NAME) { "initialized with token #{@api_token}" }
      @connection_retries = 0
      @subscribers = {}
      @rpc_server = Kontena::RpcServer.new
    end

    def connect
      logger.debug(LOG_NAME) { 'connecting' }
      headers = {
          'Kontena-Grid-Token' => self.api_token.to_s,
          'Kontena-Node-Id' => host_id.to_s
      }
      @ws = Faye::WebSocket::Client.new(self.api_uri, nil, {ping: KEEPALIVE_TIME, headers: headers})

      Pubsub.publish('websocket:connect', self)

      @ws.on :open do |event|
        logger.info(LOG_NAME) { 'connection established' }
        @connection_retries = 0
      end
      @ws.on :message do |event|
        self.on_message(@ws, event)
      end
      @ws.on :close do |event|
        logger.info(LOG_NAME) { "connection closed with code: #{event.code}" }
        @connection_retries += 1
        sleep (2 ** @connection_retries)
        self.connect
      end
      @ws.on :error do |event|
        logger.info(LOG_NAME) { "connection closed with error: #{event.message}" }
        @connection_retries += 1
        sleep (2 ** @connection_retries)
        self.connect
      end
    end

    ##
    # @param [String, Array] msg<commit_msg>always retry connection after 1s<commit_after>require 'cgi'
require 'socket'
require_relative 'logging'
require_relative 'rpc_server'

module Kontena
  class WebsocketClient
    include Kontena::Logging

    LOG_NAME = 'WebsocketClient'
    KEEPALIVE_TIME = 30

    attr_reader :api_uri, :api_token, :ws
    delegate :on, to: :ws

    ##
    # @param [String] api_uri
    # @param [String] api_token
    def initialize(api_uri, api_token)
      @api_uri = api_uri
      @api_token = api_token.to_s
      logger.info(LOG_NAME) { "initialized with token #{@api_token}" }
      @subscribers = {}
      @rpc_server = Kontena::RpcServer.new
    end

    def connect
      logger.debug(LOG_NAME) { 'connecting' }
      headers = {
          'Kontena-Grid-Token' => self.api_token.to_s,
          'Kontena-Node-Id' => host_id.to_s
      }
      @ws = Faye::WebSocket::Client.new(self.api_uri, nil, {ping: KEEPALIVE_TIME, headers: headers})

      Pubsub.publish('websocket:connect', self)

      @ws.on :open do |event|
        logger.info(LOG_NAME) { 'connection established' }
      end
      @ws.on :message do |event|
        self.on_message(@ws, event)
      end
      @ws.on :close do |event|
        logger.info(LOG_NAME) { "connection closed with code: #{event.code}" }
        sleep 1
        self.connect
      end
      @ws.on :error do |event|
        logger.info(LOG_NAME) { "connection closed with error: #{event.message}" }
      end
    end

    ##
    # @param [String, Array] msg
{'fraction_non_alphanumeric': 0.11870277078085642, 'fraction_numerical': 0.003778337531486146, 'mean_word_length': 2.606129398410897, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2748268', 'n_tokens_mistral': 1081, 'n_tokens_neox': 1058, 'n_words': 238}