Sha256: 07d2f238c1b9ea30301170ef8d9f9c462a9e8f7f0af3298c8b4b68792ea8b30d

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

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'

      def self.included(base)
        base.send :extend, ClassMethods
        if base.to_s =~ /ActiveRecord/
          base.class_eval do
            after_commit 'self.class.scaler.down'.to_sym, on: :update, if: proc { |r| !r.failed_at.nil? }
            after_commit 'self.class.scaler.down'.to_sym, on: :destroy, if: proc { |r| r.destroyed? || !r.failed_at.nil? }
            after_commit 'self.class.scaler.up'.to_sym, on: :create
          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 { |r| r.failed_at.nil? }
          end
        end
      end

      module ClassMethods
        def scaler
          @scaler ||= if ENV.include?('HEROKU_API_KEY')
                        Scaler::HerokuCedar
                      else
                        Scaler::Local
          end
        end

        def scaler=(scaler)
          @scaler = "Delayed::Workless::Scaler::#{scaler.to_s.camelize}".constantize
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
workless_revived-1.2.5 lib/workless/scaler.rb