Skip to content

Instantly share code, notes, and snippets.

@spiegela
Created March 13, 2011 06:16
Show Gist options
  • Save spiegela/867923 to your computer and use it in GitHub Desktop.
Save spiegela/867923 to your computer and use it in GitHub Desktop.
Class to check # of requests per window
# From: https://github.com/orionz/minion/blob/master/lib/minion.rb
...
def run
log "Starting minion"
Signal.trap('INT') { AMQP.stop{ EM.stop } }
Signal.trap('TERM'){ AMQP.stop{ EM.stop } }
EM.run do
AMQP.start(amqp_config) do
MQ.prefetch(1)
check_all
end
end
end
...
class RequestCounter
attr :requests, :window_size, :maximum
def initialize opts={}
@window_size = opts[:window_size] || 300
@maximum = opts[:maximum] || 300
@requests = Array.new
end
def maximum= max
@maximum = max.to_i
end
def count
clear_expired_times and requests.size
end
def increment!
clear_expired_times and requests << Time.now
end
def at_max?
count >= maximum
end
private
def clear_expired_times
requests.delete_if{ |time| time < Time.now - window_size }
end
end
require 'rubygems'
require 'bundler'
Bundler.require
require File.join '.', File.dirname(__FILE__), '..', 'lib', 'api_checker'
require File.join '.', File.dirname(__FILE__), '..', 'lib', 'request_counter'
include Minion
@api_checker = ApiChecker.new
@counter = RequestCounter.new window_size: 60, maximum: 60
job "check_api" :when => lambda{ not @counter.at_max? } do |args|
@counter.increment!
id = @api_checker.check args[0]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment