Refactored queue routing structure

This commit is contained in:
Michael Schmatz 2014-02-06 13:25:11 -08:00
parent 3ccaa6796a
commit 609547a37c
3 changed files with 74 additions and 29 deletions

View file

@ -38,5 +38,5 @@ module.exports.routes =
'routes/languages'
'routes/mail'
'routes/sprites'
'routes/scoring'
'routes/queue'
]

View file

@ -4,7 +4,7 @@ mongoose = require 'mongoose'
async = require 'async'
errors = require '../commons/errors'
aws = require 'aws-sdk'
db = require './db'
db = require './../routes/db'
mongoose = require 'mongoose'
events = require 'events'
queues = require '../commons/queue'
@ -17,35 +17,36 @@ connectToScoringQueue = ->
module.exports.setup = (app) ->
connectToScoringQueue()
app.get '/scoring/queue', (req,res) ->
#must also include the
queues.scoringTaskQueue.receiveMessage (err, message) ->
#check if message is empty!!!!!!!!!
if message.isEmpty()
sendResponseObject req, res, {"error":"No messages were received."}
else
messageBody = JSON.parse message.getBody()
constructTaskObject messageBody, (taskConstructionError, taskObject) ->
if taskConstructionError?
sendResponseObject req, res, {"error":taskConstructionError}
else
sendResponseObject req, res, taskObject
module.exports.dispatchTaskToConsumer = (req, res) ->
queues.scoringTaskQueue.receiveMessage (err, message) ->
if message.isEmpty()
#TODO: Set message code as 504 Gateway Timeout
sendResponseObject req, res, {"error":"No messages were received."}
else
messageBody = JSON.parse message.getBody()
constructTaskObject messageBody, (taskConstructionError, taskObject) ->
if taskConstructionError?
sendResponseObject req, res, {"error":taskConstructionError}
else
sendResponseObject req, res, taskObject
app.post '/scoring/queue', (req, res) ->
clientResponseObject = req.body
###
sampleClientResponseObject =
"processorUserID": "51eb2714fa058cb20d0006ef" #user ID of the person processing
"processingTime": 2745 #time in milliseconds
"processedSessionID": "52dfeb17c8b5f435c7000025" #the processed session
"processedSessionChangedTime": ISODate("2014-01-22T16:28:12.450Z") #to see if the session processed is the one in the database
"playerResults": [
{"ID":"51eb2714fa058cb20d0006ef", "team":"humans","metrics": {"reachedGoal":false, "rank":2}}
{"ID":"51eb2714fa058cb20d00fedg", "team":"ogres","metrics": {"reachedGoal":true, "rank":1}}
]
###
res.end("You posted an object to score!")
module.exports.processTaskResult = (req, res) ->
clientResponseObject = JSON.parse req.body
res.end("You posted an object to score!")
###
sampleClientResponseObject =
"processorUserID": "51eb2714fa058cb20d0006ef" #user ID of the person processing
"processingTime": 2745 #time in milliseconds
"processedSessionID": "52dfeb17c8b5f435c7000025" #the processed session
"processedSessionChangedTime": ISODate("2014-01-22T16:28:12.450Z") #to see if the session processed is the one in the database
"playerResults": [
{"ID":"51eb2714fa058cb20d0006ef", "team":"humans","metrics": {"reachedGoal":false, "rank":2}}
{"ID":"51eb2714fa058cb20d00fedg", "team":"ogres","metrics": {"reachedGoal":true, "rank":1}}
]
###

View file

@ -0,0 +1,44 @@
log = require 'winston'
errors = require '../commons/errors'
scoringQueue = require '../queues/scoring'
module.exports.setup = (app) ->
scoringQueue.setup()
app.all '/queue/*', (req, res) ->
setResponseHeaderToJSONContentType res
queueName = getQueueNameFromPath req.path
try
handler = loadQueueHandler queueName
if isHTTPMethodGet req
handler.dispatchTaskToConsumer req,res
else if isHTTPMethodPost req
handler.processTaskResult req,res
else
sendMethodNotSupportedError req, res
catch error
sendQueueNotFoundError req, res
setResponseHeaderToJSONContentType = (res) -> res.setHeader('Content-Type', 'application/json')
getQueueNameFromPath = (path) ->
pathPrefix = '/queue/'
pathAfterPrefix = path[pathPrefix.length..]
partsOfURL = pathAfterPrefix.split '/'
queueName = partsOfURL[0]
queueName
loadQueueHandler = (queueName) -> require ('../queues/' + queueName)
isHTTPMethodGet = (req) -> return req.route.method is 'get'
isHTTPMethodPost = (req) -> return req.route.method is 'post'
sendMethodNotSupportedError = (req, res) -> errors.badMethod(res,"Queues do not support the HTTP method used." )
sendQueueNotFoundError = (req,res) -> errors.notFound(res, "Route #{req.path} not found.")