Skip to content

Commit

Permalink
New(ish) Platform API implemented (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidakachaos committed Mar 1, 2017
1 parent a4f4d76 commit a3e0ff0
Show file tree
Hide file tree
Showing 17 changed files with 103 additions and 175 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.4
2.2.5
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
source 'https://rubygems.org'

gem 'coveralls', require: false
gem "codeclimate-test-reporter", require: false
gem "simplecov"
gem "codeclimate-test-reporter", "~> 1.0.0", require: false

gemspec
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ By adding the gem to your project and configuring our Heroku app with some confi

## Updates

* Version 2.0.0 Updated to use latest version of the Heroku API. Drops support for old style Heroku
* Version 1.3.0 DROPS SUPPORT FOR OLDER RUBY AND RAILS VERSIONS!
* Version 1.2.5 Added middleware to check on delayed jobs, fixed Rails 5 support
* Version 1.2.4 drops support for older versions!
Expand Down Expand Up @@ -80,7 +81,7 @@ There are three other scalers included. Note that if you are running on the Aspe

<pre>
Delayed::Job.scaler = :heroku
Delayed::Job.scaler = :heroku_cedar
Delayed::Job.scaler = :heroku
Delayed::Job.scaler = :local
</pre>

Expand Down
3 changes: 1 addition & 2 deletions lib/workless/scaler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module Delayed
module Workless
module Scaler
autoload :Heroku, 'workless/scalers/heroku'
autoload :HerokuCedar, 'workless/scalers/heroku_cedar'
autoload :Local, 'workless/scalers/local'
autoload :Null, 'workless/scalers/null'

Expand Down Expand Up @@ -45,7 +44,7 @@ def self.included(base)
module ClassMethods
def scaler
@scaler ||= if ENV.include?('HEROKU_API_KEY')
Scaler::HerokuCedar
Scaler::Heroku
else
Scaler::Local
end
Expand Down
2 changes: 1 addition & 1 deletion lib/workless/scalers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def self.jobs

module HerokuClient
def client
@client ||= ::Heroku::API.new(api_key: ENV['HEROKU_API_KEY'])
@client ||= ::PlatformAPI.connect(ENV['HEROKU_API_KEY'])
end
end
end
Expand Down
38 changes: 34 additions & 4 deletions lib/workless/scalers/heroku.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'heroku-api'
require 'platform-api'

module Delayed
module Workless
Expand All @@ -7,15 +7,45 @@ class Heroku < Base
extend Delayed::Workless::Scaler::HerokuClient

def self.up
client.put_workers(ENV['APP_NAME'], 1) if workers == 0
return unless workers_needed > min_workers && workers < workers_needed
updates = { "quantity": workers_needed }
client.formation.update(ENV['APP_NAME'], 'worker', updates)
end

def self.down
client.put_workers(ENV['APP_NAME'], 0) unless jobs.count > 0 || workers == 0
return if workers == workers_needed
updates = { "quantity": workers_needed }
client.formation.update(ENV['APP_NAME'], 'worker', updates)
end

def self.workers
client.get_ps(ENV['APP_NAME']).body.count { |p| p['process'] =~ /worker\.\d?/ }
client.formation.info(ENV['APP_NAME'], 'worker')['quantity']
end

# Returns the number of workers needed based on the current number of pending jobs and the settings defined by:
#
# ENV['WORKLESS_WORKERS_RATIO']
# ENV['WORKLESS_MAX_WORKERS']
# ENV['WORKLESS_MIN_WORKERS']
#
def self.workers_needed
[[(jobs.count.to_f / workers_ratio).ceil, max_workers].min, min_workers].max
end

def self.workers_ratio
if ENV['WORKLESS_WORKERS_RATIO'].present? && (ENV['WORKLESS_WORKERS_RATIO'].to_i != 0)
ENV['WORKLESS_WORKERS_RATIO'].to_i
else
100
end
end

def self.max_workers
ENV['WORKLESS_MAX_WORKERS'].present? ? ENV['WORKLESS_MAX_WORKERS'].to_i : 1
end

def self.min_workers
ENV['WORKLESS_MIN_WORKERS'].present? ? ENV['WORKLESS_MIN_WORKERS'].to_i : 0
end
end
end
Expand Down
49 changes: 0 additions & 49 deletions lib/workless/scalers/heroku_cedar.rb

This file was deleted.

1 change: 0 additions & 1 deletion lib/workless_revived.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'heroku-api'
require File.dirname(__FILE__) + '/workless/scalers/base'
require File.dirname(__FILE__) + '/workless/scaler'
require File.dirname(__FILE__) + '/workless/middleware/workless_checker' if defined?(Rails::Railtie)
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require 'rubygems'
require 'bundler/setup'

require "codeclimate-test-reporter"
CodeClimate::TestReporter.start
require 'simplecov'
SimpleCov.start

require 'coveralls'
Coveralls.wear!
Expand Down
11 changes: 6 additions & 5 deletions spec/workless/mongoid_scaling_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
ENV['WORKLESS_WORKERS_RATIO'] = '25'
ENV['WORKLESS_MAX_WORKERS'] = '10'
ENV['WORKLESS_MIN_WORKERS'] = '0'
Delayed::Mongoid::Job::Mock.scaler = :heroku_cedar
Delayed::Mongoid::Job::Mock.scaler = :heroku
end
context 'with no workers' do
before(:each) do
Expand All @@ -27,7 +27,7 @@
it 'should not scale' do
if_there_are_jobs 0
should_not_scale_workers
Delayed::Workless::Scaler::HerokuCedar.down
Delayed::Workless::Scaler::Heroku.down
end
end
context 'with 1 worker' do
Expand Down Expand Up @@ -78,14 +78,15 @@
private

def if_there_are_jobs(num)
Delayed::Mongoid::Job::Mock.scaler.stub(:jobs).and_return(NumWorkers.new(num))
Delayed::Workless::Scaler::Heroku.should_receive(:jobs).at_least(1).times.and_return(NumWorkers.new(num))
end

def should_scale_workers_to(num)
Delayed::Mongoid::Job::Mock.scaler.client.should_receive(:post_ps_scale).once.with(ENV['APP_NAME'], 'worker', num)
updates = { "quantity": num }
Delayed::Workless::Scaler::Heroku.client.formation.should_receive(:update).once.with(ENV['APP_NAME'], 'worker', updates)
end

def should_not_scale_workers
Delayed::Mongoid::Job::Mock.scaler.client.should_not_receive(:post_ps_scale)
Delayed::Workless::Scaler::Heroku.client.formation.should_not_receive(:update)
end
end
12 changes: 6 additions & 6 deletions spec/workless/scaler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
context 'setting a scaler' do
context 'with a known scaler' do
before do
Delayed::ActiveRecord::Job::Mock.scaler = :heroku_cedar
Delayed::ActiveRecord::Job::Mock.scaler = :heroku
end

it 'should be properly assigned' do
Delayed::ActiveRecord::Job::Mock.scaler.should == Delayed::Workless::Scaler::HerokuCedar
Delayed::ActiveRecord::Job::Mock.scaler.should == Delayed::Workless::Scaler::Heroku
end
end

Expand Down Expand Up @@ -59,11 +59,11 @@ def self.down
context 'setting a scaler' do
context 'with a known scaler' do
before do
Delayed::Mongoid::Job::Mock.scaler = :heroku_cedar
Delayed::Mongoid::Job::Mock.scaler = :heroku
end

it 'should be properly assigned' do
Delayed::Mongoid::Job::Mock.scaler.should == Delayed::Workless::Scaler::HerokuCedar
Delayed::Mongoid::Job::Mock.scaler.should == Delayed::Workless::Scaler::Heroku
end
end

Expand Down Expand Up @@ -102,11 +102,11 @@ def self.down
context 'setting a scaler' do
context 'with a known scaler' do
before do
Delayed::MongoMapper::Job::Mock.scaler = :heroku_cedar
Delayed::MongoMapper::Job::Mock.scaler = :heroku
end

it 'should be properly assigned' do
Delayed::MongoMapper::Job::Mock.scaler.should == Delayed::Workless::Scaler::HerokuCedar
Delayed::MongoMapper::Job::Mock.scaler.should == Delayed::Workless::Scaler::Heroku
end
end

Expand Down
63 changes: 0 additions & 63 deletions spec/workless/scalers/heroku_cedar_spec.rb

This file was deleted.

Loading

0 comments on commit a3e0ff0

Please sign in to comment.