All files / src/middleware rerunUpdateTransactionTask.coffee

39.47% Statements 15/38
0% Branches 0/2
0% Functions 0/11
39.47% Lines 15/38
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 761x 1x 1x 1x   1x 1x 1x 1x 1x   1x 1x   1x                             1x                         1x                                   1x                              
Q = require "q"
Transaction = require("../model/transactions").Transaction
Task = require("../model/tasks").Task
logger = require "winston"
 
config = require '../config/config'
statsdServer = config.get 'statsd'
application = config.get 'application'
SDC = require 'statsd-client'
os = require 'os'
 
domain = "#{os.hostname()}.#{application.name}.appMetrics"
sdc = new SDC statsdServer
 
exports.setAttemptNumber = (ctx, done) ->
  Transaction.findOne { _id: ctx.parentID }, (err, transaction) ->
    if transaction.autoRetry
      if transaction.autoRetryAttempt?
        ctx.currentAttempt = transaction.autoRetryAttempt + 1
      else
        ctx.currentAttempt = 1
    transaction.save (err, tx) ->
      if err
        logger.error "Original transaction #{transaction._id} could not be updated: #{err}"
      else
        logger.debug "Original transaction ##{tx._id} Updated successfully with attempt number"
 
      done null
 
exports.updateOriginalTransaction = (ctx, done) ->
  Transaction.findOne { _id: ctx.parentID }, (err, transaction) ->
    transaction.childIDs.push ctx.transactionId
    transaction.wasRerun = true
    
    transaction.save (err, tx) ->
      if err
        logger.error "Original transaction #{transaction._id} could not be updated: #{err}"
      else
        logger.debug "Original transaction #{tx._id} - Updated successfully with childID"
 
      done null, transaction
 
exports.updateTask = (ctx, done) ->
  Task.findOne { _id: ctx.taskID }, (err, task) ->
    task.transactions.forEach (tx) ->
      if tx.tid == ctx.parentID
        tx.rerunID = ctx.transactionId
        tx.rerunStatus = ctx.transactionStatus
 
    task.save (err, task) ->
      if err
        logger.info "Rerun Task #{ctx.taskID} could not be updated: #{err}"
      else
        logger.info "Rerun Task #{ctx.taskID} - Updated successfully with rerun transaction details."
 
      done null, task
 
###
# Koa middleware for updating original transaction with childID
###
exports.koaMiddleware = (next) ->
  startTime = new Date() if statsdServer.enabled
  setAttemptNumber = Q.denodeify exports.setAttemptNumber
  yield setAttemptNumber this
  sdc.timing "#{domain}.rerunUpdateTransactionMiddleware.setAttemptNumber", startTime if statsdServer.enabled
 
  # do intial yield for koa to come back to this function with updated ctx object
  yield next
  startTime = new Date() if statsdServer.enabled
  updateOriginalTransaction = Q.denodeify exports.updateOriginalTransaction
  yield updateOriginalTransaction this
 
  updateTask = Q.denodeify exports.updateTask
  yield updateTask this
  sdc.timing "#{domain}.rerunUpdateTransactionMiddleware", startTime if statsdServer.enabled