All files / lib backend-wait-logic.js

73.33% Statements 44/60
70.83% Branches 17/24
66.67% Functions 4/6
73.33% Lines 44/60
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119                          5x 5x   5x 5x 5x 5x   5x     5x 3x 3x 3x 3x       3x   2x       5x   3x 3x         3x 3x 3x         3x 3x 3x 3x 3x             3x 1x 1x 2x     2x         2x 1x 1x   1x 1x                                   5x   5x 5x 5x 4x 4x 3x             5x     5x      
/* 
 * Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
 * the License. A copy of the License is located at
 *
 *     http://aws.amazon.com/apache2.0/
 *
 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
*/
"use strict";
const chalk = require('chalk')
const ora = require('ora')
 
const awsExceptionHandler = require('./aws-operations/aws-exception-handler.js')
const awsClient = require('./aws-operations/aws-client.js')
const opsProject = require('./backend-operations/ops-project.js')
const opsCloudApi = require('./backend-operations/ops-cloud-api.js')
 
const maxCloudApiWaitLoop = 100 //each wait is 5 seconds
 
function wait(backendDetails, awsDetails, callback){
    if(isWaitNeeded(backendDetails)){
        console.log('awsmobile api call successful')
        console.log('waiting for the cloud formation to complete') 
        let mobile = awsClient.Mobile(awsDetails)
        let param = {
            projectId: backendDetails.projectId,
            syncFromResources: true
        }
        cloudApiWaitLoop(mobile, param, backendDetails, 1, callback)
    }else{
        callback(null, backendDetails)
    }
}
 
const spinner = ora('waiting ... ') //do not move inside function, the function is called recursively
function cloudApiWaitLoop(mobile, param, backendDetails, loopCount, callback){
    spinner.start()
    Iif(loopCount > maxCloudApiWaitLoop){
        spinner.stop()
        console.log(chalk.red('exceeded wait limit'))
        callback({code: maxCloudApiWaitLoop}, backendDetails)
    }else{
        mobile.describeProject(param, function(err,data){
            spinner.stop()
            Iif(err){
                console.log(chalk.red('wait interrupted'))
                console.log(err)
                callback({code: 0}, backendDetails)
            }else{
                backendDetails = data.details
                Eif(opsProject.isInNormalState(backendDetails)){
                    let cloudFormationState = opsCloudApi.getFormationStateSummary(backendDetails)
                    Eif(cloudFormationState){
                        let stateGroup = opsCloudApi.getStateGroup(cloudFormationState)
                        //status group: 
                        //-2: unrecognized status
                        //-1: not yet deployed
                        // 0: in-progress
                        // 1: terminal_complate
                        // 2: terminal_failed
                        if(stateGroup < -1){
                            console.log(chalk.red('wait interrupted') + ' unrecognized status code: ' + cloudFormationState)
                            callback({code: -2}, backendDetails)
                        }else Iif(stateGroup == -1){
                            //this happens at creating mobile project without cloud-api feature
                            callback(null, backendDetails)
                        }else Iif( stateGroup == 0){
                            spinner.start('status check #' + chalk.blue(loopCount) + ': ' + cloudFormationState)
                            setTimeout(function(){
                                cloudApiWaitLoop(mobile, param, backendDetails, loopCount + 1, callback)
                            }, 5000)
                        }else if (stateGroup == 1){
                            console.log('cloud-api update finished with status code: ' + chalk.blue(cloudFormationState))
                            callback(null, backendDetails)
                        }else{
                            console.log('cloud-api update finished with status code: ' + chalk.blue(cloudFormationState))
                            callback({code: 2}, backendDetails)
                        }
                    }else{
                        console.log(chalk.red('wait interrupted') + ' CloudFormation stack information missing')
                        callback({code: -2}, backendDetails)
                    }
                }else{
                    spinner.start('status check #' + chalk.blue(loopCount) + ': ' + backendDetails.state)
                    setTimeout(function(){
                        cloudApiWaitLoop(mobile, param, backendDetails, loopCount + 1, callback)
                    }, 5000)
                }
            }
        })
    }
}
 
function isWaitNeeded(backendDetails){ 
    let isWaitNeeded = false
 
    Eif(opsProject.isInNormalState(backendDetails)){
        let cloudFormationState = opsCloudApi.getFormationStateSummary(backendDetails)
        if(cloudFormationState){
            let stateGroup = opsCloudApi.getStateGroup(cloudFormationState)
            if(stateGroup == 0){
                isWaitNeeded = true
            }
        }
    }else{
        isWaitNeeded = true
    }
    
    return isWaitNeeded
}
 
module.exports = {
    wait
}