Class: Postman

Inherits:
Object
  • Object
show all
Extended by:
StateMachine::Helper
Defined in:
lib/postman.rb,
lib/postman/channel.rb,
lib/postman/message.rb,
lib/postman/state_machine.rb

Overview

A postman listens to a rabbitMQ message queues and manages state and reconnection.

Defined Under Namespace

Modules: StateMachine Classes: Channel, ConnectionMissing, Message

Constant Summary collapse

MAX_RECONNECT_DELAY =

Maximum wait time between database retries: 5 minutes

60 * 5
InvalidPayload =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StateMachine::Helper

states

Constructor Details

#initialize(client:, main_exchange:) ⇒ Postman

Returns a new instance of Postman


15
16
17
18
19
20
# File 'lib/postman.rb', line 15

def initialize(client:, main_exchange:)
  @client = client
  @consumer_tag = "qbm_#{Rails.env}_#{Process.pid}"
  @state = :initialized
  @main_exchange = main_exchange
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client


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

def client
  @client
end

#main_exchangeObject (readonly)

Returns the value of attribute main_exchange


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

def main_exchange
  @main_exchange
end

#stateObject (readonly)

Returns the value of attribute state


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

def state
  @state
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)

29
30
31
# File 'lib/postman.rb', line 29

def alive?
  !stopped?
end

#loggerObject


25
26
27
# File 'lib/postman.rb', line 25

def logger
  Rails.logger
end

#pause!Object


55
56
57
58
59
60
61
62
# File 'lib/postman.rb', line 55

def pause!
  return unless running?

  unsubscribe!
  @recovery_attempts = 0
  @recover_at = Time.current
  paused!
end

#run!Object


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/postman.rb', line 33

def run!
  starting!
  trap_signals
  @client.start # Start the client
  main_exchange.activate! # Set up the queues
  running!            # Transition to running state
  subscribe!          # Subscribe to the queue
  # Monitor our state to control stopping and re-connection
  # This loop blocks until the state is :stopped
  control_loop while alive?
  # And we leave the application
  info "Stopped #{@consumer_tag}"
  info 'Goodbye!'
ensure
  @client.close
end

#stop!Object


50
51
52
53
# File 'lib/postman.rb', line 50

def stop!
  stopping!
  STDOUT.puts "Stopping #{@consumer_tag}"
end