Sha256: 93e8d52133a65d70a639df789787ef53804e6a8b297b5b8898caa2df67653cc9

Contents?: true

Size: 1.96 KB

Versions: 3

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

module Mihari
  #
  # Yet another base service class for Analyzer, Emitter and Enricher
  #
  class Actor
    include Dry::Monads[:result, :try]

    include Mixins::Configurable
    include Mixins::Retriable

    # @return [Hash]
    attr_reader :options

    #
    # @param [Hash, nil] options
    #
    def initialize(options: nil)
      super()

      @options = options || {}
    end

    #
    # @return [Integer]
    #
    def retry_interval
      options[:retry_interval] || Mihari.config.retry_interval
    end

    #
    # @return [Boolean]
    #
    def retry_exponential_backoff
      options[:retry_exponential_backoff] || Mihari.config.retry_exponential_backoff
    end

    #
    # @return [Integer]
    #
    def retry_times
      options[:retry_times] || Mihari.config.retry_times
    end

    #
    # @return [Integer, nil]
    #
    def timeout
      options[:timeout]
    end

    def validate_configuration!
      return if configured?

      joined = configuration_keys.join(", ")
      be = (configuration_keys.length > 1) ? "are" : "is"
      message = "#{self.class.class_key} is not configured correctly. #{joined} #{be} missing."
      raise ConfigurationError, message
    end

    def call(*args, **kwargs)
      raise NotImplementedError, "You must implement #{self.class}##{__method__}"
    end

    def result(...)
      Try[StandardError] do
        retry_on_error(
          times: retry_times,
          interval: retry_interval,
          exponential_backoff: retry_exponential_backoff
        ) { call(...) }
      end.to_result
    end

    class << self
      #
      # @return [String]
      #
      def class_key
        to_s.split("::").last
      end

      #
      # @return [Array<String>, nil]
      #
      def class_key_aliases
        nil
      end

      #
      # @return [Array<String>]
      #
      def class_keys
        ([class_key] + [class_key_aliases]).flatten.compact.map(&:downcase)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mihari-6.0.0 lib/mihari/actor.rb
mihari-5.7.2 lib/mihari/actor.rb
mihari-5.7.1 lib/mihari/actor.rb