Sha256: dfad8f0c6d64ffcf69bfaad72818fe61d1791b225984cc8597467c19150d1913

Contents?: true

Size: 1.03 KB

Versions: 24

Compression:

Stored size: 1.03 KB

Contents

module RocketJob
  class ThrottleDefinition
    attr_reader :method_name, :filter

    def initialize(method_name, filter)
      @method_name = method_name.to_sym
      @filter      = filter
    end

    # Returns [true|false] whether the throttle was triggered.
    def throttled?(job, *args)
      # Throttle exceeded?
      # Throttle methods can be private.
      throttled =
        if args.size.positive?
          job.method(method_name).arity.zero? ? job.send(method_name) : job.send(method_name, *args)
        else
          job.send(method_name)
        end
      return false unless throttled

      job.logger.debug { "Throttle: #{method_name} has been exceeded." }
      true
    end

    # Returns the filter to apply to the job when the above throttle returns true.
    def extract_filter(job, *args)
      return filter.call(job, *args) if filter.is_a?(Proc)

      if args.size.positive?
        job.method(filter).arity.zero? ? job.send(filter) : job.send(filter, *args)
      else
        job.send(filter)
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
rocketjob-6.3.1 lib/rocket_job/throttle_definition.rb
rocketjob-6.3.0 lib/rocket_job/throttle_definition.rb
rocketjob-6.2.0 lib/rocket_job/throttle_definition.rb
rocketjob-6.1.1 lib/rocket_job/throttle_definition.rb
rocketjob-6.1.0 lib/rocket_job/throttle_definition.rb
rocketjob-6.0.3 lib/rocket_job/throttle_definition.rb
rocketjob-6.0.2 lib/rocket_job/throttle_definition.rb
rocketjob-6.0.1 lib/rocket_job/throttle_definition.rb
rocketjob-6.0.0 lib/rocket_job/throttle_definition.rb
rocketjob-6.0.0.rc3 lib/rocket_job/throttle_definition.rb
rocketjob-6.0.0.rc2 lib/rocket_job/throttle_definition.rb
rocketjob-6.0.0.rc1 lib/rocket_job/throttle_definition.rb
rocketjob-5.4.1 lib/rocket_job/throttle_definition.rb
rocketjob-5.4.0 lib/rocket_job/throttle_definition.rb
rocketjob-5.4.0.beta2 lib/rocket_job/throttle_definition.rb
rocketjob-5.4.0.beta1 lib/rocket_job/throttle_definition.rb
rocketjob-5.3.3 lib/rocket_job/throttle_definition.rb
rocketjob-5.3.2 lib/rocket_job/throttle_definition.rb
rocketjob-5.3.1 lib/rocket_job/throttle_definition.rb
rocketjob-5.3.0 lib/rocket_job/throttle_definition.rb