Sha256: 0efd2e720323c600b153076201256e6e2bd3f6f4a2cb4866a3ddc01910f0761f

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

require 'httparty'

module DeepThought
  module Scaler
    def self.scale
      if (ENV['RACK_ENV'] != 'development' && ENV['RACK_ENV'] != 'test') && (ENV['HEROKU_APP'] && ENV['HEROKU_APP'] != '') && (ENV['HEROKU_API_KEY'] && ENV['HEROKU_API_KEY'] != '')
        if Delayed::Job.count > 0
          scale_up
        else
          scale_down
        end
      end
    end

    private

    def self.scale_up
      options = {:body => {:type => 'worker', :qty => '1'}, :basic_auth => {:username => '', :password => ENV['HEROKU_API_KEY']}}
      HTTParty.post("https://api.heroku.com/apps/#{ENV['HEROKU_APP']}/ps/scale", options)
    end

    def self.scale_down
      options = {:body => {:type => 'worker', :qty => '0'}, :basic_auth => {:username => '', :password => ENV['HEROKU_API_KEY']}}
      HTTParty.post("https://api.heroku.com/apps/#{ENV['HEROKU_APP']}/ps/scale", options)
    end

    Delayed::Backend::ActiveRecord::Job.class_eval do
      after_destroy :after_destroy

      def after_destroy
        DeepThought::Scaler.scale
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
deep_thought-0.2.1 lib/deep_thought/scaler.rb
deep_thought-0.1.1 lib/deep_thought/scaler.rb
deep_thought-0.1.0 lib/deep_thought/scaler.rb