Sha256: eb729dec00736c8e8c3d3bd531dc66e1d94b989274f212d00ea223f7158dc4cf

Contents?: true

Size: 1.21 KB

Versions: 8

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

require 'sidekiq/launcher'
require 'timeout'

module Sidekiq
  class Simulator
    extend Forwardable
    def_delegator SidekiqUniqueJobs, :logger

    attr_reader :queues, :launcher

    def self.process_queue(queue)
      new(queue).process_queue { yield }
    end

    def initialize(queue)
      @queues = [queue].flatten.uniq
      @launcher = Sidekiq::Launcher.new(sidekiq_options(queues))
    end

    def process_queue
      run_launcher { yield }
    ensure
      terminate_launcher
    end

    private

    def run_launcher
      using_timeout(15) do
        launcher.run
        sleep 0.001 until alive?
      end
    rescue Timeout::Error => e
      logger.warn { "Timeout while running #{__method__}" }
      logger.warn { e }
    ensure
      yield
    end

    def terminate_launcher
      launcher.stop
    end

    def alive?
      launcher.manager.workers.any?
    end

    def stopped?
      !alive?
    end

    def using_timeout(value)
      Timeout.timeout(value) do
        yield
      end
    end

    def sidekiq_options(queues = [])
      { queues: queues,
        concurrency: 3,
        timeout: 3,
        verbose: false,
        logfile: './tmp/sidekiq.log' }
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-5.0.11 lib/sidekiq/simulator.rb
sidekiq-unique-jobs-5.0.10 lib/sidekiq/simulator.rb
sidekiq-unique-jobs-5.0.9 lib/sidekiq/simulator.rb
sidekiq-unique-jobs-5.0.8 lib/sidekiq/simulator.rb
sidekiq-unique-jobs-5.0.7 lib/sidekiq/simulator.rb
sidekiq-unique-jobs-5.0.6 lib/sidekiq/simulator.rb
sidekiq-unique-jobs-5.0.5 lib/sidekiq/simulator.rb
sidekiq-unique-jobs-5.0.4 lib/sidekiq/simulator.rb