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

Returns a new instance of 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/'
  config[:pass] ||= @uri.password

  @uri.user   = config[:user]  ||= @uri.user || 'guest'
  @uri.scheme = (config[:ssl]  ||= @uri.scheme.to_s.downcase == 'amqps') ? 'amqps' : 'amqp'
  @uri.host   = config[:host]  ||= @uri.host || '127.0.0.1'
  @uri.port   = config[:port]  ||= @uri.port
  @uri.path   = config[:vhost] ||= @uri.path.present? ? @uri.path : '/'
  config.reject! { |_,v| v.nil? }

  @config  = config
  @bunny   = Bunny.new config
  @pool    = Pool.new Dispatcher, size: config[:size]
end

Instance Attribute Details

#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

#new_connectionObject



34
35
36
# File 'lib/msgr/client.rb', line 34

def new_connection
  @connection = Connection.new @bunny, routes, @pool, prefix: @config[:prefix]
end

#publish(routing_key, payload) ⇒ Object



83
84
85
# File 'lib/msgr/client.rb', line 83

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

#reloadObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/msgr/client.rb', line 38

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

  @connection.release
  @connection.terminate

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

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

#routesObject



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

def routes
  @routes ||= Routes.new
end

#running?Boolean

Returns:

  • (Boolean)


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

def running?; @running end

#startObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/msgr/client.rb', line 51

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

  @bunny.start
  @pool.start
  @running = true
  new_connection

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

#stop(opts = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/msgr/client.rb', line 62

def stop(opts = {})
  return unless running?
  opts.reverse_merge! timeout: 10, delete: false

  stop_connection opts

  @running = false
  wait_for_graceful_shutdown opts if opts[:timeout]

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

  if opts[:delete]
    log(:debug) { 'Delete connection.' }
    @connection.delete
  end
  @connection.terminate
  @bunny.stop

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