2014-01-22 20:48:11 -05:00
|
|
|
config = require '../server_config'
|
|
|
|
winston = require 'winston'
|
|
|
|
mongoose = require 'mongoose'
|
|
|
|
async = require 'async'
|
|
|
|
errors = require './errors'
|
2014-01-22 21:29:02 -05:00
|
|
|
aws = require 'aws-sdk'
|
2014-01-22 20:48:11 -05:00
|
|
|
|
2014-01-22 21:41:14 -05:00
|
|
|
queueInstance = null
|
2014-01-22 21:29:02 -05:00
|
|
|
|
2014-01-23 00:41:29 -05:00
|
|
|
module.exports.setupRoutes = (app) ->
|
2014-01-22 21:29:02 -05:00
|
|
|
queueInstance = generateQueueInstance()
|
2014-01-22 20:48:11 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-22 21:29:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
generateQueueInstance = ->
|
|
|
|
if config.isProduction
|
2014-01-23 02:41:26 -05:00
|
|
|
return new RemoteQueue(config.queue.simulationQueueName)
|
2014-01-22 21:29:02 -05:00
|
|
|
else
|
|
|
|
return new LocalQueue()
|
|
|
|
|
|
|
|
|
2014-01-23 00:41:29 -05:00
|
|
|
|
|
|
|
class AbstractQueue
|
2014-01-22 21:29:02 -05:00
|
|
|
configure: ->
|
2014-01-23 00:41:29 -05:00
|
|
|
throw new Error "Subclasses must override the configure method"
|
2014-01-22 21:29:02 -05:00
|
|
|
|
|
|
|
connect: ->
|
2014-01-23 00:41:29 -05:00
|
|
|
throw new Error "Subclasses must override the connect method"
|
|
|
|
|
|
|
|
createSimulationQueue: ->
|
|
|
|
throw new Error "Subclasses must override the createSimulationQueue method"
|
|
|
|
|
|
|
|
|
2014-01-22 21:29:02 -05:00
|
|
|
|
|
|
|
|
2014-01-23 00:41:29 -05:00
|
|
|
class RemoteQueue extends AbstractQueue
|
2014-01-23 02:41:26 -05:00
|
|
|
constructor: (queueName) ->
|
2014-01-23 00:41:29 -05:00
|
|
|
@configure()
|
2014-01-22 21:29:02 -05:00
|
|
|
@sqs = @generateSQSInstance()
|
2014-01-23 02:41:26 -05:00
|
|
|
@createSimulationQueueAndSetUrl queueName, (err, data) =>
|
|
|
|
@sendMessage "This is a new test message",5, (error,data) ->
|
|
|
|
if err?
|
|
|
|
winston.error "#{JSON.stringify error}"
|
|
|
|
@enterReceieveMessageForeverLoop()
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-22 21:29:02 -05:00
|
|
|
|
|
|
|
configure: ->
|
2014-01-23 00:41:29 -05:00
|
|
|
aws.config.update @generateAWSConfigurationObject()
|
|
|
|
|
2014-01-23 02:41:26 -05:00
|
|
|
enterReceieveMessageForeverLoop: ->
|
|
|
|
async.forever (asyncCallback) =>
|
|
|
|
@receiveMessage (err, data) =>
|
|
|
|
if err?
|
|
|
|
winston.error "Error receiving message, reason: #{JSON.stringify err}"
|
|
|
|
else
|
|
|
|
if data.Messages?
|
|
|
|
winston.info "Received message, content: #{JSON.stringify data.Messages[0].Body}"
|
|
|
|
winston.info "Deleting message..."
|
|
|
|
@deleteMessage data.Messages?[0].ReceiptHandle, ->
|
|
|
|
winston.info "Deleted message!"
|
|
|
|
else
|
|
|
|
winston.info "No messages to receieve"
|
|
|
|
asyncCallback(null)
|
|
|
|
|
|
|
|
createSimulationQueueAndSetUrl: (queueName, callback) ->
|
|
|
|
@sqs.createQueue {QueueName: queueName}, (err, data) =>
|
2014-01-23 00:41:29 -05:00
|
|
|
if err?
|
2014-01-23 02:41:26 -05:00
|
|
|
throw new Error "Failed to create queue \"#{queueName}\""
|
2014-01-23 00:41:29 -05:00
|
|
|
else
|
2014-01-23 02:41:26 -05:00
|
|
|
winston.info "Created queue, URL is #{data.QueueUrl}"
|
|
|
|
@queueUrl = data.QueueUrl
|
|
|
|
callback?(err,data)
|
2014-01-23 00:41:29 -05:00
|
|
|
|
|
|
|
|
2014-01-23 02:13:14 -05:00
|
|
|
receiveMessage: (callback) ->
|
2014-01-23 02:41:26 -05:00
|
|
|
@sqs.receiveMessage {QueueUrl: @queueUrl, WaitTimeSeconds: 20}, callback
|
2014-01-23 02:13:14 -05:00
|
|
|
|
|
|
|
deleteMessage: (receiptHandle, callback) ->
|
2014-01-23 02:41:26 -05:00
|
|
|
@sqs.deleteMessage {QueueUrl: @queueUrl, ReceiptHandle: receiptHandle}, callback
|
2014-01-23 02:13:14 -05:00
|
|
|
|
|
|
|
sendMessage: (messageBody, delaySeconds, callback) ->
|
2014-01-23 02:41:26 -05:00
|
|
|
@sqs.sendMessage {QueueUrl: @queueUrl, MessageBody: messageBody, DelaySeconds: delaySeconds}, callback
|
2014-01-23 00:41:29 -05:00
|
|
|
|
|
|
|
|
|
|
|
generateAWSConfigurationObject: ->
|
2014-01-22 21:29:02 -05:00
|
|
|
awsConfigurationObject =
|
|
|
|
accessKeyId: config.queue.accessKeyId
|
|
|
|
secretAccessKey: config.queue.secretAccessKey
|
|
|
|
region: config.queue.region
|
2014-01-23 00:41:29 -05:00
|
|
|
|
2014-01-22 21:29:02 -05:00
|
|
|
|
|
|
|
generateSQSInstance: ->
|
2014-01-23 00:41:29 -05:00
|
|
|
new aws.SQS()
|
2014-01-22 21:29:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-23 00:41:29 -05:00
|
|
|
class LocalQueue extends AbstractQueue
|
2014-01-22 21:29:02 -05:00
|
|
|
constructor: ()->
|
2014-01-23 00:41:29 -05:00
|
|
|
return
|
2014-01-22 21:29:02 -05:00
|
|
|
|
|
|
|
configure: () ->
|
|
|
|
super()
|
|
|
|
|
|
|
|
|