class EbxDeliver::Writer::SNSHandler
Attributes
response_queue[R]
Public Class Methods
new()
click to toggle source
# File lib/ebx_deliver/writer/sns_handler.rb, line 6 def initialize @response_queue = AWS.sqs.queues.named('read-development-sqs') end
Public Instance Methods
command_notifications()
click to toggle source
# File lib/ebx_deliver/writer/sns_handler.rb, line 10 def command_notifications r = AWS.config.region AWS.config(region: 'us-west-2') @command_notifications ||= AWS.sns.topics.create('write-development-sns') ensure AWS.config(region: r) end
handle(request, response, &read_block)
click to toggle source
# File lib/ebx_deliver/writer/sns_handler.rb, line 18 def handle(request, response, &read_block) name = request.headers['x-amz-target'].split('.')[-1].underscore options = JSON.parse(request.body).reduce({}) {|h, (k, v)| h[k.underscore] = v; h} puts request.http_method request_id = SecureRandom.uuid publish_command(request_id, name, options) retrieve_response(request_id, response) end
publish_command(request_id, name, args)
click to toggle source
# File lib/ebx_deliver/writer/sns_handler.rb, line 28 def publish_command(request_id, name, args) command_notifications.publish({ method: name, args: args, request_id: request_id }.to_json) end
retrieve_response(request_id, response)
click to toggle source
# File lib/ebx_deliver/writer/sns_handler.rb, line 36 def retrieve_response(request_id, response) start_time = Time.now found = false loop do msgs = response_queue.receive_messages(limit: 10) msgs.each do |notification| msg = JSON.parse(JSON.parse(notification.body)['Message']) if msg['request_id'] == request_id found = true response.status = msg['response']['status'] response.headers = msg['response']['headers'] response.body= msg['response']['body'] notification.delete end end break if found #raise 'Timeout' if (Time.now - start_time) > 5 end nil end