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



38
39
40
41
42
43
44
45
46
47
# File 'lib/quebert/worker.rb', line 38

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
28
29
30
31
32
33
34
35
36
# 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 => error
      if exception_handler
        exception_handler.call(
          error,
          :controller => @controller,
          :pid => $$,
          :worker => self
        )
      else
        raise error
      end
    end
    @controller = nil

    stop if @terminate_sent
  end
end

#stopObject



49
50
51
52
# File 'lib/quebert/worker.rb', line 49

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