Permalink
Browse files

Added Sequel support

  • Loading branch information...
1 parent d651c20 commit 1b2051379881203c46a3cf32ceb87cc0e4af0a9e @davidakachaos davidakachaos committed Jul 17, 2013
Showing with 116 additions and 7 deletions.
  1. +1 −0 lib/workless/initialize.rb
  2. +20 −7 lib/workless/scaler.rb
  3. +11 −0 spec/spec_helper.rb
  4. +84 −0 spec/workless/sequel_scaling_spec.rb
@@ -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)
View
@@ -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
View
@@ -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
@@ -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'
@@ -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.