Class: Peatio::MQ::Events::RangerEvents
- Inherits:
-
Object
- Object
- Peatio::MQ::Events::RangerEvents
- Defined in:
- lib/peatio/mq/events.rb
Instance Attribute Summary collapse
-
#exchange_name ⇒ Object
Returns the value of attribute exchange_name.
Instance Method Summary collapse
- #connect! ⇒ Object
-
#initialize ⇒ RangerEvents
constructor
A new instance of RangerEvents.
- #publish(type, id, event, payload) ⇒ Object
- #subscribe ⇒ Object
Constructor Details
#initialize ⇒ RangerEvents
Returns a new instance of RangerEvents.
56 57 58 |
# File 'lib/peatio/mq/events.rb', line 56 def initialize @exchange_name = "peatio.events.ranger" end |
Instance Attribute Details
#exchange_name ⇒ Object
Returns the value of attribute exchange_name.
54 55 56 |
# File 'lib/peatio/mq/events.rb', line 54 def exchange_name @exchange_name end |
Instance Method Details
#connect! ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/peatio/mq/events.rb', line 60 def connect! if Peatio::MQ::Client.channel.nil? Peatio::MQ::Client.new Peatio::MQ::Client.connect! Peatio::MQ::Client.create_channel! end @exchange = Peatio::MQ::Client.channel.topic(@exchange_name) end |
#publish(type, id, event, payload) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/peatio/mq/events.rb', line 69 def publish(type, id, event, payload) routing_key = [type, id, event].join(".") serialized_data = JSON.dump(payload) @exchange.publish(serialized_data, routing_key: routing_key) Peatio::Logger::debug { "published event to #{routing_key} " } yield if block_given? end |
#subscribe ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/peatio/mq/events.rb', line 80 def subscribe exchange = Peatio::MQ::Client.channel.topic(@exchange_name) suffix = "#{Socket.gethostname.split(/-/).last}#{Random.rand(10_000)}" queue_name = "#{@topic_name}.ranger.#{suffix}" Peatio::MQ::Client.channel .queue(queue_name, durable: false, auto_delete: true) .bind(exchange, routing_key: "#").subscribe do |delivery_info, , payload| # type@id@event # type can be public|private # id can be user id or market # event can be anything like order_completed or just trade routing_key = delivery_info.routing_key if routing_key.count(".") != 2 Peatio::Logger::error do "got invalid routing key from amqp: #{routing_key}" end next end type, id, event = routing_key.split(".") payload_decoded = JSON.parse payload if type == "private" Client.user(id) do |client| if client.streams.include?(event) client.send_payload(event => payload_decoded) end end next end stream = [id, event].join(".") Client.all.each do |handler| if handler.streams.include?(id) or handler.streams.include?(stream) handler.send_payload(stream => payload_decoded) end end end end |