<commit_before>require "sensu/daemon"
require "sensu/server/filter"
require "sensu/server/mutate"
require "sensu/server/handle"

module Sensu
  module Server
    class Process
      include Daemon
      include Filter
      include Mutate
      include Handle

      attr_reader :is_leader, :handling_event_count

      # Create an instance of the Sensu server process, start the
      # server within the EventMachine event loop, and set up server
      # process signal traps (for stopping).
      #
      # @param options [Hash]
      def self.run(options={})
        server = self.new(options)
        EM::run do
          server.start
          server.setup_signal_traps
        end
      end

      # Override Daemon initialize() to support Sensu server leader
      # election and the handling event count.
      #
      # @param options [Hash]
      def initialize(options={})
        super
        @is_leader = false
        @timers[:leader] = Array.new
        @handling_event_count = 0
      end

      # Create a registration check definition for a client. Client
      # definitions may contain `:registration` configuration,
      # containing custom attributes and handler information. By
      # default, the registration check definition sets the `:handler`
      # to `registration`. If the client provides its own
      # `:registration` configuration, it's deep merged with the
      # defaults. The check `:name`, `:output`, `:status`, `:issued`,
      # and `:executed` values are always overridden to guard against
      # an invalid definition.
      def create_registration_check(client)
        check = {:handler => "registration"}
        if client.has_key?(:registration)
          check = deep_merge(check, client[:registration])
        end
        timestamp = Time.now.to_i
        overrides = {
          :name => "registration",
          :output => "new client registration",
          :status => 1,
          :issued => timestamp,
          :executed => timestamp
        }
        check.merge(overrides)
      end

      def create_client_registration_event(client)
        event = {
          :id => random_uuid,
          :client => client,
          :check => create_registration_check(client),
          :occurrences => 1,
          :action => :create,
          :timestamp => Time.now.to_i
        }
        process_event(event)
      end

      # Update the Sensu client registry, stored in Redis. Sensu
      # client data is used to provide additional event context and
      # enable agent health monitoring. The client registry supports
      # client signatures, unique string identifiers used for
      # keepalive and result source verification. If a client has a
      # signature, all further registry updates for the client must
      # have the same signature. A client can begin to use a signature
      # if one was not previously configured. JSON serialization is
      # used for the stored client data.
      #
      # @param client [Hash]
      # @param callback [Proc] to call after the the client data has
      #   been added to (or updated) the registry.
      def update_client_registry(client, &callback)
        @logger.debug("updating client registry", :client => client)
        client_key = "client:#{client[:name]}"
        signature_key = "#{client_key}:signature"
        @redis.setnx(signature_key, client[:signature]) do |created|
          create_client_registration_event(client) if created
          @redis.get(signature_key) do |signature|
            if signature.empty? && client[:signature]
              @redis.set(signature_key, client[:signature])
            end
            if signature.empty? || (client[:signature] == signature)
              @redis.set(client_key, MultiJson.dump(client)) do
                @redis.sadd("clients", client[:name]) do
                  callback.call(true)
                end
              end
            else
              @logger.warn("invalid client signature", {
                :client => client,
                :signature => signature
              })
              @logger.warn("not updating client in the registry", :client => client)
              callback.call(false)
            end
          end
        end
      end

      # Set up the client keepalive consumer, keeping the Sensu client
      # registry updated. The consumer receives JSON serialized client
      # keepalives from the transport, parses them, and calls
      # `update_client_registry()` with the client data to update the
      # registry. Transport message acknowledgements are used to<commit_msg>[client-signature] documented update registry callback<commit_after>require "sensu/daemon"
require "sensu/server/filter"
require "sensu/server/mutate"
require "sensu/server/handle"

module Sensu
  module Server
    class Process
      include Daemon
      include Filter
      include Mutate
      include Handle

      attr_reader :is_leader, :handling_event_count

      # Create an instance of the Sensu server process, start the
      # server within the EventMachine event loop, and set up server
      # process signal traps (for stopping).
      #
      # @param options [Hash]
      def self.run(options={})
        server = self.new(options)
        EM::run do
          server.start
          server.setup_signal_traps
        end
      end

      # Override Daemon initialize() to support Sensu server leader
      # election and the handling event count.
      #
      # @param options [Hash]
      def initialize(options={})
        super
        @is_leader = false
        @timers[:leader] = Array.new
        @handling_event_count = 0
      end

      # Create a registration check definition for a client. Client
      # definitions may contain `:registration` configuration,
      # containing custom attributes and handler information. By
      # default, the registration check definition sets the `:handler`
      # to `registration`. If the client provides its own
      # `:registration` configuration, it's deep merged with the
      # defaults. The check `:name`, `:output`, `:status`, `:issued`,
      # and `:executed` values are always overridden to guard against
      # an invalid definition.
      def create_registration_check(client)
        check = {:handler => "registration"}
        if client.has_key?(:registration)
          check = deep_merge(check, client[:registration])
        end
        timestamp = Time.now.to_i
        overrides = {
          :name => "registration",
          :output => "new client registration",
          :status => 1,
          :issued => timestamp,
          :executed => timestamp
        }
        check.merge(overrides)
      end

      def create_client_registration_event(client)
        event = {
          :id => random_uuid,
          :client => client,
          :check => create_registration_check(client),
          :occurrences => 1,
          :action => :create,
          :timestamp => Time.now.to_i
        }
        process_event(event)
      end

      # Update the Sensu client registry, stored in Redis. Sensu
      # client data is used to provide additional event context and
      # enable agent health monitoring. The client registry supports
      # client signatures, unique string identifiers used for
      # keepalive and result source verification. If a client has a
      # signature, all further registry updates for the client must
      # have the same signature. A client can begin to use a signature
      # if one was not previously configured. JSON serialization is
      # used for the stored client data.
      #
      # @param client [Hash]
      # @param callback [Proc] to call after the the client data has
      #   been added to (or updated) the registry.
      # @yield [success] passes success status to an optional
      #   callback/block.
      # @yieldparam success [TrueClass,FalseClass] if registry update
      #   was a success.
      def update_client_registry(client, &callback)
        @logger.debug("updating client registry", :client => client)
        client_key = "client:#{client[:name]}"
        signature_key = "#{client_key}:signature"
        @redis.setnx(signature_key, client[:signature]) do |created|
          create_client_registration_event(client) if created
          @redis.get(signature_key) do |signature|
            if signature.empty? && client[:signature]
              @redis.set(signature_key, client[:signature])
            end
            if signature.empty? || (client[:signature] == signature)
              @redis.set(client_key, MultiJson.dump(client)) do
                @redis.sadd("clients", client[:name]) do
                  callback.call(true) if callback
                end
              end
            else
              @logger.warn("invalid client signature", {
                :client => client,
                :signature => signature
              })
              @logger.warn("not updating client in the registry", :client => client)
              callback.call(false) if callback
            end
          end
        end
      end

      # Set up the client keepalive consumer, keeping the Sensu client
      # registry updated. The consumer receives JSON serialized client
      # keepalives from the transport, parses them, and calls
      # `update_client_registry()` with the client data to update the
      # registry. Transport message acknowledgements are used to
{'fraction_non_alphanumeric': 0.08218743389041676, 'fraction_numerical': 0.0006346519991537973, 'mean_word_length': 2.2391229873244263, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, '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': '3683074', 'n_tokens_mistral': 2424, 'n_tokens_neox': 2330, 'n_words': 829}