codecombat/server/queue.coffee

110 lines
2.8 KiB
CoffeeScript
Raw Normal View History

2014-01-22 20:48:11 -05:00
config = require '../server_config'
winston = require 'winston'
mongoose = require 'mongoose'
async = require 'async'
errors = require './errors'
aws = require 'aws-sdk'
2014-01-22 20:48:11 -05:00
2014-01-22 21:41:14 -05:00
queueInstance = null
2014-01-23 00:41:29 -05:00
module.exports.setupRoutes = (app) ->
queueInstance = generateQueueInstance()
2014-01-22 20:48:11 -05:00
generateQueueInstance = ->
if config.isProduction
return new RemoteQueue(config.queue.simulationQueueName)
else
return new LocalQueue()
2014-01-23 00:41:29 -05:00
class AbstractQueue
configure: ->
2014-01-23 00:41:29 -05:00
throw new Error "Subclasses must override the configure method"
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-23 00:41:29 -05:00
class RemoteQueue extends AbstractQueue
constructor: (queueName) ->
2014-01-23 00:41:29 -05:00
@configure()
@sqs = @generateSQSInstance()
@createSimulationQueueAndSetUrl queueName, (err, data) =>
@sendMessage "This is a new test message",5, (error,data) ->
if err?
winston.error "#{JSON.stringify error}"
@enterReceieveMessageForeverLoop()
configure: ->
2014-01-23 00:41:29 -05:00
aws.config.update @generateAWSConfigurationObject()
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?
throw new Error "Failed to create queue \"#{queueName}\""
2014-01-23 00:41:29 -05:00
else
winston.info "Created queue, URL is #{data.QueueUrl}"
@queueUrl = data.QueueUrl
callback?(err,data)
2014-01-23 00:41:29 -05:00
receiveMessage: (callback) ->
@sqs.receiveMessage {QueueUrl: @queueUrl, WaitTimeSeconds: 20}, callback
deleteMessage: (receiptHandle, callback) ->
@sqs.deleteMessage {QueueUrl: @queueUrl, ReceiptHandle: receiptHandle}, callback
sendMessage: (messageBody, delaySeconds, callback) ->
@sqs.sendMessage {QueueUrl: @queueUrl, MessageBody: messageBody, DelaySeconds: delaySeconds}, callback
2014-01-23 00:41:29 -05:00
generateAWSConfigurationObject: ->
awsConfigurationObject =
accessKeyId: config.queue.accessKeyId
secretAccessKey: config.queue.secretAccessKey
region: config.queue.region
2014-01-23 00:41:29 -05:00
generateSQSInstance: ->
2014-01-23 00:41:29 -05:00
new aws.SQS()
2014-01-23 00:41:29 -05:00
class LocalQueue extends AbstractQueue
constructor: ()->
2014-01-23 00:41:29 -05:00
return
configure: () ->
super()