module RocketJob::Plugins::Job::Throttle

Rocket Job Throttling Framework.

Example:

# Do not run this job when the MySQL slave delay exceeds 5 minutes.
class MyJob < RocketJob::Job
  # Define a custom mysql throttle
  # Prevents all jobs of this class from running on the current server.
  define_throttle :mysql_throttle_exceeded?

  def perform
    # ....
  end

  private

  # Returns true if the MySQL slave delay exceeds 5 minutes
  def mysql_throttle_exceeded?
    status        = ActiveRecord::Base.connection.select_one('show slave status')
    seconds_delay = Hash(status)['Seconds_Behind_Master'].to_i
    seconds_delay >= 300
  end
end

Public Instance Methods

throttle_filter_class() click to toggle source

Default throttle to use when the throttle is exceeded. When the throttle has been exceeded all jobs of this class will be ignored until the next refresh. `RocketJob::Config.re_check_seconds` which by default is 60 seconds.

# File lib/rocket_job/plugins/job/throttle.rb, line 74
def throttle_filter_class
  {:_type.nin => [self.class.name]}
end
throttle_filter_id() click to toggle source

Filter out only this instance of the job. When the throttle has been exceeded this job will be ignored by this server until the next refresh. `RocketJob::Config.re_check_seconds` which by default is 60 seconds.

# File lib/rocket_job/plugins/job/throttle.rb, line 81
def throttle_filter_id
  {:id.nin => [id]}
end