Sha256: 217f710cec24467e784fcada51fc8c0b9e46528350fde5c6cf28a562fb39fbb3

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

require 'sidekiq'
require 'roo_on_rails/config'
require 'roo_on_rails/statsd'
require 'roo_on_rails/sidekiq/settings'
require 'roo_on_rails/sidekiq/sla_metric'

module RooOnRails
  module Railties
    class SidekiqIntegration < Rails::Railtie
      initializer 'roo_on_rails.sidekiq' do |app|
        unless RooOnRails::Config.sidekiq_enabled?
          Rails.logger.debug '[roo_on_rails.sidekiq] skipping'
          next
        end

        Rails.logger.debug '[roo_on_rails.sidekiq] loading'
        require 'hirefire-resource'

        config_sidekiq
        config_sidekiq_metrics
        config_hirefire(app)
      end

      def config_hirefire(app)
        unless ENV['HIREFIRE_TOKEN']
          Rails.logger.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 config_sidekiq_metrics
        # https://github.com/mperham/sidekiq/wiki/Pro-Metrics
        require 'sidekiq-pro'
        ::Sidekiq::Pro.dogstatsd = -> { RooOnRails.statsd }

        ::Sidekiq.configure_server do |config|
          config.server_middleware do |chain|
            require 'sidekiq/middleware/server/statsd'
            chain.add ::Sidekiq::Middleware::Server::Statsd
          end
        end
      rescue LoadError
        Rails.logger.warn 'Sidekiq metrics unavailable without Sidekiq Pro'
      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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
roo_on_rails-2.1.0 lib/roo_on_rails/railties/sidekiq_integration.rb