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
10
# File 'lib/quebert/worker.rb', line 7

def initialize
  @queues = []
  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

#queuesObject

Returns the value of attribute queues.



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

def queues
  @queues
end

Instance Method Details

#safe_stopObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/quebert/worker.rb', line 42

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/quebert/worker.rb', line 13

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

  logger.info "Worker started with #{backend.class.name} backend\n"

  backend.queues = queues if backend.respond_to?(:queues=)

  while @controller = backend.reserve do
    begin
      @controller.perform
    rescue => error
      if exception_handler
        exception_handler.call(
          error,
          :controller => @controller,
          :pid => $$,
          :worker => self
        )
      else
        raise
      end
    end
    @controller = nil

    stop if @terminate_sent
  end
end

#stopObject



53
54
55
56
# File 'lib/quebert/worker.rb', line 53

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