2014-02-06 16:25:11 -05:00
|
|
|
log = require 'winston'
|
|
|
|
errors = require '../commons/errors'
|
|
|
|
scoringQueue = require '../queues/scoring'
|
|
|
|
|
|
|
|
module.exports.setup = (app) ->
|
|
|
|
scoringQueue.setup()
|
|
|
|
|
2014-02-24 10:50:43 -05:00
|
|
|
#app.post '/queue/scoring/pairwise', (req, res) ->
|
|
|
|
# handler = loadQueueHandler 'scoring'
|
|
|
|
# handler.addPairwiseTaskToQueue req, res
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-03-15 12:20:13 -04:00
|
|
|
app.get '/queue/messagesInQueueCount', (req, res) ->
|
|
|
|
handler = loadQueueHandler 'scoring'
|
|
|
|
handler.messagesInQueueCount req, res
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-03-22 12:17:14 -04:00
|
|
|
app.post '/queue/scoring/resimulateAllSessions', (req, res) ->
|
|
|
|
handler = loadQueueHandler 'scoring'
|
|
|
|
handler.resimulateAllSessions req, res
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-05-19 13:11:20 -04:00
|
|
|
app.post '/queue/scoring/getTwoGames', (req, res) ->
|
|
|
|
handler = loadQueueHandler 'scoring'
|
2014-06-30 22:16:26 -04:00
|
|
|
handler.getTwoGames req, res
|
|
|
|
|
2014-05-19 21:58:45 -04:00
|
|
|
app.put '/queue/scoring/recordTwoGames', (req, res) ->
|
|
|
|
handler = loadQueueHandler 'scoring'
|
|
|
|
handler.recordTwoGames req, res
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-02-06 16:25:11 -05:00
|
|
|
app.all '/queue/*', (req, res) ->
|
|
|
|
setResponseHeaderToJSONContentType res
|
2014-06-09 12:01:43 -04:00
|
|
|
|
2014-02-06 16:25:11 -05:00
|
|
|
queueName = getQueueNameFromPath req.path
|
|
|
|
try
|
|
|
|
handler = loadQueueHandler queueName
|
|
|
|
if isHTTPMethodGet req
|
2014-06-30 22:16:26 -04:00
|
|
|
handler.dispatchTaskToConsumer req, res
|
2014-02-10 16:18:39 -05:00
|
|
|
else if isHTTPMethodPut req
|
2014-06-30 22:16:26 -04:00
|
|
|
handler.processTaskResult req, res
|
2014-02-10 16:18:39 -05:00
|
|
|
else if isHTTPMethodPost req
|
2014-09-27 09:06:53 -04:00
|
|
|
handler.createNewTask req, res
|
2014-02-06 16:25:11 -05:00
|
|
|
else
|
|
|
|
sendMethodNotSupportedError req, res
|
|
|
|
catch error
|
2014-02-06 17:32:35 -05:00
|
|
|
log.error error
|
2014-02-10 16:18:39 -05:00
|
|
|
sendQueueError req, res, error
|
2014-02-06 16:25:11 -05:00
|
|
|
|
|
|
|
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'
|
|
|
|
|
2014-02-10 16:18:39 -05:00
|
|
|
isHTTPMethodPut = (req) -> return req.route.method is 'put'
|
|
|
|
|
2014-06-30 22:16:26 -04:00
|
|
|
sendMethodNotSupportedError = (req, res) -> errors.badMethod(res, ['GET', 'POST', 'PUT'], 'Queues do not support the HTTP method used.' )
|
2014-02-06 16:25:11 -05:00
|
|
|
|
2014-06-30 22:16:26 -04:00
|
|
|
sendQueueError = (req, res, error) -> errors.serverError(res, "Route #{req.path} had a problem: #{error}")
|