Sha256: 310a1bc0b09b01ccf0eb50306e2331324903edcf4569654041cde72d6af7405d

Contents?: true

Size: 1.78 KB

Versions: 3

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module Delayed
  module Workless
    module Scaler
      autoload :Heroku,      'workless/scalers/heroku'
      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(on: :update, if: proc { |r| !r.failed_at.nil? }) do 
              self.class.scaler.down
            end
            after_commit(on: :destroy, if: proc { |r| r.destroyed? || !r.failed_at.nil? }) do
              self.class.scaler.down
            end
            after_commit(on: :create) do 
              self.class.scaler.up
            end
          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?('WORKLESS_API_KEY')
                        Scaler::Heroku
                      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

3 entries across 3 versions & 2 rubygems

Version Path
workless_revived-2.2.0 lib/workless/scaler.rb
workless-2.2.0 lib/workless/scaler.rb
workless_revived-2.1.0.2 lib/workless/scaler.rb