Class: Chore::Strategy::SingleWorkerStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/chore/strategies/worker/single_worker_strategy.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manager, opts = {}) ⇒ SingleWorkerStrategy

Returns a new instance of SingleWorkerStrategy.



10
11
12
13
14
15
16
17
# 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

Instance Attribute Details

#workerObject (readonly)

Returns the value of attribute worker.



8
9
10
# File 'lib/chore/strategies/worker/single_worker_strategy.rb', line 8

def worker
  @worker
end

Instance Method Details

#assign(work) ⇒ Object

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.



35
36
37
38
39
40
41
42
43
44
45
# 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

#startObject

Starts the SingleWorkerStrategy. Currently a noop



20
# File 'lib/chore/strategies/worker/single_worker_strategy.rb', line 20

def start;end

#stop!Object

Stops the SingleWorkerStrategy if there is a worker to stop



23
24
25
26
27
28
29
30
# 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_klassObject



47
48
49
# File 'lib/chore/strategies/worker/single_worker_strategy.rb', line 47

def worker_klass
  Worker
end