codecombat/server/queue.coffee

144 lines
4.3 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-23 13:52:43 -05:00
db = require './db'
mongoose = require 'mongoose'
2014-01-23 13:52:43 -05:00
events = require 'events'
2014-01-22 20:48:11 -05:00
2014-01-23 13:52:43 -05:00
queueClient = null
simulationQueue = null
2014-01-23 00:41:29 -05:00
module.exports.setupRoutes = (app) ->
2014-01-23 13:52:43 -05:00
generateQueueInstance()
###queueClient.registerQueue "simulationQueue", {}, (err,data) ->
2014-01-23 13:52:43 -05:00
simulationQueue = data
simulationQueue.subscribe 'message', (err, data) ->
if data.Messages?
winston.info "Receieved message #{data.Messages?[0].Body}"
simulationQueue.deleteMessage data.Messages?[0].ReceiptHandle, ->
winston.info "Deleted message"
###
2014-01-23 00:41:29 -05:00
2014-01-23 13:52:43 -05:00
generateQueueInstance = ->
if config.isProduction
2014-01-23 13:52:43 -05:00
queueClient = new SQSQueueClient()
else
queueClient = new MongoQueueClient()
2014-01-23 00:41:29 -05:00
2014-01-23 13:52:43 -05:00
class SQSQueueClient extends AbstractQueueClient
constructor: ->
2014-01-23 00:41:29 -05:00
@configure()
@sqs = @generateSQSInstance()
2014-01-23 02:48:47 -05:00
configure: ->
aws.config.update
accessKeyId: config.queue.accessKeyId
secretAccessKey: config.queue.secretAccessKey
region: config.queue.region
2014-01-23 13:52:43 -05:00
registerQueue: (queueName, options, callback) ->
#returns new queue in data argument of callback
@sqs.createQueue {QueueName: queueName}, (err,data) =>
if err?
winston.error("There was an error creating a new SQS queue, reason: #{JSON.stringify err}")
throw new Error("Fatal SQS error, see Winston output")
newQueue = new SQSQueue(queueName, data.QueueUrl, @sqs)
callback err, newQueue
2014-01-23 02:48:47 -05:00
generateSQSInstance: ->
new aws.SQS()
2014-01-23 13:52:43 -05:00
class SQSQueue extends events.EventEmitter
constructor: (@queueName, @queueUrl, @sqs) ->
subscribe: (eventName, callback) ->
@on eventName, callback
return {eventName, callback}
2014-01-23 13:52:43 -05:00
unsubscribe: (subscriptionObject) ->
@removeListener subscriptionObject.eventName, subscriptionObject.callback
2014-01-23 13:52:43 -05:00
2014-01-23 00:41:29 -05:00
publish: (messageBody,delayInSeconds, callback) ->
@sendMessage messageBody, delayInSeconds, callback
receiveMessage: (callback) ->
2014-01-23 13:52:43 -05:00
@sqs.receiveMessage {QueueUrl: @queueUrl, WaitTimeSeconds: 20}, (err, data) =>
if err? then @emit 'error',err,data else @emit 'message',err,data
callback? err,data
deleteMessage: (receiptHandle, callback) ->
2014-01-23 13:52:43 -05:00
@sqs.deleteMessage {QueueUrl: @queueUrl, ReceiptHandle: receiptHandle}, (err, data) =>
if err? then @emit 'error',err,data else @emit 'message',err,data
callback? err,data
sendMessage: (messageBody, delaySeconds, callback) ->
2014-01-23 13:52:43 -05:00
@sqs.sendMessage {QueueUrl: @queueUrl, MessageBody: messageBody, DelaySeconds: delaySeconds}, (err, data) =>
if err? then @emit 'error',err,data else @emit 'sent',err, data
callback? err,data
listenForever: =>
async.forever (asyncCallback) =>
@receiveMessage (err, data) ->
asyncCallback(null)
2014-01-23 13:52:43 -05:00
2014-01-23 00:41:29 -05:00
class MongoQueueClient extends AbstractQueueClient
constructor: ->
@configure()
registerQueue: (queueName, options, callback) ->
channel = new MongoQueue queueName,options,this
callback(null, channel)
configure: ->
@databaseAddress = db.generateDatabaseAddress()
@mongoDatabaseName = config.mongoQueue.queueDatabaseName;
@createMongoConnection()
createMongoConnection: ->
@mongooseConnection = mongoose.createConnection "mongodb://#{@databaseAddress}/#{@mongoDatabaseName}"
@mongooseConnection.on 'error', ->
winston.error "There was an error connecting to the queue in MongoDB"
@mongooseConnection.once 'open', ->
winston.info "Successfully connected to MongoDB queue!"
class MongoQueue extends events.EventEmitter
constructor: (@queueName, options, mubSubClient) ->
@channel = mubSubClient.channel queueName, options
@subscribe 'message', receieveMessage
subscribe: (eventName, callback) ->
@channel.subscribe eventName, callback
unsubscribe: (subscriptionObject) ->
subscriptionObject.unsubscribe()
publish: (messageBody, delayInSeconds, callback) ->
#TODO: Mongo-based persistence of delayed messages
setTimeout @channel.publish.bind(this), delayInSeconds * 1000, @queueName, messageBody, callback
receieveMessage: (callback) ->
throw new Error "MongoQueue does not support fetching one message, it continually listens"
deleteMessage: (callback) ->
throw new Error "MongoQueue "
2014-01-23 13:52:43 -05:00