class Chore::Strategy::SingleWorkerStrategy

Worker strategy for performing batches of work in a linear fashion. Ideally used for running Chore jobs locally in a development environment where performance or throughput may not matter.

Attributes

worker[R]

Public Class Methods

new(manager, opts={}) click to toggle source
# File lib/chore/strategies/worker/single_worker_strategy.rb, line 10
def initialize(manager, opts={})
  @options = opts
  @manager = manager
  @stopped = false
  @worker = nil
  @queue = Queue.new
  @queue << :worker
end

Public Instance Methods

assign(work) click to toggle source

Assigns work if there isn't already a worker in progress. In this, the single worker strategy, this should never be called if the worker is in progress.

# File lib/chore/strategies/worker/single_worker_strategy.rb, line 35
def assign(work)
  return unless acquire_worker

  begin
    @worker = worker_klass.new(work, @options)
    @worker.start
    true
  ensure
    release_worker
  end
end
start() click to toggle source

Starts the SingleWorkerStrategy. Currently a noop

# File lib/chore/strategies/worker/single_worker_strategy.rb, line 20
def start;end
stop!() click to toggle source

Stops the SingleWorkerStrategy if there is a worker to stop

# File lib/chore/strategies/worker/single_worker_strategy.rb, line 23
def stop!
  return if @stopped

  @stopped = true
  Chore.logger.info { "Manager #{Process.pid} stopping" }

  worker.stop! if worker
end
worker_klass() click to toggle source
# File lib/chore/strategies/worker/single_worker_strategy.rb, line 47
def worker_klass
  Worker
end

Private Instance Methods

acquire_worker() click to toggle source

Attempts to essentially acquire a lock on a worker. If no workers are available, then this will block until one is.

# File lib/chore/strategies/worker/single_worker_strategy.rb, line 55
def acquire_worker
  result = @queue.pop

  if @stopped
    # Strategy has stopped since the worker was acquired
    release_worker
    nil
  else
    result
  end
end
release_worker() click to toggle source

Releases the lock on a worker so that another thread can pick it up.

# File lib/chore/strategies/worker/single_worker_strategy.rb, line 68
def release_worker
  @queue << :worker
end