module Qless::WorkerHelpers

Public Instance Methods

drain_worker_queues(worker) click to toggle source

Runs the worker until it has no more jobs to process, effectively drainig its queues.

# File lib/qless/test_helpers/worker_helpers.rb, line 38
def drain_worker_queues(worker)
  worker.extend Module.new {
    # For the child: stop as soon as it can't pop more jobs.
    def no_job_available
      shutdown
    end

    # For the parent: when the child stops,
    # don't try to restart it; shutdown instead.
    def spawn_replacement_child(*)
      shutdown
    end
  }

  worker.run
end
run_jobs(worker, count) { || ... } click to toggle source

Run only the given number of jobs, then stop

# File lib/qless/test_helpers/worker_helpers.rb, line 19
def run_jobs(worker, count)
  worker.extend Module.new {
    define_method(:jobs) do
      base_enum = super()
      Enumerator.new do |enum|
        count.times { enum << base_enum.next }
      end
    end
  }

  thread = Thread.start { yield } if block_given?
  thread.abort_on_exception if thread
  worker.run
ensure
  thread.join(0.1) if thread
end
run_worker_concurrently_with(worker, &block) click to toggle source

Yield with a worker running, and then clean the worker up afterwards

# File lib/qless/test_helpers/worker_helpers.rb, line 4
def run_worker_concurrently_with(worker, &block)
  thread = Thread.start { stop_worker_after(worker, &block) }
  thread.abort_on_exception = true
  worker.run
ensure
  thread.join(0.1)
end
stop_worker_after(worker) { || ... } click to toggle source
# File lib/qless/test_helpers/worker_helpers.rb, line 12
def stop_worker_after(worker, &block)
  yield
ensure
  worker.stop!
end