Class: Artery::Backends::NATSPure

Inherits:
Base
  • Object
show all
Defined in:
lib/artery/backends/nats_pure.rb

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#initialize, #subscribe, #unsubscribe

Constructor Details

This class inherits a constructor from Artery::Backends::Base

Instance Method Details

#clientObject



8
9
10
# File 'lib/artery/backends/nats_pure.rb', line 8

def client
  @client || connect
end

#connectObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/artery/backends/nats_pure.rb', line 14

def connect
  @client ||= begin
    client = ::NATS.connect(options)

    client.on_reconnect do
      Artery::Instrumentation.instrument(:connection, state: :reconnected, server: client.connected_server)
    end

    client.on_disconnect do
      Artery::Instrumentation.instrument(:connection, state: :disconnected)
    end

    client.on_close do
      Artery::Instrumentation.instrument(:connection, state: :closed)
    end
    client
  end

  Artery::Instrumentation.instrument(:connection, state: :connected, server: @client.connected_server)
  @client.connect unless @client.connected?

  @client
end

#publish(route, data) ⇒ Object



61
62
63
# File 'lib/artery/backends/nats_pure.rb', line 61

def publish(route, data)
  client.publish route, data
end

#request(route, data, opts = {}) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/artery/backends/nats_pure.rb', line 52

def request(route, data, opts = {})
  opts[:timeout] ||= Artery.request_timeout
  # Always synchronous for now
  response = client.request route, data, **opts
  yield response.data
rescue ::NATS::Timeout
  yield(TimeoutError.new(request: { route: route, data: data }))
end

#startObject



43
44
45
46
47
48
49
50
# File 'lib/artery/backends/nats_pure.rb', line 43

def start
  @stop = false
  connect

  yield

  sleep 0.1 until @stop
end

#stopObject



38
39
40
41
# File 'lib/artery/backends/nats_pure.rb', line 38

def stop
  client.close
  @stop = true
end