Skip to content

Commit

Permalink
Added Sequel support
Browse files Browse the repository at this point in the history
  • Loading branch information
davidakachaos committed Jul 17, 2013
1 parent d651c20 commit 1b20513
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/workless/initialize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
Delayed::Backend::ActiveRecord::Job.send(:include, Delayed::Workless::Scaler) if defined?(Delayed::Backend::ActiveRecord::Job)
Delayed::Backend::Mongoid::Job.send(:include, Delayed::Workless::Scaler) if defined?(Delayed::Backend::Mongoid::Job)
Delayed::Backend::MongoMapper::Job.send(:include, Delayed::Workless::Scaler) if defined?(Delayed::Backend::MongoMapper::Job)
Delayed::Backend::Sequel::Job.send(:include, Delayed::Workless::Scaler) if defined?(Delayed::Backend::Sequel::Job)
27 changes: 20 additions & 7 deletions lib/workless/scaler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,31 @@ module Scaler

def self.included(base)
base.send :extend, ClassMethods
unless base.to_s =~ /ActiveRecord/
base.class_eval do
after_destroy "self.class.scaler.down"
after_create "self.class.scaler.up"
after_update "self.class.scaler.down", :unless => Proc.new {|r| r.failed_at.nil? }
end
else
if base.to_s =~ /ActiveRecord/
base.class_eval do
after_commit "self.class.scaler.down", :on => :destroy
after_commit "self.class.scaler.up", :on => :create
after_commit "self.class.scaler.down", :on => :update, :unless => Proc.new {|r| r.failed_at.nil? }
end
elsif base.to_s =~ /Sequel/
base.send(:define_method, 'after_destroy') do
super
self.class.scaler.down
end
base.send(:define_method, 'after_create') do
super
self.class.scaler.up
end
base.send(:define_method, 'after_update') do
super
self.class.scaler.down
end
else
base.class_eval do
after_destroy "self.class.scaler.down"
after_create "self.class.scaler.up"
after_update "self.class.scaler.down", :unless => Proc.new {|r| r.failed_at.nil? }
end
end

end
Expand Down
11 changes: 11 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ def self.after_update(method, *args)
end
end

module Delayed
module Sequel
module Job
class Delayed::Sequel::Job::Mock

end
end
end
end

class NumWorkers
def initialize(count)
@count = count
Expand All @@ -59,5 +69,6 @@ def count
Delayed::ActiveRecord::Job::Mock.send(:include, Delayed::Workless::Scaler)
Delayed::Mongoid::Job::Mock.send(:include, Delayed::Workless::Scaler)
Delayed::MongoMapper::Job::Mock.send(:include, Delayed::Workless::Scaler)
Delayed::Sequel::Job::Mock.send(:include, Delayed::Workless::Scaler)

ENV['APP_NAME'] = 'TestHerokuApp'
84 changes: 84 additions & 0 deletions spec/workless/sequel_scaling_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
require 'spec_helper'

describe Delayed::Sequel::Job do
context 'with Sequel scaler' do
before(:each) do
ENV['WORKLESS_WORKERS_RATIO'] = '25'
Delayed::Sequel::Job::Mock.scaler = :heroku_cedar
end
context 'with no workers' do
before(:each) do
Delayed::Sequel::Job::Mock.scaler.should_receive(:workers).and_return(0)
end
it "should scale up" do
if_there_are_jobs 1
should_scale_workers_to 1

Delayed::Sequel::Job::Mock.scaler.up
end
it "should scale up" do
if_there_are_jobs 5
should_scale_workers_to 1

Delayed::Sequel::Job::Mock.scaler.up
end
it 'should not scale' do
if_there_are_jobs 0
should_not_scale_workers
Delayed::Workless::Scaler::HerokuCedar.down
end
end
context 'with 1 worker' do
before(:each) do
Delayed::Sequel::Job::Mock.scaler.stub(:workers).and_return(1)
end
it "should scale down to none" do
if_there_are_jobs 1
should_not_scale_workers

Delayed::Sequel::Job::Mock.scaler.up
end
it "should scale down to 1" do
if_there_are_jobs 1
should_not_scale_workers

Delayed::Sequel::Job::Mock.scaler.down
end
end

context 'with 5 workers' do
before(:each) do
Delayed::Sequel::Job::Mock.scaler.stub(:workers).and_return(5)
end
it "should scale down to none" do
if_there_are_jobs 0
should_scale_workers_to 0

Delayed::Sequel::Job::Mock.scaler.down
end
pending "This will be a new feature" do
it "should scale down to 1" do
if_there_are_jobs 1
should_scale_workers_to 1

Delayed::Sequel::Job::Mock.scaler.down
end
end
end
end

private

def if_there_are_jobs(num)
Delayed::Sequel::Job::Mock.scaler.stub(:jobs).and_return(NumWorkers.new(num))
end

def should_scale_workers_to(num)
Delayed::Sequel::Job::Mock.scaler.client.should_receive(:post_ps_scale).once.with(ENV['APP_NAME'], 'worker', num)
end

def should_not_scale_workers
Delayed::Sequel::Job::Mock.scaler.client.should_not_receive(:post_ps_scale)
end

end

0 comments on commit 1b20513

Please sign in to comment.