lib/sidekiq/throttled/job.rb in sidekiq-throttled-1.4.0 vs lib/sidekiq/throttled/job.rb in sidekiq-throttled-1.5.0
- old
+ new
@@ -11,27 +11,31 @@
#
# class MyJob
# include Sidekiq::Job
# include Sidekiq::Throttled::Job
#
- # sidekiq_options :queue => :my_queue
- # sidekiq_throttle :threshold => { :limit => 123, :period => 1.hour }
+ # sidkiq_options :queue => :my_queue
+ # sidekiq_throttle :threshold => { :limit => 123, :period => 1.hour },
+ # :requeue => { :to => :other_queue, :with => :schedule }
#
# def perform
# # ...
# end
# end
#
# @see ClassMethods
module Job
+ VALID_VALUES_FOR_REQUEUE_WITH = %i[enqueue schedule].freeze
+
# Extends worker class with {ClassMethods}.
#
# @note Using `included` hook with extending worker with {ClassMethods}
# in order to make API inline with `include Sidekiq::Job`.
#
# @private
def self.included(base)
+ base.sidekiq_class_attribute :sidekiq_throttled_requeue_options
base.extend(ClassMethods)
end
# Helper methods added to the singleton class of destination
module ClassMethods
@@ -69,12 +73,32 @@
# :threshold => { :limit => 123, :period => 1.hour },
# :concurrency => { :limit => 10 }
# })
# end
#
+ # @example Allow max 123 MyJob jobs per hour; when jobs are throttled, schedule them for later in :other_queue
+ #
+ # class MyJob
+ # include Sidekiq::Job
+ # include Sidekiq::Throttled::Job
+ #
+ # sidekiq_throttle({
+ # :threshold => { :limit => 123, :period => 1.hour },
+ # :requeue => { :to => :other_queue, :with => :schedule }
+ # })
+ # end
+ #
+ # @param [Hash] requeue What to do with jobs that are throttled
# @see Registry.add
# @return [void]
def sidekiq_throttle(**kwargs)
+ requeue_options = Throttled.config.default_requeue_options.merge(kwargs.delete(:requeue) || {})
+ unless VALID_VALUES_FOR_REQUEUE_WITH.include?(requeue_options[:with])
+ raise ArgumentError, "requeue: #{requeue_options[:with]} is not a valid value for :with"
+ end
+
+ self.sidekiq_throttled_requeue_options = requeue_options
+
Registry.add(self, **kwargs)
end
# Adds current worker to preconfigured throttling strategy. Allows
# sharing same pool for multiple workers.