From b560827e323761bac74c05e6585455722568e2e5 Mon Sep 17 00:00:00 2001 From: Nick Winter Date: Tue, 11 Mar 2014 19:17:58 -0700 Subject: [PATCH] Working on cleaning up some server errors. --- app/lib/simulator/Simulator.coffee | 14 +++++++++----- app/views/play/ladder/my_matches_tab.coffee | 1 + .../play/level/modal/victory_modal.coffee | 1 + server/queues/scoring.coffee | 6 ++++-- server/routes/mail.coffee | 2 +- server/sendwithus.coffee | 4 ++-- server/users/User.coffee | 19 ++++++++++--------- 7 files changed, 28 insertions(+), 19 deletions(-) diff --git a/app/lib/simulator/Simulator.coffee b/app/lib/simulator/Simulator.coffee index f41c0acb3..38463114c 100644 --- a/app/lib/simulator/Simulator.coffee +++ b/app/lib/simulator/Simulator.coffee @@ -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() diff --git a/app/views/play/ladder/my_matches_tab.coffee b/app/views/play/ladder/my_matches_tab.coffee index 073b80a0c..6f676df78 100644 --- a/app/views/play/ladder/my_matches_tab.coffee +++ b/app/views/play/ladder/my_matches_tab.coffee @@ -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 diff --git a/app/views/play/level/modal/victory_modal.coffee b/app/views/play/level/modal/victory_modal.coffee index c71b2bcac..b339962c7 100644 --- a/app/views/play/level/modal/victory_modal.coffee +++ b/app/views/play/level/modal/victory_modal.coffee @@ -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 diff --git a/server/queues/scoring.coffee b/server/queues/scoring.coffee index 9577df844..9fdfb6e64 100644 --- a/server/queues/scoring.coffee +++ b/server/queues/scoring.coffee @@ -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!" diff --git a/server/routes/mail.coffee b/server/routes/mail.coffee index 5a46a9006..284f8d432 100644 --- a/server/routes/mail.coffee +++ b/server/routes/mail.coffee @@ -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 diff --git a/server/sendwithus.coffee b/server/sendwithus.coffee index 659ce5ec8..ad7a07500 100644 --- a/server/sendwithus.coffee +++ b/server/sendwithus.coffee @@ -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' diff --git a/server/users/User.coffee b/server/users/User.coffee index d167b2d73..9ccbab5e2 100644 --- a/server/users/User.coffee +++ b/server/users/User.coffee @@ -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) \ No newline at end of file +module.exports = User = mongoose.model('User', UserSchema)