Sha256: 33c1cdcf278f6525506fe48b86397b9b4e5e8089dc594582293cfc27343d8622

Contents?: true

Size: 1.97 KB

Versions: 8

Compression:

Stored size: 1.97 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 Concerns::Configurable
    include Concerns::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 = self.class.configuration_keys.join(", ")
      be = (self.class.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) do
          call(...)
        end
      end.to_result
    end

    class << self
      #
      # @return [String]
      #
      def class_key
        to_s.split("::").last.downcase
      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

8 entries across 8 versions & 1 rubygems

Version Path
mihari-7.1.1 lib/mihari/actor.rb
mihari-7.1.0 lib/mihari/actor.rb
mihari-7.0.5 lib/mihari/actor.rb
mihari-7.0.4 lib/mihari/actor.rb
mihari-7.0.3 lib/mihari/actor.rb
mihari-7.0.2 lib/mihari/actor.rb
mihari-7.0.1 lib/mihari/actor.rb
mihari-7.0.0 lib/mihari/actor.rb