Working on cleaning up some server errors.

This commit is contained in:
Nick Winter 2014-03-11 19:17:58 -07:00
parent 2b487eeed8
commit b560827e32
7 changed files with 28 additions and 19 deletions

View file

@ -10,7 +10,7 @@ module.exports = class Simulator
@trigger 'statusUpdate', 'Starting simulation!'
@retryDelayInSeconds = 10
@taskURL = '/queue/scoring'
destroy: ->
@off()
@cleanupSimulation()
@ -25,17 +25,21 @@ module.exports = class Simulator
success: @setupSimulationAndLoadLevel
handleFetchTaskError: (errorData) =>
console.log "There were no games to score. Error: #{JSON.stringify errorData}"
console.log "Retrying in #{@retryDelayInSeconds}"
@trigger 'statusUpdate', 'There were no games to simulate! Trying again in 10 seconds.'
console.error "There was a horrible Error: #{JSON.stringify errorData}"
@trigger 'statusUpdate', 'There was an error fetching games to simulate. Retrying in 10 seconds.'
@simulateAnotherTaskAfterDelay()
handleNoGamesResponse: ->
@trigger 'statusUpdate', 'There were no games to simulate--nice. Retrying in 10 seconds.'
@simulateAnotherTaskAfterDelay()
simulateAnotherTaskAfterDelay: =>
console.log "Retrying in #{@retryDelayInSeconds}"
retryDelayInMilliseconds = @retryDelayInSeconds * 1000
_.delay @fetchAndSimulateTask, retryDelayInMilliseconds
setupSimulationAndLoadLevel: (taskData) =>
setupSimulationAndLoadLevel: (taskData, textStatus, jqXHR) =>
return @handleNoGamesResponse() if jqXHR.status is 204
@trigger 'statusUpdate', 'Setting up simulation!'
@task = new SimulationTask(taskData)
@supermodel = new SuperModel()

View file

@ -122,6 +122,7 @@ module.exports = class MyMatchesTabView extends CocoView
failure = => @setRankingButtonText(button, 'failed')
ajaxData = { session: sessionID, levelID: @level.id, originalLevelID: @level.attributes.original, levelMajorVersion: @level.attributes.version.major }
console.log "Posting game for ranking from My Matches view."
$.ajax '/queue/scoring', {
type: 'POST'
data: ajaxData

View file

@ -66,6 +66,7 @@ module.exports = class VictoryModal extends View
ajaxData = session: @session.id, levelID: @level.id, originalLevelID: @level.get('original'), levelMajorVersion: @level.get('version').major
ladderURL = "/play/ladder/#{@level.get('slug')}#my-matches"
goToLadder = -> Backbone.Mediator.publish 'router:navigate', route: ladderURL
console.log "Posting game for ranking from victory modal."
$.ajax '/queue/scoring',
type: 'POST'
data: ajaxData

View file

@ -84,7 +84,9 @@ module.exports.dispatchTaskToConsumer = (req, res) ->
if isUserAnonymous(req) then return errors.forbidden res, "You need to be logged in to simulate games"
scoringTaskQueue.receiveMessage (err, message) ->
if err? or messageIsInvalid(message) then return errors.gatewayTimeoutError res, "Queue Receive Error:#{err}"
if err? or messageIsInvalid(message)
res.send 204, "No games to score. #{message}"
return res.end()
console.log "Received Message"
messageBody = parseTaskQueueMessage req, res, message
return unless messageBody?
@ -155,7 +157,7 @@ module.exports.processTaskResult = (req, res) ->
levelOriginalMajorVersion = levelSession.level.majorVersion
findNearestBetterSessionID levelOriginalID, levelOriginalMajorVersion, originalSessionID, sessionNewScore, opponentNewScore, opponentID ,opposingTeam, (err, opponentSessionID) ->
if err? then return errors.serverError res, "There was an error finding the nearest sessionID!"
unless opponentSessionID then return sendResponseObject req, res, {"message":"There were no more games to rank(game is at top!"}
unless opponentSessionID then return sendResponseObject req, res, {"message":"There were no more games to rank (game is at top)!"}
addPairwiseTaskToQueue [originalSessionID, opponentSessionID], (err, success) ->
if err? then return errors.serverError res, "There was an error sending the pairwise tasks to the queue!"

View file

@ -102,7 +102,7 @@ sendLadderUpdateEmail = (session, daysAgo) ->
score_history_graph_url: getScoreHistoryGraphURL session, daysAgo
defeat: defeatContext
victory: victoryContext
log.info "Sending ladder update email to #{context.recipient.address} with #{context.email_data.wins} wins and #{context.email_data.losses} since #{daysAgo} day(s) ago."
log.info "Sending ladder update email to #{context.recipient.address} with #{context.email_data.wins} wins and #{context.email_data.losses} losses since #{daysAgo} day(s) ago."
sendwithus.api.send context, (err, result) ->
log.error "Error sending ladder update email: #{err} with result #{result}" if err

View file

@ -7,8 +7,8 @@ module.exports.setupRoutes = (app) ->
return
options = { DEBUG: not config.isProduction }
module.exports.api = new sendwithusAPI swuAPIKey, options
debug = not config.isProduction
module.exports.api = new sendwithusAPI swuAPIKey, debug
module.exports.templates =
welcome_email: 'utnGaBHuSU4Hmsi7qrAypU'
ladder_update_email: 'JzaZxf39A4cKMxpPZUfWy4'

View file

@ -3,6 +3,7 @@ jsonschema = require('./user_schema')
crypto = require('crypto')
{salt, isProduction} = require('../../server_config')
mail = require '../commons/mail'
log = require 'winston'
sendwithus = require '../sendwithus'
@ -27,7 +28,7 @@ UserSchema.post('init', ->
UserSchema.methods.isAdmin = ->
p = @get('permissions')
return p and 'admin' in p
UserSchema.statics.updateMailChimp = (doc, callback) ->
return callback?() unless isProduction
return callback?() if doc.updatedMailChimp
@ -41,25 +42,25 @@ UserSchema.statics.updateMailChimp = (doc, callback) ->
return callback?() # don't add totally unsubscribed people to the list
subsChanged = doc.currentSubscriptions isnt JSON.stringify(emailSubs)
return callback?() unless emailChanged or subsChanged
params = {}
params.id = mail.MAILCHIMP_LIST_ID
params.email = if existingProps then {leid:existingProps.leid} else {email:doc.get('email')}
params.merge_vars = { groupings: [ {id: mail.MAILCHIMP_GROUP_ID, groups: newGroups} ] }
params.update_existing = true
params.double_optin = false
onSuccess = (data) ->
doc.set('mailChimp', data)
doc.updatedMailChimp = true
doc.save()
callback?()
onFailure = (error) ->
console.error 'failed to subscribe', error, callback?
log.error 'failed to subscribe', error, callback?
doc.updatedMailChimp = true
callback?()
mc.lists.subscribe params, onSuccess, onFailure
@ -75,9 +76,9 @@ UserSchema.pre('save', (next) ->
data =
email_id: sendwithus.templates.welcome_email
recipient:
address: @get 'email'
address: @get 'email'
sendwithus.api.send data, (err, result) ->
console.log 'error', err, 'result', result
log.error 'error', err, 'result', result if err
next()
)
@ -90,4 +91,4 @@ UserSchema.statics.hashPassword = (password) ->
shasum.update(salt + password)
shasum.digest('hex')
module.exports = User = mongoose.model('User', UserSchema)
module.exports = User = mongoose.model('User', UserSchema)