Skip to content

Commit

Permalink
adding Config
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmurmann committed Feb 10, 2011
1 parent 77c59ea commit 2b78510
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 7 deletions.
34 changes: 34 additions & 0 deletions lib/resque/plugins/heroku_autoscaler/config.rb
@@ -0,0 +1,34 @@
module Resque
module Plugins
module HerokuAutoscaler
module Config
extend self

@new_worker_count = Proc.new {|pending| pending >0 ? 1 : 0}

attr_writer :heroku_user
def heroku_user
@heroku_user || ENV['HEROKU_USER']
end

attr_writer :heroku_pass
def heroku_pass
@heroku_pass || ENV['HEROKU_PASS']
end

attr_writer :heroku_app
def heroku_app
@heroku_app || ENV['HEROKU_APP']
end

def new_worker_count(pending=nil, *payload, &calculate_count)
if calculate_count
@new_worker_count = calculate_count
else
@new_worker_count.call(pending, *payload)
end
end
end
end
end
end
3 changes: 3 additions & 0 deletions lib/resque/plugins/heroku_autoscaler/version.rb
@@ -0,0 +1,3 @@
module Resque::Plugins::HerokuAutoscaler
VERSION = "0.1.0"
end
11 changes: 9 additions & 2 deletions lib/resque/plugins/resque_heroku_autoscaler.rb
@@ -1,3 +1,5 @@
require 'resque/plugins/heroku_autoscaler/config'

module Resque
module Plugins
module HerokuAutoscaler
Expand All @@ -12,11 +14,16 @@ def after_perform_scale_workers_down(*args)
end

def set_workers(number_of_workers)
heroku_client.set_workers(ENV['HEROKU_APP'], number_of_workers)
heroku_client.set_workers(Resque::Plugins::HerokuAutoscaler::Config.heroku_app, number_of_workers)
end

def heroku_client
@@heroku_client || @@heroku_client = Heroku::Client.new(ENV['HEROKU_USER'], ENV['HEROKU_PASS'])
@@heroku_client || @@heroku_client = Heroku::Client.new(Resque::Plugins::HerokuAutoscaler::Config.heroku_user,
Resque::Plugins::HerokuAutoscaler::Config.heroku_pass)
end

def self.config
yield Resque::Plugins::HerokuAutoscaler::Config
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion resque-heroku-autoscaler.gemspec
Expand Up @@ -4,7 +4,7 @@ require 'resque/version'
Gem::Specification.new do |s|
s.name = "resque-heroku-autoscaler"
s.date = Time.now.strftime('%Y-%m-%d')
s.version = "0.1.0"
s.version = Resque::Plugins::HerokuAutoscaler::VERSION
s.summary = "Resque plugin to autoscale your workers on Heroku"
s.homepage = "https://github.com/ajmurmann/resque-heroku-autoscaler"
s.authors = ["Alexander Murmann"]
Expand Down
86 changes: 86 additions & 0 deletions spec/config_spec.rb
@@ -0,0 +1,86 @@
require 'rspec'
require 'heroku'
require 'resque'
require 'resque/plugins/heroku_autoscaler/config'

describe Resque::Plugins::HerokuAutoscaler::Config do
describe ".heroku_user" do
it "stores the given heroku user name" do
subject.heroku_user = "my_user@example.com"
subject.heroku_user.should == "my_user@example.com"
end

it "defaults to HEROKU_USER environment variable" do
subject.heroku_user = nil
ENV["HEROKU_USER"] = "user@example.com"
subject.heroku_user.should == "user@example.com"
end
end

describe ".heroku_pass" do
it "stores the given heroku password" do
subject.heroku_pass = "password"
subject.heroku_pass.should == "password"
end

it "defaults to HEROKU_PASS environment variable" do
subject.heroku_pass = nil
ENV["HEROKU_PASS"] = "123"
subject.heroku_pass.should == "123"
end
end

describe ".heroku_app" do
it "stores the given heroku application name" do
subject.heroku_app = "my-grand-app"
subject.heroku_app.should == "my-grand-app"
end

it "defaults to HEROKU_APP environment variable" do
subject.heroku_app = nil
ENV["HEROKU_APP"] = "yaa"
subject.heroku_app.should == "yaa"
end
end

describe ".new_worker_count" do
before do
@original_method = Resque::Plugins::HerokuAutoscaler::Config.instance_variable_get(:@new_worker_count)
end

after do
Resque::Plugins::HerokuAutoscaler::Config.instance_variable_set(:@new_worker_count, @original_method)
end

it "should store a block as a Proc" do
subject.new_worker_count do |pending|
pending/5
end

subject.new_worker_count(10).should == 2
end

it "should be able to take the Resque job's payload as arguments" do
subject.new_worker_count do |pending, queue|
if queue == "test_queue"
10
else
pending/5
end
end

job_payload = ["test_queue", "more", "payload"]
puts "job_payload: #{job_payload.inspect}"
subject.new_worker_count(10, *job_payload).should == 10
end

context "when the proc was not yet set" do
before do
subject.new_worker_count do |pending, queue|
end
it { subject.new_worker_count(0).should == 0 }
it { subject.new_worker_count(1).should == 1 }
end
end
end
end
20 changes: 16 additions & 4 deletions spec/resque_heroku_autoscaler_spec.rb
Expand Up @@ -80,16 +80,20 @@ class AnotherJob

describe ".set_workers" do
it "should use the Heroku client to set the workers" do
ENV['HEROKU_APP'] = 'some app name'
subject.config do |c|
c.heroku_app = 'some app name'
end
mock(TestJob).heroku_client { mock!.set_workers('some app name', 10) }
TestJob.set_workers(10)
end
end

describe ".heroku_client" do
before do
ENV['HEROKU_USER'] = 'john doe'
ENV['HEROKU_PASS'] = 'password'
subject.config do |c|
c.heroku_user = 'john doe'
c.heroku_pass = 'password'
end
end

it "should return a heroku client" do
Expand All @@ -116,4 +120,12 @@ class AnotherJob
TestJob.heroku_client.should == AnotherJob.heroku_client
end
end
end

describe ".config" do
it "yields the configuration" do
subject.config do |c|
c.should == Resque::Plugins::HerokuAutoscaler::Config
end
end
end
end

0 comments on commit 2b78510

Please sign in to comment.