Sha256: 5c7ee455828f1d37bbb02b2c28b0b60c1c33e044f2998f39414673d91534013d

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

require 'sidekiq'
require 'sidekiq-heroku-scaler/sidekiq_config'

module SidekiqHerokuScaler
  class Worker
    attr_reader :worker_name

    def initialize(worker_name, formation)
      @worker_name = worker_name
      @formation = formation
    end

    def concurrency
      process['concurrency'] || 0
    end

    def formation_id
      formation.id
    end

    def quantity
      formation.quantity
    end

    def jobs_running?
      Sidekiq::Workers.new.any? { |_process_id, _thread_id, work| queues.include?(work['queue']) }
    end

    def latency
      queues.sum { |queue| Sidekiq::Queue.new(queue).latency }
    end

    def queues_size
      queues.sum { |queue| Sidekiq::Queue.new(queue).size }
    end

    def processes
      @processes ||= Sidekiq::ProcessSet.new.select { |process| process.identity.match?(/\A#{worker_name}\./) }
    end

    private

    attr_reader :formation

    def build_process
      command = formation.command.gsub(/.*sidekiq(\s|\z)/, '').split
      sideki_config = SidekiqHerokuScaler::SidekiqConfig.new(command)
      Sidekiq::Process.new(sideki_config.config)
    end

    def queues
      process['queues'] || []
    end

    def process
      @process ||= processes.first || build_process
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sidekiq-heroku-scaler-0.4.1 lib/sidekiq-heroku-scaler/worker.rb
sidekiq-heroku-scaler-0.4.0 lib/sidekiq-heroku-scaler/worker.rb