Class: Quebert::Worker

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/quebert/worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Worker

Returns a new instance of Worker.

Yields:

  • (_self)

Yield Parameters:



7
8
9
# File 'lib/quebert/worker.rb', line 7

def initialize
  yield self if block_given?
end

Instance Attribute Details

#backend=(value) ⇒ Object

Sets the attribute backend

Parameters:

  • value

    the value to set the attribute backend to.



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

def backend=(value)
  @backend = value
end

#exception_handler=(value) ⇒ Object

Sets the attribute exception_handler

Parameters:

  • value

    the value to set the attribute exception_handler to.



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

def exception_handler=(value)
  @exception_handler = value
end

Instance Method Details

#safe_stopObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/quebert/worker.rb', line 29

def safe_stop
  if @terminate_sent
    logger.info "Ok! I get the point. Shutting down immediately."
    stop
  else
    logger.info "Finishing current job then shutting down."
    @terminate_sent = true
    stop unless @controller
  end
end

#startObject

Start the worker backend and intercept exceptions if a handler is provided



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/quebert/worker.rb', line 12

def start
  Signal.trap('TERM') { safe_stop }
  Signal.trap('INT') { safe_stop }

  logger.info "Worker started with #{backend.class.name} backend\n"
  while @controller = backend.reserve do      
    begin
      @controller.perform
    rescue Exception => e
      exception_handler ? exception_handler.call(e) : raise(e)
    end        
    @controller = nil

    stop if @terminate_sent
  end
end

#stopObject



40
41
42
43
# File 'lib/quebert/worker.rb', line 40

def stop
  logger.info "Worker stopping\n"
  exit 0
end