<commit_before>module EtTools
  class NatMonitor
    require 'net/http'
    require 'net/ping'
    require 'fog'
    require 'yaml'
    require 'syslog'

    def initialize(conf_file = nil)
      @conf = defaults.merge load_conf(conf_file)
    end

    def load_conf(conf_file = nil)
      YAML.load_file(conf_file || '/etc/nat_monitor.yml')
    end

    def run
      validate!
      output 'Starting NAT Monitor'
      main_loop
    end

    def validate!
      case
      when !@conf['route_table_id']
        output 'route_table_id not specified'
        exit 1
      when !route_exists?(@conf['route_table_id'])
        output "Route #{@conf['route_table_id']} not found"
        exit 2
      when @conf['nodes'].count < 3
        output '3 or more nodes are required to create a quorum'
        exit 3
      end
    end

    def defaults
      { 'pings' => 3,
        'ping_timeout' => 1,
        'heartbeat_interval' => 10 }
    end

    def main_loop
      loop do
        heartbeat
        sleep @conf['heartbeat_interval']
      end
    end

    def heartbeat
      if am_i_master?
        output "Looks like I'm the master"
        return
      end
      un = unreachable_nodes
      return if un.empty?
      if un.count == other_nodes.keys.count # return if I'm unreachable...
        output "No nodes are reachable. Seems I'm the unreachable one."
        return
      end
      cm = current_master
      unless un.include?(cm) # ...unless master is unreachable
        output "Unreachable nodes: #{un.inspect}"
        output "Current master (#{cm}) is still reachable"
        return
      end<commit_msg>Catch all potential errors and keep going<commit_after>module EtTools
  class NatMonitor
    require 'net/http'
    require 'net/ping'
    require 'fog'
    require 'yaml'
    require 'syslog'

    def initialize(conf_file = nil)
      @conf = defaults.merge load_conf(conf_file)
    end

    def load_conf(conf_file = nil)
      YAML.load_file(conf_file || '/etc/nat_monitor.yml')
    end

    def run
      validate!
      output 'Starting NAT Monitor'
      main_loop
    end

    def validate!
      case
      when !@conf['route_table_id']
        output 'route_table_id not specified'
        exit 1
      when !route_exists?(@conf['route_table_id'])
        output "Route #{@conf['route_table_id']} not found"
        exit 2
      when @conf['nodes'].count < 3
        output '3 or more nodes are required to create a quorum'
        exit 3
      end
    end

    def defaults
      { 'pings' => 3,
        'ping_timeout' => 1,
        'heartbeat_interval' => 10 }
    end

    def main_loop
      loop do
        begin
          heartbeat
        rescue => e
          output "Caught #{e.class} exception: #{e.message}"
          output e.backtrace
        end
        sleep @conf['heartbeat_interval']
      end
    end

    def heartbeat
      if am_i_master?
        output "Looks like I'm the master"
        return
      end
      un = unreachable_nodes
      return if un.empty?
      if un.count == other_nodes.keys.count # return if I'm unreachable...
        output "No nodes are reachable. Seems I'm the unreachable one."
        return
      end
      cm = current_master
      unless un.include?(cm) # ...unless master is unreachable
        output "Unreachable nodes: #{un.inspect}"
        output "Current master (#{cm}) is still reachable"
        return
      end
{'fraction_non_alphanumeric': 0.08744131455399061, 'fraction_numerical': 0.00528169014084507, 'mean_word_length': 2.000880281690141, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, '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': '6467011', 'n_tokens_mistral': 1058, 'n_tokens_neox': 1010, 'n_words': 314}