Class: RServiceBus2::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/rservicebus2/monitor.rb

Overview

Monitor

Direct Known Subclasses

MonitorDir, MonitorDirNotifier

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bus, name, uri) ⇒ Monitor

Resources are attached resources, and can be specified using the URI syntax.

Parameters:

  • uri (String)

    a location for the resource to which we will attach, eg redis://127.0.0.1/foo



26
27
28
29
30
31
32
33
34
# File 'lib/rservicebus2/monitor.rb', line 26

def initialize(bus, name, uri)
  @bus = bus
  new_anonymous_class = Class.new(MonitorMessage)
  Object.const_set(name, new_anonymous_class)
  @msg_type = Object.const_get(name)

  @uri = uri
  _connect
end

Instance Attribute Details

#busObject

Returns the value of attribute bus.



4
5
6
# File 'lib/rservicebus2/monitor.rb', line 4

def bus
  @bus
end

Instance Method Details

#_connectObject



16
17
18
19
20
# File 'lib/rservicebus2/monitor.rb', line 16

def _connect
  @connection = connect(@uri)
  @bus.log "#{self.class.name}. Connected to, #{@uri}" if
    ENV['QUIET'].nil?
end

#connect(_uri) ⇒ Object

The method which actually connects to the resource.



7
8
9
# File 'lib/rservicebus2/monitor.rb', line 7

def connect(_uri)
  fail 'Method, connect, needs to be implemented for resource'
end

#finishedObject

A notification that allows cleanup



37
38
# File 'lib/rservicebus2/monitor.rb', line 37

def finished
end

#lookObject

The method which actually connects to the resource.



12
13
14
# File 'lib/rservicebus2/monitor.rb', line 12

def look
  fail 'Method, Look, needs to be implemented for the Monitor'
end

#reconnectObject

At least called in the Host rescue block, to ensure all network links

are healthy


42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rservicebus2/monitor.rb', line 42

def reconnect
  begin
    finished
  rescue StandardError => e
    puts '** Monitor. An error was raised while closing connection to, ' +
      @uri
    puts 'Message: ' + e.message
    puts e.backtrace
  end

  _connect
end

#send(payload, uri) ⇒ Object



55
56
57
58
59
# File 'lib/rservicebus2/monitor.rb', line 55

def send(payload, uri)
  msg = @msg_type.new(payload, uri)

  @bus.send(msg)
end