Sha256: 066cb20c059a60df6d2b234a9b038657572749306c78ab4811e23fffe77b8643

Contents?: true

Size: 854 Bytes

Versions: 9

Compression:

Stored size: 854 Bytes

Contents

require "resque/throttle"

module Resque
  extend self

  # Raised when trying to create a job that is throttled
  class ThrottledError < RuntimeError; end
      
  def enqueue_with_throttle(klass, *args)
    if should_throttle?(klass)
      raise ThrottledError.new("#{klass} with key #{klass.key} has exceeded it's throttle limit")
    end
    enqueue_without_throttle(klass, *args)
  end
  alias_method :enqueue_without_throttle, :enqueue
  alias_method :enqueue, :enqueue_with_throttle

  private
   
  def should_throttle?(klass)
    return false if !throttle_job?(klass) || klass.disabled
    return true if key_found?(klass)
    redis.set(klass.key, true, klass.can_run_every)
    return false
  end

  def key_found?(klass)
     redis.get(klass.key)
  end

  def throttle_job?(klass)
    klass.ancestors.include?(Resque::ThrottledJob)  
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
resque-throttle-0.2.9 lib/resque/resque.rb
resque-throttle-0.2.8 lib/resque/resque.rb
resque-throttle-0.2.7 lib/resque/resque.rb
resque-throttle-0.2.6 lib/resque/resque.rb
resque-throttle-0.2.4 lib/resque/resque.rb
resque-throttle-0.2.3 lib/resque/resque.rb
resque-throttle-0.2.2 lib/resque/resque.rb
resque-throttle-0.2.1 lib/resque/resque.rb
resque-throttle-0.2.0 lib/resque/resque.rb