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
# 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
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



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

def log_name; self.class.name end

#new_connectionObject



32
33
34
# File 'lib/msgr/client.rb', line 32

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

#publish(payload, opts = {}) ⇒ Object



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

def publish(payload, opts = {})
  opts[:routing_key] = opts.delete(:to) if opts[:to]
  @connection.publish payload, opts
end

#reloadObject



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

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



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

def routes
  @routes ||= Routes.new
end

#running?Boolean

Returns:



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

def running?; @running end

#startObject



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

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

  init
  launch

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

#stop(opts = {}) ⇒ Object



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

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