require 'sidekiq'
require 'roo_on_rails/sidekiq/settings'
module RooOnRails
  module Railties
    class Sidekiq < Rails::Railtie
      initializer 'roo_on_rails.sidekiq' do |app|
        require 'hirefire-resource'
        $stderr.puts 'initializer roo_on_rails.sidekiq'
        break unless ENV.fetch('SIDEKIQ_ENABLED', 'true').to_s =~ /\A(YES|TRUE|ON|1)\Z/i
        config_sidekiq
        config_hirefire(app)
      end

      def config_hirefire(app)
        unless ENV['HIREFIRE_TOKEN']
          warn 'No HIREFIRE_TOKEN token set, auto scaling not enabled'
          return
        end
        add_middleware(app)
      end

      def config_sidekiq
        ::Sidekiq.configure_server do |x|
          x.options[:concurrency] = RooOnRails::Sidekiq::Settings.concurrency.to_i
          x.options[:queues] = RooOnRails::Sidekiq::Settings.queues
        end
      end

      def add_middleware(app)
        $stderr.puts 'HIREFIRE_TOKEN set'
        app.middleware.use HireFire::Middleware
        HireFire::Resource.configure do |config|
          config.dyno(:worker) do
            RooOnRails::SidekiqSla.queue
          end
        end
      end
    end
  end
end

require 'sidekiq'
require 'roo_on_rails/sidekiq/settings'
require 'roo_on_rails/sidekiq/sla_metric'

module RooOnRails
  module Railties
    class Sidekiq < Rails::Railtie
      initializer 'roo_on_rails.sidekiq' do |app|
        require 'hirefire-resource'
        $stderr.puts 'initializer roo_on_rails.sidekiq'
        break unless ENV.fetch('SIDEKIQ_ENABLED', 'true').to_s =~ /\A(YES|TRUE|ON|1)\Z/i
        config_sidekiq
        config_hirefire(app)
      end

      def config_hirefire(app)
        unless ENV['HIREFIRE_TOKEN']
          warn 'No HIREFIRE_TOKEN token set, auto scaling not enabled'
          return
        end
        add_middleware(app)
      end

      def config_sidekiq
        ::Sidekiq.configure_server do |x|
          x.options[:concurrency] = RooOnRails::Sidekiq::Settings.concurrency.to_i
          x.options[:queues] = RooOnRails::Sidekiq::Settings.queues
        end
      end

      def add_middleware(app)
        $stderr.puts 'HIREFIRE_TOKEN set'
        app.middleware.use HireFire::Middleware
        HireFire::Resource.configure do |config|
          config.dyno(:worker) do
            RooOnRails::Sidekiq::SlaMetric.queue
          end
        end
      end
    end
  end
end

Use correct SLA class name
HOTFIX: Use correct SLA class name

This could really do with some tests, but I’m not quite sure how to
test it and this is causing errors in production so I’d prefer to patch
it quickly and then work out how to test it later.

@danielcooper maybe you have some thoughts on how to test?
