lib/boom_nats/application.rb in boom_nats-0.1.3 vs lib/boom_nats/application.rb in boom_nats-0.1.4
- old
+ new
@@ -7,10 +7,15 @@
attr_reader :route_topics
def initialize
@route_topics = []
@subscriptions = []
+ @callbacks = {
+ before: [],
+ after: []
+ }
+
@mutex = Mutex.new
end
def servers(value)
stop
@@ -30,21 +35,33 @@
def nats
NATS
end
+ def on_before(&block)
+ @callbacks[:before] << block
+ end
+
+ def on_after(&block)
+ @callbacks[:after] << block
+ end
+
def start
Thread.new do
+ @callbacks[:before].each { |callback| callback.call(self) }
+
nats_connect do |nats|
@route_topics.each do |rt|
@subscriptions << nats.subscribe(rt.topic, rt.options) do |msg, reply, _sub|
rt.executor.new(msg, reply, nats, rt.serializer, rt.parser)
end
end
BoomNats.logger.debug "BoomNats::started"
prepare_trap unless defined?(Rails::Railtie)
+
+ @callbacks[:after].each { |callback| callback.call(self) }
end
end
wait unless defined?(Rails::Railtie)
end