Initial commit
This commit is contained in:
61
lib/barong/amqp/config.rb
Normal file
61
lib/barong/amqp/config.rb
Normal file
@@ -0,0 +1,61 @@
|
||||
# encoding: UTF-8
|
||||
# frozen_string_literal: true
|
||||
|
||||
module AMQP
|
||||
class Config
|
||||
class <<self
|
||||
def data
|
||||
@data ||= Hashie::Mash.new(
|
||||
YAML.safe_load(
|
||||
ERB.new(File.read(Rails.root.join('config', 'amqp.yml'))).result
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
def connect
|
||||
data[:connect]
|
||||
end
|
||||
|
||||
def binding_exchange_id(id)
|
||||
data[:binding][id][:exchange]
|
||||
end
|
||||
|
||||
def binding_exchange(id)
|
||||
eid = binding_exchange_id(id)
|
||||
eid && exchange(eid)
|
||||
end
|
||||
|
||||
def binding_queue(id)
|
||||
queue data[:binding][id][:queue]
|
||||
end
|
||||
|
||||
def binding_worker(id)
|
||||
::Workers::AMQP.const_get(id.to_s.camelize).new
|
||||
end
|
||||
|
||||
def routing_key(id)
|
||||
binding_queue(id).first
|
||||
end
|
||||
|
||||
def topics(id)
|
||||
data[:binding][id][:topics].split(',')
|
||||
end
|
||||
|
||||
def channel(id)
|
||||
(data[:channel] && data[:channel][id]) || {}
|
||||
end
|
||||
|
||||
def queue(id)
|
||||
name = data[:queue][id][:name]
|
||||
settings = { durable: data[:queue][id][:durable] }
|
||||
[name, settings]
|
||||
end
|
||||
|
||||
def exchange(id)
|
||||
type = data[:exchange][id][:type]
|
||||
name = data[:exchange][id][:name]
|
||||
[type, name]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
45
lib/barong/amqp/queue.rb
Normal file
45
lib/barong/amqp/queue.rb
Normal file
@@ -0,0 +1,45 @@
|
||||
# encoding: UTF-8
|
||||
# frozen_string_literal: true
|
||||
|
||||
module AMQP
|
||||
class Queue
|
||||
|
||||
class <<self
|
||||
def connection
|
||||
@connection ||= ::Bunny.new(AMQP::Config.connect).tap do |conn|
|
||||
conn.start
|
||||
end
|
||||
end
|
||||
|
||||
def channel
|
||||
@channel ||= connection.create_channel
|
||||
end
|
||||
|
||||
def exchanges
|
||||
@exchanges ||= { default: channel.default_exchange }
|
||||
end
|
||||
|
||||
def exchange(id)
|
||||
exchanges[id] ||= channel.send *AMQP::Config.exchange(id)
|
||||
end
|
||||
|
||||
def publish(eid, payload, attrs={})
|
||||
payload = JSON.dump payload
|
||||
exchange(eid).publish(payload, attrs)
|
||||
end
|
||||
|
||||
# enqueue = publish to direct exchange
|
||||
def enqueue(id, payload, attrs={})
|
||||
eid = ::AMQP::Config.binding_exchange_id(id) || :default
|
||||
attrs.merge!({routing_key: AMQP::Config.routing_key(id)})
|
||||
publish(eid, payload, attrs)
|
||||
end
|
||||
|
||||
def enqueue_event(type, id, event, payload, opts={})
|
||||
routing_key = [type, id, event].join('.')
|
||||
serialized_data = JSON.dump(payload)
|
||||
channel.exchange('peatio.events.ranger', type: 'topic').publish(serialized_data, routing_key: routing_key)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user