Best JavaScript code snippet using redwood
executionengine.js
Source:executionengine.js  
...35            req.end();36            req.destroy();37        })38    }39    cleanUpMachines(execution.machines,req.body.executionID);40    unlockMachines(execution.machines);41    unlockCloudMachines(execution.machines);42    for(var testcase in execution.currentTestCases){43        if(execution.currentTestCases[testcase].testcase.dbTestCase.tcData && execution.currentTestCases[testcase].testcase.dbTestCase.tcData.length > 0){44            updateExecutionTestCase({_id:execution.testcases[testcase]._id},{$set:{status:"Not Run","result":"",resultID:null,error:"",trace:"",startdate:"",enddate:"",runtime:""}});45        }46        else{47            updateExecutionTestCase({_id:execution.testcases[testcase]._id},{$set:{status:"Not Run","result":"",resultID:null,error:"",trace:"",startdate:"",enddate:"",runtime:""}});48        }49    }50    //git.deleteFiles(path.join(__dirname, '../public/automationscripts/'+req.cookies.project+"/"+req.cookies.username+"/build"),os.tmpDir()+"jar_"+req.body.executionID);51    deleteDir(os.tmpDir()+"/jar_"+req.body.executionID);52    common.logger.log("Stop button was pushed");53    cleanExecutionMachines(req.body.executionID,function(){54        updateExecution({_id:req.body.executionID},{$set:{status:"Ready To Run"}},true,function(){55            executionsRoute.updateExecutionTotals(req.body.executionID);56            res.contentType('json');57            res.json({success:true});58            delete executions[req.body.executionID];59        });60    });61};62exports.startexecutionPost = function(req, res){63    db = common.getDB();64    if (req.body.testcases.length == 0){65        res.contentType('json');66        res.json({error:"No Test Cases are selected for execution."});67        return;68    }69    var executionID =  req.body.executionID;70    var ignoreStatus =  req.body.ignoreStatus;71    var ignoreScreenshots =  req.body.ignoreScreenshots;72    var allScreenshots =  req.body.allScreenshots;73    var ignoreAfterState =  req.body.ignoreAfterState;74    var sendEmail =  req.body.sendEmail;75    var machines = req.body.machines;76    var variables = {};77    var testcases = req.body.testcases;78    var template = null;79    //clean up previous files if needed80    /*81    for(var file in fileSync){82        if (file.indexOf() != "_id"){83            record.getAt(0).set(propt,item[propt]);84        }85    }86    */87    req.body.variables.forEach(function(variable){88        variables[variable.name] = variable.value;89    });90    var machineConflict = false;91    var updatingConflict = false;92    machines.forEach(function(machine){93        if (machine.state == "Running Test"){94            machineConflict = true;95        }96        else if (machine.state == "Updating"){97            updatingConflict = true;98        }99    });100    if(machineConflict == true){101        res.contentType('json');102        res.json({error:"Selected machines are currently running other tests."});103        return;104    }105    if(updatingConflict == true){106        res.contentType('json');107        res.json({error:"Selected machines are being updated."});108        return;109    }110    if(executions[executionID]){111        res.contentType('json');112        res.json({error:"Execution is already running."});113        return;114    }115    if(req.body.templates){116        template = req.body.templates[0]117    }118    executions[executionID] = {template:template,sendEmail:sendEmail,ignoreAfterState:ignoreAfterState,ignoreStatus:ignoreStatus,ignoreScreenshots:ignoreScreenshots,allScreenshots:allScreenshots,testcases:{},machines:machines,variables:variables,currentTestCases:{},project:req.cookies.project,username:req.cookies.username,returnVars:{}};119    updateExecution({_id:executionID},{$set:{status:"Running",user:req.cookies.username}},false);120    compileBuild(req.cookies.project,req.cookies.username,function(err){121        if (err != null){122            res.contentType('json');123            res.json({error:"Unable to compile scripts."});124            updateExecution({_id:executionID},{$set:{status:"Ready To Run"}},true);125            delete executions[executionID];126        }127        else{128            //copy files for each execution to prevent conflicts129            //git.copyFiles(path.join(__dirname, '../public/automationscripts/'+req.cookies.project+"/"+req.cookies.username+"/build"),"jar",os.tmpDir()+"/jar_"+executionID,function(){130            copyFiles(path.join(__dirname, '../public/automationscripts/'+req.cookies.project+"/"+req.cookies.username+"/build"),os.tmpDir()+"/jar_"+executionID,function(){131                copyFiles(path.join(__dirname, '../public/automationscripts/'+req.cookies.project+"/"+req.cookies.username+"/build/jar"),os.tmpDir()+"/jar_"+executionID,function(){132                    zipPythonFiles(path.join(__dirname, '../public/automationscripts/'+req.cookies.project+"/"+req.cookies.username),os.tmpDir()+"/jar_"+executionID,function(){133                        cacheSourceCode(path.join(__dirname, '../public/automationscripts/'+req.cookies.project+"/"+req.cookies.username),function(sourceCache){134                            if(executions[executionID]){135                                executions[executionID].sourceCache = sourceCache;136                            }137                            else{138                                return;139                            }140                            verifyMachineState(machines,function(err){141                                if(err){142                                    updateExecution({_id:executionID},{$set:{status:"Ready To Run"}},true);143                                    res.contentType('json');144                                    res.json({error:err});145                                    //git.deleteFiles(path.join(__dirname, '../public/automationscripts/'+req.cookies.project+"/"+req.cookies.username+"/build"),os.tmpDir()+"/jar_"+req.body.executionID);146                                    deleteDir(os.tmpDir()+"/jar_"+req.body.executionID);147                                    delete executions[executionID];148                                    return;149                                }150                                VerifyCloudCapacity(executions[executionID].template,function(response){151                                    if(response.err || response.capacityAvailable == false){152                                        var message = "";153                                        if(response.err){154                                            message = response.err155                                        }156                                        else{157                                            message = "Cloud does not have the capacity to run this execution."158                                        }159                                        updateExecution({_id:executionID},{$set:{status:"Ready To Run",cloudStatus:"Error: "+message}},true);160                                        res.contentType('json');161                                        res.json({error:"Cloud Error: "+message});162                                        //git.deleteFiles(path.join(__dirname, '../public/automationscripts/'+req.cookies.project+"/"+req.cookies.username+"/build"),os.tmpDir()+"/jar_"+req.body.executionID);163                                        deleteDir(os.tmpDir()+"/jar_"+req.body.executionID);164                                        delete executions[executionID];165                                        return;166                                    }167                                    res.contentType('json');168                                    res.json({success:true});169                                    lockMachines(machines,executionID,function(){170                                        if(executions[executionID].template){171                                            updateExecution({_id:executionID},{$set:{status:"Running",cloudStatus:"Provisioning Virtual Machines..."}},false);172                                        }173                                        else{174                                            updateExecution({_id:executionID},{$set:{status:"Running",cloudStatus:""}},false);175                                        }176                                        StartCloudMachines(template,executionID,function(cloudMachines){177                                            if(cloudMachines.err){178                                                unlockMachines(machines);179                                                updateExecution({_id:executionID},{$set:{status:"Ready To Run",cloudStatus:"Error: "+cloudMachines.err}},true);180                                                //git.deleteFiles(path.join(__dirname, '../public/automationscripts/'+req.cookies.project+"/"+req.cookies.username+"/build"),os.tmpDir()+"/jar_"+req.body.executionID);181                                                deleteDir(os.tmpDir()+"/jar_"+req.body.executionID);182                                                delete executions[executionID];183                                                return;184                                            }185                                            if(executions[executionID].template){186                                                updateExecution({_id:executionID},{$set:{cloudStatus:"Virtual Machines have been provisioned."}},false);187                                            }188                                            executions[executionID].machines = machines.concat(cloudMachines);189                                            getGlobalVars(executionID,function(){190                                                testcases.forEach(function(testcase){191                                                    testcase.dbID = testcase.testcaseID;192                                                    if(testcase.tcData){193                                                        testcase.testcaseID = testcase.testcaseID+testcase.rowIndex;194                                                        executions[executionID].testcases[testcase.testcaseID] = testcase;195                                                    }196                                                    else{197                                                        executions[executionID].testcases[testcase.testcaseID] = testcase;198                                                    }199                                                });200                                                //see if there is a base state201                                                suiteBaseState(executionID,executions[executionID].machines,function(){202                                                    //magic happens here203                                                    applyMultiThreading(executionID,function(){204                                                        updateExecution({_id:executionID},{$set:{status:"Running",lastRunDate:new Date()}},false,function(){205                                                            executeTestCases(executions[executionID].testcases,executionID);206                                                        });207                                                    })208                                                });209                                            });210                                        });211                                    });212                                });213                            });214                        });215                    });216                });217            });218        }219    });220};221function zipPythonFiles(projectDir,destDir,callback){222    fs.mkdir(destDir,function(){223        fs.exists(projectDir + "/PythonWorkDir",function(exists){224            if(exists == true){225                git.lsFiles(projectDir + "/src/",["*.py"],function(data){226                    if ((data != "")&&(data.indexOf("\n") != -1)){227                        var libDir = projectDir + "/PythonWorkDir/Lib";228                        if(process.platform != "win32"){229                            libDir = projectDir + "/PythonWorkDir/lib/python2.7";230                        }231                        zipDir(libDir,destDir+"/pythonLibs.zip",['**','!**.pyc','!**/*.pyc'],function(){232                            zipDir(projectDir + "/src/",destDir+"/pythonSources.zip",['**/*.py','**.py','**/*.cfg','**/*.ini'],function(){233                                callback();234                            });235                        })236                    }237                    else{238                        callback();239                    }240                });241            }242            else{243                callback()244            }245        });246    });247}248function zipDir(sourceDir,targetFile,pattern,callback){249    fs.exists(sourceDir,function(exists){250        if(exists == true){251            var output = fs.createWriteStream(targetFile);252            /*253            var archive = archiver('tar', {254                gzip: true,255                gzipOptions: {256                    level: 1257                }258            });259            */260            var archive = archiver('zip');261            output.on('close', function () {262                output.end();263                callback();264                console.log(archive.pointer() + ' total bytes');265                console.log('archiver has been finalized and the output file descriptor has closed.');266            });267            archive.on('error', function(err){268                console.log(err);269                //throw err;270            });271            archive.pipe(output);272            archive.bulk([273                { expand: true, cwd: sourceDir, src: pattern}274            ]);275            archive.finalize();276        }277        else{278            callback()279        }280    })281}282exports.compileBuild = function(project,username,callback){compileBuild(project,username,callback)};283function compileBuild(project,username,callback){284    var workDir = rootDir+project+"/"+username;285    var msg = {project:project,username:username,java:true,python:true,csharp:true};286    var compileScripts = function(){287        var compileOut = "";288        //random id for compile proc289        var id;290        for (var i = 0; i < 24; i++) {291            id += Math.floor(Math.random() * 10).toString(16);292        }293        compile.operation(msg,id,function(data){compileOut = compileOut + data},function(){294            if (compileOut.indexOf("BUILD FAILED") != -1){295                callback("unable to compile")296            }297            else{298                callback(null)299            }300        })301    };302    needToCompileJava(workDir,project,function(compileJava){303        msg.java = compileJava;304        needToCompilePython(workDir,project,username,function(compilePython){305            msg.python = compilePython;306            needToCompileCSharp(workDir,project,function(compileCSharp){307                msg.csharp = compileCSharp;308                compileScripts();309            })310        })311    })312}313function needToCompilePython(workDir,project,username,callback){314    var needToCompile = true;315    if(compilations[project+username+"python"]){316        git.filesModifiedSinceDate(workDir,compilations[project+username+"python"],function(data){317            if (data == ""){318                needToCompile = false;319            }320            else{321                compilations[project+username+"python"] = new Date();322            }323            callback(needToCompile)324        });325    }326    else{327        compilations[project+username+"python"] = new Date();328        callback(needToCompile);329    }330}331function needToCompileJava(workDir,project,callback){332    var needToCompile = true;333    fs.exists(workDir+"/build/jar/"+project+".jar", function (exists) {334        if(exists == true){335            fs.stat(workDir+"/build/jar/"+project+".jar",function(err,stats){336                if(err) {337                    callback(needToCompile);338                }339                else{340                    git.filesModifiedSinceDate(workDir,stats.mtime,function(data){341                        if (data == ""){342                            needToCompile = false;343                        }344                        callback(needToCompile)345                    });346                }347            });348        }349        else{350            callback(needToCompile);351        }352    });353}354function needToCompileCSharp(workDir,project,callback){355    var needToCompile = true;356    fs.exists(workDir+"/build/RedwoodHQAutomation.dll", function (exists) {357        if(exists == true){358            fs.stat(workDir+"/build/RedwoodHQAutomation.dll",function(err,stats){359                if(err) {360                    callback(needToCompile);361                }362                else{363                    git.filesModifiedSinceDate(workDir,stats.mtime,function(data){364                        if (data == ""){365                            needToCompile = false;366                        }367                        callback(needToCompile)368                    });369                }370            });371        }372        else{373            callback(needToCompile);374        }375    });376}377function applyMultiThreading(executionID,callback){378    var count = 0;379    var mainMachinesCount = executions[executionID].machines.length;380    executions[executionID].machines.forEach(function(machine){381        db.collection('machines', function(err, collection) {382            collection.findOne({_id:new ObjectID(machine._id)}, {}, function(err, dbMachine) {383                var startThread = 0;384                if(dbMachine){385                    startThread = dbMachine.takenThreads - machine.threads;386                }387                //if(startThread != 0){388                //    startThread = dbMachine.lastStartThread + 1389                //}390                //collection.findAndModify({_id:new ObjectID(machine._id)},{},{$set:{lastStartThread:startThread}},{safe:true,new:true},function(err,data){391                //});392                common.logger.info("staring at:"+startThread);393                machine.threadID = startThread;394                //if more than one thread run base state if not let it go395                if (machine.threads > 1){396                    machine.multiThreaded = true;397                    agentBaseState(executions[executionID].project+"/"+executions[executionID].username,executionID,machine.host,machine.port,machine.threadID,function(err){398                        machine.runBaseState = true;399                        var newMachineCount = 1;400                        for(var i=machine.threads;i>1;i--){401                            var newMachine = {};402                            for (var key in machine) {403                                newMachine[key] = machine[key];404                            }405                            newMachine.threadID = i+startThread-1;406                            common.logger.info(newMachine);407                            if(!executions[executionID]) return;408                            executions[executionID].machines.push(newMachine);409                            sendAgentCommand(newMachine.host,newMachine.port,{command:"start launcher",executionID:executionID,threadID:newMachine.threadID},3,function(err){410                                newMachineCount++;411                                if (newMachineCount == machine.threads){412                                    count++;413                                    if(count == mainMachinesCount){414                                        callback();415                                    }416                                }417                            });418                        }419                    });420                }421                else{422                    count++;423                    if(count == mainMachinesCount){424                        callback();425                    }426                }427            });428        });429    });430}431function suiteBaseState(executionID,machines,callback){432    var count = 0;433    var foundSuiteState = false;434    var suiteBaseTCs = [];435    var lastMachine = function(){436        if (count === machines.length){437            if (suiteBaseTCs.length > 0){438                executions[executionID].cachedTCs = executions[executionID].testcases;439                executions[executionID].testcases = {};440                suiteBaseTCs.forEach(function(tc){441                    executions[executionID].testcases[tc.testcaseID] = tc;442                });443                executions[executionID].baseStateFailed = false;444            }445            callback()446        }447    };448    machines.forEach(function(machine){449        if(machine.baseState){450            foundSuiteState = true;451            machine.roles.push(machine.host);452            db.collection('testcases', function(err, collection) {453                collection.insert({baseState:true,name:machine.host+"_state",status:"Automated",type:"collection",collection:[{order:"1",actionid:machine.baseState,host:machine.host,executionflow:"Record Error Stop Test Case",parameters:[]}]}, {safe:true},function(err,testcaseData){454                    db.collection('executiontestcases', function(err, collection) {455                        //collection.save({_id:machine.resultID},{},{$set:{executionID:executionID,name:machine.host+"_state",status:"Not Run",testcaseID:testcaseData[0]._id.__id,_id: machine.resultID}}, {safe:true,new:true},function(err,returnData){456                        collection.save({baseState:true,executionID:executionID,name:machine.host+"_state",status:"Not Run",testcaseID:testcaseData[0]._id.toString(),_id: machine.baseStateTCID},function(err,returnData){457                            suiteBaseTCs.push({testcaseID:testcaseData[0]._id.toString(),retryCount:0,_id:machine.baseStateTCID,status:"Not Run",name:machine.host+"_state",dbID:testcaseData[0]._id.toString(),type:"collection"});458                            count++;459                            lastMachine();460                        });461                    });462                });463            });464        }465        else{466            count++;467        }468        lastMachine();469    });470}471exports.cacheSourceCode = function(rootPath,callback){cacheSourceCode(rootPath,callback)};472function cacheSourceCode(rootPath,callback){473    git.lsFiles(rootPath,["*.groovy","*.java"],function(data){474        var files = [];475        if ((data != "")&&(data.indexOf("\n") != -1)){476            files = data.split("\n",data.match(/\n/g).length);477        }478        callback(files);479    })480}481function getGlobalVars(executionID,callback){482    db.collection('variables', function(err, collection) {483        collection.find({taskVar:false}, {}, function(err, cursor) {484            cursor.each(function(err, variable) {485                if(variable == null) {486                    callback();487                }488                else{489                    executions[executionID].variables[variable.name] = variable.value;490                }491            });492        })493    });494}495function cleanUpMachines(machines,executionID,callback){496    var count = 0;497    machines.forEach(function(machine){498        sendAgentCommand(machine.host,machine.port,{command:"cleanup",executionID:executionID},3,function(err){499            count++;500            if(count == machines.length){501                if(callback) callback();502            }503        });504    })505}506function executeTestCases(testcases,executionID){507    if (!executions[executionID]) return;508    executions[executionID].executingTCs = true;509    var variables = executions[executionID].variables;510    var machines = executions[executionID].machines;511    var tcArray = [];512    for(var key in testcases){513        tcArray.push(key);514    }515    //execution all done516    //if (tcArray.length == 0){517    var finishExecution = function(callback){518        if (executions[executionID].cachedTCs){519            if(executions[executionID].baseStateFailed === true){520                unlockCloudMachines(executions[executionID].machines);521                unlockMachines(machines,function(){522                    cleanUpMachines(executions[executionID].machines,executionID,function(){523                    });524                    updateExecution({_id:executionID},{$set:{status:"Ready To Run"}},true,function(){525                        executionsRoute.updateExecutionTotals(executionID,function(){526                            if(executions[executionID].sendEmail == true) sendNotification(executionID);527                            //git.deleteFiles(path.join(__dirname, '../public/automationscripts/'+executions[executionID].project+"/"+executions[executionID].username+"/build"),os.tmpDir()+"/jar_"+executionID);528                            deleteDir(os.tmpDir()+"/jar_"+executionID);529                            delete executions[executionID];530                        });531                    });532                    callback(true)533                });534                return;535            }536            executions[executionID].machines.forEach(function(machine){537                machine.baseState = null;538            });539            executions[executionID].testcases = executions[executionID].cachedTCs;540            delete executions[executionID].cachedTCs;541            tcArray = [];542            for(key in executions[executionID].testcases){543                tcArray.push(key);544            }545            testcases = executions[executionID].testcases;546            callback(false)547        }548        else{549            if(executions[executionID]){550                var shouldFinish = true;551                //if something is running dont finish the execution552                for(var tc in executions[executionID].testcases) {553                    if(tc.finished == false) shouldFinish = false;554                }555                if(shouldFinish == false) return;556                unlockCloudMachines(executions[executionID].machines);557                unlockMachines(executions[executionID].machines,function(){558                    if(!executions[executionID]) return;559                    cleanUpMachines(executions[executionID].machines,executionID,function(){560                    });561                    updateExecution({_id:executionID},{$set:{status:"Ready To Run"}},true,function(){562                        executionsRoute.updateExecutionTotals(executionID,function(){563                            if(executions[executionID] && executions[executionID].sendEmail == true) sendNotification(executionID);564                            //git.deleteFiles(path.join(__dirname, '../public/automationscripts/'+executions[executionID].project+"/"+executions[executionID].username+"/build"),os.tmpDir()+"/jar_"+executionID);565                            deleteDir(os.tmpDir()+"/jar_"+executionID);566                            delete executions[executionID];567                        });568                    });569                    callback(true)570                });571            }572            //return;573        }...executionengine original.js
Source:executionengine original.js  
...23    if(!execution) {24        res.contentType('json');25        res.json({success:true});26    }27    cleanUpMachines(execution.machines,req.body.executionID);28    unlockMachines(execution.machines);29    unlockCloudMachines(execution.machines);30    for(var testcase in execution.currentTestCases){31        updateExecutionTestCase({_id:execution.testcases[testcase]._id},{$set:{status:"Not Run","result":"",resultID:null,error:"",trace:"",startdate:"",enddate:"",runtime:""}});32    }33    //git.deleteFiles(path.join(__dirname, '../public/automationscripts/'+req.cookies.project+"/"+req.cookies.username+"/build"),os.tmpDir()+"jar_"+req.body.executionID);34    deleteDir(os.tmpDir()+"/jar_"+req.body.executionID);35    common.logger.log("Stop button was pushed");36    cleanExecutionMachines(req.body.executionID,function(){37        updateExecution({_id:req.body.executionID},{$set:{status:"Ready To Run"}},true,function(){38            executionsRoute.updateExecutionTotals(req.body.executionID);39            res.contentType('json');40            res.json({success:true});41            delete executions[req.body.executionID];42        });43    });44};45exports.startexecutionPost = function(req, res){46    db = common.getDB();47    if (req.body.testcases.length == 0){48        res.contentType('json');49        res.json({error:"No Test Cases are selected for execution."});50        return;51    }52    var executionID =  req.body.executionID;53    var ignoreStatus =  req.body.ignoreStatus;54    var ignoreScreenshots =  req.body.ignoreScreenshots;55    var allScreenshots =  req.body.allScreenshots;56    var ignoreAfterState =  req.body.ignoreAfterState;57    var sendEmail =  req.body.sendEmail;58    var machines = req.body.machines;59    var variables = {};60    var testcases = req.body.testcases;61    var template = null;62    req.body.variables.forEach(function(variable){63        variables[variable.name] = variable.value;64    });65    var machineConflict = false;66    var updatingConflict = false;67    machines.forEach(function(machine){68        if (machine.state == "Running Test"){69            machineConflict = true;70        }71        else if (machine.state == "Updating"){72            updatingConflict = true;73        }74    });75    if(machineConflict == true){76        res.contentType('json');77        res.json({error:"Selected machines are currently running other tests."});78        return;79    }80    if(updatingConflict == true){81        res.contentType('json');82        res.json({error:"Selected machines are being updated."});83        return;84    }85    if(executions[executionID]){86        res.contentType('json');87        res.json({error:"Execution is already running."});88        return;89    }90    if(req.body.templates){91        template = req.body.templates[0]92    }93    executions[executionID] = {template:template,sendEmail:sendEmail,ignoreAfterState:ignoreAfterState,ignoreStatus:ignoreStatus,ignoreScreenshots:ignoreScreenshots,allScreenshots:allScreenshots,testcases:{},machines:machines,variables:variables,currentTestCases:{},project:req.cookies.project,username:req.cookies.username,returnVars:{}};94    compileBuild(req.cookies.project,req.cookies.username,function(err){95        if (err != null){96            res.contentType('json');97            res.json({error:"Unable to compile scripts."});98            delete executions[executionID];99        }100        else{101            //copy files for each execution to prevent conflicts102            //git.copyFiles(path.join(__dirname, '../public/automationscripts/'+req.cookies.project+"/"+req.cookies.username+"/build"),"jar",os.tmpDir()+"/jar_"+executionID,function(){103            copyFiles(path.join(__dirname, '../public/automationscripts/'+req.cookies.project+"/"+req.cookies.username+"/build"),os.tmpDir()+"/jar_"+executionID,function(){104                copyFiles(path.join(__dirname, '../public/automationscripts/'+req.cookies.project+"/"+req.cookies.username+"/build/jar"),os.tmpDir()+"/jar_"+executionID,function(){105                    zipPythonFiles(path.join(__dirname, '../public/automationscripts/'+req.cookies.project+"/"+req.cookies.username),os.tmpDir()+"/jar_"+executionID,function(){106                        cacheSourceCode(path.join(__dirname, '../public/automationscripts/'+req.cookies.project+"/"+req.cookies.username),function(sourceCache){107                            executions[executionID].sourceCache = sourceCache;108                            verifyMachineState(machines,function(err){109                                if(err){110                                    updateExecution({_id:executionID},{$set:{status:"Ready To Run"}},true);111                                    res.contentType('json');112                                    res.json({error:err});113                                    //git.deleteFiles(path.join(__dirname, '../public/automationscripts/'+req.cookies.project+"/"+req.cookies.username+"/build"),os.tmpDir()+"/jar_"+req.body.executionID);114                                    deleteDir(os.tmpDir()+"/jar_"+req.body.executionID);115                                    delete executions[executionID];116                                    return;117                                }118                                VerifyCloudCapacity(executions[executionID].template,function(response){119                                    if(response.err || response.capacityAvailable == false){120                                        var message = "";121                                        if(response.err){122                                            message = response.err123                                        }124                                        else{125                                            message = "Cloud does not have the capacity to run this execution."126                                        }127                                        updateExecution({_id:executionID},{$set:{status:"Ready To Run",cloudStatus:"Error: "+message}},true);128                                        res.contentType('json');129                                        res.json({error:"Cloud Error: "+message});130                                        //git.deleteFiles(path.join(__dirname, '../public/automationscripts/'+req.cookies.project+"/"+req.cookies.username+"/build"),os.tmpDir()+"/jar_"+req.body.executionID);131                                        deleteDir(os.tmpDir()+"/jar_"+req.body.executionID);132                                        delete executions[executionID];133                                        return;134                                    }135                                    res.contentType('json');136                                    res.json({success:true});137                                    lockMachines(machines,executionID,function(){138                                        if(executions[executionID].template){139                                            updateExecution({_id:executionID},{$set:{status:"Running",cloudStatus:"Provisioning Virtual Machines..."}},false);140                                        }141                                        else{142                                            updateExecution({_id:executionID},{$set:{status:"Running",cloudStatus:""}},false);143                                        }144                                        StartCloudMachines(template,executionID,function(cloudMachines){145                                            if(cloudMachines.err){146                                                unlockMachines(machines);147                                                updateExecution({_id:executionID},{$set:{status:"Ready To Run",cloudStatus:"Error: "+cloudMachines.err}},true);148                                                //git.deleteFiles(path.join(__dirname, '../public/automationscripts/'+req.cookies.project+"/"+req.cookies.username+"/build"),os.tmpDir()+"/jar_"+req.body.executionID);149                                                deleteDir(os.tmpDir()+"/jar_"+req.body.executionID);150                                                delete executions[executionID];151                                                return;152                                            }153                                            if(executions[executionID].template){154                                                updateExecution({_id:executionID},{$set:{cloudStatus:"Virtual Machines have been provisioned."}},false);155                                            }156                                            executions[executionID].machines = machines.concat(cloudMachines);157                                            getGlobalVars(executionID,function(){158                                                testcases.forEach(function(testcase){159                                                    executions[executionID].testcases[testcase.testcaseID] = testcase;160                                                });161                                                //see if there is a base state162                                                suiteBaseState(executionID,executions[executionID].machines,function(){163                                                    //magic happens here164                                                    applyMultiThreading(executionID,function(){165                                                        updateExecution({_id:executionID},{$set:{status:"Running",lastRunDate:new Date()}},false,function(){166                                                            executeTestCases(executions[executionID].testcases,executionID);167                                                        });168                                                    })169                                                });170                                            });171                                        });172                                    });173                                });174                            });175                        });176                    });177                });178            });179        }180    });181};182function zipPythonFiles(projectDir,destDir,callback){183    fs.exists(projectDir + "/PythonWorkDir",function(exists){184        if(exists == true){185            git.lsFiles(projectDir + "/src/",["*.py"],function(data){186                if ((data != "")&&(data.indexOf("\n") != -1)){187                    zipDir(projectDir + "/PythonWorkDir/Lib",destDir+"/pythonLibs.zip",['**','!**.pyc','!**/*.pyc'],function(){188                        zipDir(projectDir + "/src/",destDir+"/pythonSources.zip",['**/*.py','**.py'],function(){189                            callback();190                        });191                    })192                }193                else{194                    callback();195                }196            });197        }198        else{199            callback()200        }201    });202}203function zipDir(sourceDir,targetFile,pattern,callback){204    fs.exists(sourceDir,function(exists){205        if(exists == true){206            var output = fs.createWriteStream(targetFile);207            /*208            var archive = archiver('tar', {209                gzip: true,210                gzipOptions: {211                    level: 1212                }213            });214            */215            var archive = archiver('zip');216            output.on('close', function () {217                output.end();218                callback();219                console.log(archive.pointer() + ' total bytes');220                console.log('archiver has been finalized and the output file descriptor has closed.');221            });222            archive.on('error', function(err){223                console.log(err);224                //throw err;225            });226            archive.pipe(output);227            archive.bulk([228                { expand: true, cwd: sourceDir, src: pattern}229            ]);230            archive.finalize();231        }232        else{233            callback()234        }235    })236}237exports.compileBuild = function(project,username,callback){compileBuild(project,username,callback)};238function compileBuild(project,username,callback){239    var workDir = rootDir+project+"/"+username;240    var msg = {project:project,username:username,java:true,python:true,csharp:true};241    var compileScripts = function(){242        var compileOut = "";243        //random id for compile proc244        var id;245        for (var i = 0; i < 24; i++) {246            id += Math.floor(Math.random() * 10).toString(16);247        }248        compile.operation(msg,id,function(data){compileOut = compileOut + data},function(){249            if (compileOut.indexOf("BUILD FAILED") != -1){250                callback("unable to compile")251            }252            else{253                callback(null)254            }255        })256    };257    needToCompileJava(workDir,project,function(compileJava){258        msg.java = compileJava;259        needToCompilePython(workDir,project,username,function(compilePython){260            msg.python = compilePython;261            needToCompileCSharp(workDir,project,function(compileCSharp){262                msg.csharp = compileCSharp;263                compileScripts();264            })265        })266    })267}268function needToCompilePython(workDir,project,username,callback){269    var needToCompile = true;270    if(compilations[project+username+"python"]){271        git.commitsSinceDate(workDir,compilations[project+username+"python"],function(data){272            if (data == "0\n"){273                needToCompile = false;274            }275            else{276                compilations[project+username+"python"] = new Date();277            }278            callback(needToCompile)279        });280    }281    else{282        compilations[project+username+"python"] = new Date();283        callback(needToCompile);284    }285}286function needToCompileJava(workDir,project,callback){287    var needToCompile = true;288    fs.exists(workDir+"/build/jar/"+project+".jar", function (exists) {289        if(exists == true){290            fs.stat(workDir+"/build/jar/"+project+".jar",function(err,stats){291                if(err) {292                    callback(needToCompile);293                }294                else{295                    git.commitsSinceDate(workDir,stats.mtime,function(data){296                        if (data == "0\n"){297                            needToCompile = false;298                        }299                        callback(needToCompile)300                    });301                }302            });303        }304        else{305            callback(needToCompile);306        }307    });308}309function needToCompileCSharp(workDir,project,callback){310    var needToCompile = true;311    fs.exists(workDir+"/build/RedwoodHQAutomation.dll", function (exists) {312        if(exists == true){313            fs.stat(workDir+"/build/RedwoodHQAutomation.dll",function(err,stats){314                if(err) {315                    callback(needToCompile);316                }317                else{318                    git.commitsSinceDate(workDir,stats.mtime,function(data){319                        if (data == "0\n"){320                            needToCompile = false;321                        }322                        callback(needToCompile)323                    });324                }325            });326        }327        else{328            callback(needToCompile);329        }330    });331}332function applyMultiThreading(executionID,callback){333    var count = 0;334    var mainMachinesCount = executions[executionID].machines.length;335    executions[executionID].machines.forEach(function(machine){336        db.collection('machines', function(err, collection) {337            collection.findOne({_id:db.bson_serializer.ObjectID(machine._id)}, {}, function(err, dbMachine) {338                var startThread = 0;339                if(dbMachine){340                    startThread = dbMachine.takenThreads - machine.threads;341                }342                if(startThread != 0){343                    startThread = dbMachine.lastStartThread + 1344                }345                collection.findAndModify({_id:db.bson_serializer.ObjectID(machine._id)},{},{$set:{lastStartThread:startThread}},{safe:true,new:true},function(err,data){346                });347                common.logger.info("staring at:"+startThread);348                machine.threadID = startThread;349                //if more than one thread run base state if not let it go350                if (machine.threads > 1){351                    machine.multiThreaded = true;352                    agentBaseState(executions[executionID].project+"/"+executions[executionID].username,executionID,machine.host,machine.port,machine.threadID,function(err){353                        machine.runBaseState = true;354                        var newMachineCount = 1;355                        for(var i=machine.threads;i>1;i--){356                            var newMachine = {};357                            for (var key in machine) {358                                newMachine[key] = machine[key];359                            }360                            newMachine.threadID = i+startThread-1;361                            common.logger.info(newMachine);362                            executions[executionID].machines.push(newMachine);363                            sendAgentCommand(newMachine.host,newMachine.port,{command:"start launcher",executionID:executionID,threadID:newMachine.threadID},3,function(err){364                                newMachineCount++;365                                if (newMachineCount == machine.threads){366                                    count++;367                                    if(count == mainMachinesCount){368                                        callback();369                                    }370                                }371                            });372                        }373                    });374                }375                else{376                    count++;377                    if(count == mainMachinesCount){378                        callback();379                    }380                }381            });382        });383    });384}385function suiteBaseState(executionID,machines,callback){386    var count = 0;387    var foundSuiteState = false;388    var suiteBaseTCs = [];389    var lastMachine = function(){390        if (count === machines.length){391            if (suiteBaseTCs.length > 0){392                executions[executionID].cachedTCs = executions[executionID].testcases;393                executions[executionID].testcases = {};394                suiteBaseTCs.forEach(function(tc){395                    executions[executionID].testcases[tc.testcaseID] = tc;396                });397                executions[executionID].baseStateFailed = false;398            }399            callback()400        }401    };402    machines.forEach(function(machine){403        if(machine.baseState){404            foundSuiteState = true;405            machine.roles.push(machine.host);406            db.collection('testcases', function(err, collection) {407                collection.insert({baseState:true,name:machine.host+"_state",status:"Automated",type:"collection",collection:[{order:"1",actionid:machine.baseState,host:machine.host,executionflow:"Record Error Stop Test Case",parameters:[]}]}, {safe:true},function(err,testcaseData){408                    db.collection('executiontestcases', function(err, collection) {409                        //collection.save({_id:machine.resultID},{},{$set:{executionID:executionID,name:machine.host+"_state",status:"Not Run",testcaseID:testcaseData[0]._id.__id,_id: machine.resultID}}, {safe:true,new:true},function(err,returnData){410                        collection.save({baseState:true,executionID:executionID,name:machine.host+"_state",status:"Not Run",testcaseID:testcaseData[0]._id.__id,_id: machine.baseStateTCID},function(err,returnData){411                            suiteBaseTCs.push({testcaseID:testcaseData[0]._id.__id,retryCount:0,_id:machine.baseStateTCID,status:"Not Run",name:machine.host+"_state",type:"collection"});412                            count++;413                            lastMachine();414                        });415                    });416                });417            });418        }419        else{420            count++;421        }422        lastMachine();423    });424}425exports.cacheSourceCode = function(rootPath,callback){cacheSourceCode(rootPath,callback)};426function cacheSourceCode(rootPath,callback){427    git.lsFiles(rootPath,["*.groovy","*.java"],function(data){428        var files = [];429        if ((data != "")&&(data.indexOf("\n") != -1)){430            files = data.split("\n",data.match(/\n/g).length);431        }432        callback(files);433    })434}435function getGlobalVars(executionID,callback){436    db.collection('variables', function(err, collection) {437        collection.find({taskVar:false}, {}, function(err, cursor) {438            cursor.each(function(err, variable) {439                if(variable == null) {440                    callback();441                }442                else{443                    executions[executionID].variables[variable.name] = variable.value;444                }445            });446        })447    });448}449function cleanUpMachines(machines,executionID,callback){450    var count = 0;451    machines.forEach(function(machine){452        sendAgentCommand(machine.host,machine.port,{command:"cleanup",executionID:executionID},3,function(err){453            count++;454            if(count == machines.length){455                if(callback) callback();456            }457        });458    })459}460function executeTestCases(testcases,executionID){461    if (!executions[executionID]) return;462    executions[executionID].executingTCs = true;463    var variables = executions[executionID].variables;464    var machines = executions[executionID].machines;465    var tcArray = [];466    for(var key in testcases){467        tcArray.push(key);468    }469    //execution all done470    //if (tcArray.length == 0){471    var finishExecution = function(callback){472        if (executions[executionID].cachedTCs){473            if(executions[executionID].baseStateFailed === true){474                unlockCloudMachines(executions[executionID].machines);475                unlockMachines(machines,function(){476                    cleanUpMachines(executions[executionID].machines,executionID,function(){477                    });478                    updateExecution({_id:executionID},{$set:{status:"Ready To Run"}},true,function(){479                        executionsRoute.updateExecutionTotals(executionID,function(){480                            if(executions[executionID].sendEmail == true) sendNotification(executionID);481                            //git.deleteFiles(path.join(__dirname, '../public/automationscripts/'+executions[executionID].project+"/"+executions[executionID].username+"/build"),os.tmpDir()+"/jar_"+executionID);482                            deleteDir(os.tmpDir()+"/jar_"+executionID);483                            delete executions[executionID];484                        });485                    });486                    callback(true)487                });488                return;489            }490            executions[executionID].machines.forEach(function(machine){491                machine.baseState = null;492            });493            executions[executionID].testcases = executions[executionID].cachedTCs;494            delete executions[executionID].cachedTCs;495            tcArray = [];496            for(key in executions[executionID].testcases){497                tcArray.push(key);498            }499            testcases = executions[executionID].testcases;500            callback(false)501        }502        else{503            if(executions[executionID]){504                unlockCloudMachines(executions[executionID].machines);505                unlockMachines(executions[executionID].machines,function(){506                    cleanUpMachines(executions[executionID].machines,executionID,function(){507                    });508                    updateExecution({_id:executionID},{$set:{status:"Ready To Run"}},true,function(){509                        executionsRoute.updateExecutionTotals(executionID,function(){510                            if(executions[executionID].sendEmail == true) sendNotification(executionID);511                            //git.deleteFiles(path.join(__dirname, '../public/automationscripts/'+executions[executionID].project+"/"+executions[executionID].username+"/build"),os.tmpDir()+"/jar_"+executionID);512                            deleteDir(os.tmpDir()+"/jar_"+executionID);513                            delete executions[executionID];514                        });515                    });516                    callback(true)517                });518            }519            //return;520        }...Using AI Code Generation
1var redwoodHQ = require('redwoodhq');2redwoodHQ.cleanUpMachines();3var redwoodHQ = require('redwoodhq');4redwoodHQ.cleanUpMachines();5var redwoodHQ = require('redwoodhq');6redwoodHQ.cleanUpMachines();7var redwoodHQ = require('redwoodhq');8redwoodHQ.cleanUpMachines();9var redwoodHQ = require('redwoodhq');10redwoodHQ.cleanUpMachines();11var redwoodHQ = require('redwoodhq');12redwoodHQ.cleanUpMachines();13var redwoodHQ = require('redwoodhq');14redwoodHQ.cleanUpMachines();15var redwoodHQ = require('redwoodhq');16redwoodHQ.cleanUpMachines();17var redwoodHQ = require('redwoodhq');18redwoodHQ.cleanUpMachines();19var redwoodHQ = require('redwoodhq');20redwoodHQ.cleanUpMachines();21var redwoodHQ = require('redwoodhq');22redwoodHQ.cleanUpMachines();23var redwoodHQ = require('redwoodhq');24redwoodHQ.cleanUpMachines();25var redwoodHQ = require('redwoodhq');26redwoodHQ.cleanUpMachines();Using AI Code Generation
1var redwood = require('redwoodHQ');2redwood.cleanUpMachines();3var redwood = require('redwoodHQ');4redwood.cleanUpMachines();5var redwood = require('redwoodHQ');6redwood.cleanUpMachines();7var redwood = require('redwoodHQ');8redwood.cleanUpMachines();9var redwood = require('redwoodHQ');10redwood.cleanUpMachines();11var redwood = require('redwoodHQ');12redwood.cleanUpMachines();13var redwood = require('redwoodHQ');14redwood.cleanUpMachines();15var redwood = require('redwoodHQ');16redwood.cleanUpMachines();17var redwood = require('redwoodHQ');18redwood.cleanUpMachines();19var redwood = require('redwoodHQ');20redwood.cleanUpMachines();21var redwood = require('redwoodHQ');22redwood.cleanUpMachines();23var redwood = require('redwoodHQ');24redwood.cleanUpMachines();25var redwood = require('redwoodHQ');26redwood.cleanUpMachines();27var redwood = require('redwoodHQ');Using AI Code Generation
1var redwood = require('redwood');2var machines = ['machine1', 'machine2'];3redwood.cleanUpMachines(machines, function(err, result) {4    if (err) {5        console.log(err);6    }7    console.log(result);8});9var redwood = require('redwood');10var machines = ['machine1', 'machine2'];11redwood.cleanUpMachines(machines, function(err, result) {12    if (err) {13        console.log(err);14    }15    console.log(result);16});17var redwood = require('redwood');18var machines = ['machine1', 'machine2'];19redwood.cleanUpMachines(machines, function(err, result) {20    if (err) {21        console.log(err);22    }23    console.log(result);24});25var redwood = require('redwood');26var machines = ['machine1', 'machine2'];27redwood.cleanUpMachines(machines, function(err, result) {28    if (err) {29        console.log(err);30    }31    console.log(result);32});33var redwood = require('redwood');34var machines = ['machine1', 'machine2'];35redwood.cleanUpMachines(machines, function(err, result) {36    if (err) {37        console.log(err);38    }39    console.log(result);40});41var redwood = require('redwood');42var machines = ['machine1', 'machine2'];43redwood.cleanUpMachines(machines, function(err, result) {44    if (err) {45        console.log(err);46    }47    console.log(result);48});49var redwood = require('redwood');50var machines = ['machine1', 'machine2'];51redwood.cleanUpMachines(machines, function(err, result) {52    if (err) {53        console.log(err);54    }55    console.log(result);56});57var redwood = require('redwood');58var machines = ['machine1', 'machine2'];Using AI Code Generation
1var redwood = require('redwood');2redwood.cleanUpMachines(function(err, result) {3  if (err) {4    console.log('error cleaning up machines', err);5  } else {6    console.log('cleaned up machines', result);7  }8});9var redwood = require('redwood');10redwood.setMachine('test-machine', function(err, result) {11  if (err) {12    console.log('error setting machine name', err);13  } else {14    console.log('machine name set', result);15  }16});17var redwood = require('redwood');18redwood.getMachine(function(err, result) {19  if (err) {20    console.log('error getting machine name', err);21  } else {22    console.log('machine name', result);23  }24});25var redwood = require('redwood');26redwood.getMachine(function(err, result) {27  if (err) {28    console.log('error getting machine name', err);29  } else {30    console.log('machine name', result);31  }32});33var redwood = require('redwood');34redwood.getMachine(function(err, result) {35  if (err) {36    console.log('error getting machine name', err);37  } else {38    console.log('machine name', result);39  }40});41var redwood = require('redwood');42redwood.getMachine(function(err, result) {Using AI Code Generation
1var redwood = require('redwood');2redwood.cleanUpMachines(function(err, result) {3    console.log('cleanUpMachines: ', err, result);4});5var redwood = require('redwood');6redwood.cleanUpMachines(function(err, result) {7    console.log('cleanUpMachines: ', err, result);8});9var redwood = require('redwood');10redwood.cleanUpMachines(function(err, result) {11    console.log('cleanUpMachines: ', err, result);12});13var redwood = require('redwood');14redwood.cleanUpMachines(function(err, result) {15    console.log('cleanUpMachines: ', err, result);16});17var redwood = require('redwood');18redwood.cleanUpMachines(function(err, result) {19    console.log('cleanUpMachines: ', err, result);20});21var redwood = require('redwood');22redwood.cleanUpMachines(function(err, result) {23    console.log('cleanUpMachines: ', err, result);24});25var redwood = require('redwood');26redwood.cleanUpMachines(function(err, result) {27    console.log('cleanUpMachines: ', err, result);28});29var redwood = require('redwood');30redwood.cleanUpMachines(function(err, result) {31    console.log('cleanUpMachines: ', err, result);32});33var redwood = require('redwood');34redwood.cleanUpMachines(function(err, result) {35    console.log('cleanUpMachines: ', err, result);36});37var redwood = require('redwood');38redwood.cleanUpMachines(function(err, result) {39    console.log('cleanUpMachines: ', err, result);40});41var redwood = require('redwoodUsing AI Code Generation
1var redwood = require('redwood');2var config = require('redwood-config');3redwood.init(config, function() {4    redwood.cleanUpMachines(function(err) {5        console.log('cleaup done');6    });7});8var config = require('./redwood-config.json');9var config = require('./redwood-config.json');10var config = require('./redwood-config.json');Using AI Code Generation
1var redwoodHQ = require('redwoodhq');2redwoodHQ.cleanUpMachines("MyMachineGroup");3var redwoodHQ = require('redwoodhq');4redwoodHQ.cleanUpMachines("MyMachineGroup", "MyMachine");5var redwoodHQ = require('redwoodhq');6redwoodHQ.cleanUpMachines("MyMachineGroup", "MyMachine", "MyMachine");7var redwoodHQ = require('redwoodhq');8redwoodHQ.cleanUpMachines("MyMachineGroup", "MyMachine", "MyMachine", "MyMachine");9var redwoodHQ = require('redwoodhq');10redwoodHQ.cleanUpMachines("MyMachineGroup", "MyMachine", "MyMachine", "MyMachine", "MyMachine");11var redwoodHQ = require('redwoodhq');12redwoodHQ.cleanUpMachines("MyMachineGroup", "MyMachine", "MyMachine", "MyMachine", "MyMachine", "MyMachine");13var redwoodHQ = require('redwoodhq');14redwoodHQ.cleanUpMachines("MyMachineGroup", "MyMachine", "MyMachine", "MyMachine", "MyMachine", "MyMachine", "MyMachine");15var redwoodHQ = require('redwoodhq');16redwoodHQ.cleanUpMachines("MyMachineGroup", "MyMachine", "MyMachine", "MyMachine", "MyMachine", "MyMachine", "MyMachine", "MyMachine");Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
