<commit_before>require 'nagios'
require 'singleton'

module Nagios
  ##
  # Background parsing of status.dat file in separate thread. Runs on
  # regular intervals defined by :ttl
  #
  class BackgroundParser
    include Singleton

    def initialize
      @use_inflight_flag = false
      @ttl = @@ttl
      @start = @@start
    end

    attr_accessor :ttl, :start, :use_inflight_flag

    class << self
      def ttl= ttl
        @@ttl = ttl
      end

      def start= start
        @@start = start
      end
    end

    def configured?
       ttl > 0 && start
    end

    ##
    # Is BG parser thread running
    #
    def alive?
      !@bg.nil? && @bg.alive?
    end

    ##
    # See alive?
    def dead?
      !alive?
    end

    ##
    # Start BG Parser if it's configured to run and TTL is defined
    def run
      if configured? && dead?
        puts "[#{Time.now}] Starting background parser thread with interval #{ttl} sec"
        @bg = Thread.new {
          loop {
            if  use_inflight_flag = !use_inflight_flag
              $nagios[:status].parse
            else
              $nagios[:status_inflight].parse
            end
            sleep interval
          }
        }
      end
    end

  end
end
<commit_msg>Move to class level methods.<commit_after>require 'nagios'
require 'singleton'

module Nagios
  ##
  # Background parsing of status.dat file in separate thread. Runs on
  # regular intervals defined by :ttl
  #
  class BackgroundParser
    include Singleton

    attr_accessor :use_inflight_flag

    class << self
      ##
      # @ttl (Fixint, seconds) defines re-parsing interval for the
      # BackgroundParser.
      #
      # Set @@ttl after initialisation, to be able to pass
      #  configuration varialbles.
      #
      # @see start=
      #
      # Example:
      #     Nagios::BackgroundParser.ttl = ::DEFAULT[:ttl].to_i
      #     Nagios::BackgroundParser.start = ::DEFAULT[:start_background_parser]
      #     Nagios::BackgroundParser.instance.run
      #
      def ttl= ttl
        @ttl = ttl
      end

      ##
      # @start (Boolean) defines whether BackgroundParser should be
      # started.
      #
      # Set :start variable after initialisation, to be able to pass
      #  configuration values.
      #
      # @see ttl=
      #
      # Example:
      #     Nagios::BackgroundParser.ttl = ::DEFAULT[:ttl].to_i
      #     Nagios::BackgroundParser.start = ::DEFAULT[:start_background_parser]
      #     Nagios::BackgroundParser.instance.run
      #
      def start= start
        @start = start
      end

      ##
      # Is BackgroundParser configured to run?
      def configured?
        @ttl > 0 && @start
      end

      ##
      # Is BG parser thread running
      #
      def alive?
        !@bg.nil? && @bg.alive?
      end

      ##
      # See alive?
      def dead?
        !alive?
      end

      ##
      # Start BG Parser if it's configured to run and TTL is defined
      def run
        if configured? && dead?
          puts "[#{Time.now}] Starting background parser thread with interval #{@ttl} sec"
          @bg = Thread.new {
            loop {
              if  @use_inflight_flag = !@use_inflight_flag
                $nagios[:status].parse
              else
                $nagios[:status_inflight].parse
              end
              sleep @ttl
            }
          }
        end
      end


    end
  end
end

{'fraction_non_alphanumeric': 0.08769365682548963, 'fraction_numerical': 0.0005846243788365975, 'mean_word_length': 1.6713505074160813, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '1053535', 'n_tokens_mistral': 1031, 'n_tokens_neox': 1005, 'n_words': 290}