Class: Msgr::Client

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Logging
Defined in:
lib/msgr/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log

Constructor Details

#initialize(config) ⇒ Client



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/msgr/client.rb', line 11

def initialize(config)
  @uri = URI.parse config[:uri] ? config.delete(:uri) : 'amqp://localhost/'
  @uri.protocol = 'amqps'                   if config[:secure]
  @uri.user     = config.delete :user       if config[:user]
  @uri.password = config.delete :password   if config[:password]
  @uri.host     = config.delete :host       if config[:host]
  @uri.port     = config.delete(:port).to_i if config[:port]
  @uri.path     = "/#{config.delete :vhost}".gsub /\/+/, '/' if config[:vhost]

  @config  = config
  @bunny   = Bunny.new @uri.to_s
  @pool    = Pool.new Dispatcher, autostart: false

  @uri.password = nil
end

Instance Attribute Details

#poolObject (readonly)

Returns the value of attribute pool.



9
10
11
# File 'lib/msgr/client.rb', line 9

def pool
  @pool
end

#uriObject (readonly)

Returns the value of attribute uri.



9
10
11
# File 'lib/msgr/client.rb', line 9

def uri
  @uri
end

Instance Method Details

#log_nameObject



28
# File 'lib/msgr/client.rb', line 28

def log_name; self.class.name end

#publish(routing_key, payload) ⇒ Object



78
79
80
# File 'lib/msgr/client.rb', line 78

def publish(routing_key, payload)
  @connection.publish payload, routing_key: routing_key
end

#reloadObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/msgr/client.rb', line 34

def reload
  raise StandardError.new 'Client not running.' unless running?
  log(:info) { 'Reload client.' }

  @connection.release
  @connection.terminate

  log(:debug) { 'Create new connection.' }

  @connection = Connection.new @bunny, routes, pool

  log(:info) { 'Client reloaded.' }
end

#routesObject



30
31
32
# File 'lib/msgr/client.rb', line 30

def routes
  @routes ||= Routes.new
end

#running?Boolean



27
# File 'lib/msgr/client.rb', line 27

def running?; @running end

#startObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/msgr/client.rb', line 48

def start
  log(:info) { "Start client to #{uri}" }

  @bunny.start
  @pool.start

  @running    = true
  @connection = Connection.new @bunny, routes, pool

  log(:info) { "Client started. pool: #{pool.size}" }
end

#stopObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/msgr/client.rb', line 60

def stop
  return unless running?

  @running = false
  log(:info) { 'Graceful shutdown client...' }

  @connection.release
  @pool.stop

  log(:debug) { 'Terminating...' }

  @connection.terminate
  @pool.terminate
  @bunny.stop

  log(:info) { 'Terminated.' }
end