Class: Sisyphus::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/sisyphus/worker.rb

Constant Summary collapse

UNCAUGHT_ERROR =
'.'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job, output, execution_strategy) ⇒ Worker

Returns a new instance of Worker.



7
8
9
10
11
# File 'lib/sisyphus/worker.rb', line 7

def initialize(job, output, execution_strategy)
  @job = job
  @output = output
  @execution_strategy = execution_strategy
end

Instance Attribute Details

#execution_strategyObject (readonly)

Returns the value of attribute execution_strategy.



5
6
7
# File 'lib/sisyphus/worker.rb', line 5

def execution_strategy
  @execution_strategy
end

#jobObject (readonly)

Returns the value of attribute job.



5
6
7
# File 'lib/sisyphus/worker.rb', line 5

def job
  @job
end

#outputObject (readonly)

Returns the value of attribute output.



5
6
7
# File 'lib/sisyphus/worker.rb', line 5

def output
  @output
end

Instance Method Details

#error_handlerObject



32
33
34
35
36
37
38
39
40
# File 'lib/sisyphus/worker.rb', line 32

def error_handler
  -> {
    begin
      output.write UNCAUGHT_ERROR unless stopped?
    rescue Errno::EAGAIN, Errno::EINTR
      # Ignore
    end
  }
end

#perform_jobObject



28
29
30
# File 'lib/sisyphus/worker.rb', line 28

def perform_job
  execution_strategy.execute job, error_handler
end

#setupObject



13
14
15
# File 'lib/sisyphus/worker.rb', line 13

def setup
  job.setup if job.respond_to? :setup
end

#startObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/sisyphus/worker.rb', line 17

def start
  trap_signals

  loop do
    break if stopped?
    perform_job
  end

  exit! 0
end