更改功能执行顺序会导致应用程序失去同步 - node.js

时间:2018-03-04 09:42:37

标签: node.js

我有这段代码:

function asyncFunction (version, xmlConfig, resolve) {

  var xmlFileName = "/path/to/file/file.xml";
  fileService.generateXmlToFile(xmlFileName, xmlConfig, function(){

    runningService.startApp(appDir, version, function(exitCode){
      if (exitCode == 0) {
        console.log("App " + version + " started");

        multiJobService.runMultipleJobTypes(id, version, allJobsConfig, appDir, function(){
          runningService.stopApp(appDir, version, function(exitCode){
            if (exitCode == 0) {
              console.log("App " + version + " stopped");
              resolve();
            } else {
              console.log("App " + version + " failed to stop. Exit code " + exitCode);
            }
          });
        });
      } else {
        console.log("App " + version + " failed to start. Exit code " + exitCode);
      }

    });

   });
}

它启动我的应用程序的一个版本,运行各种测试,停止应用程序,启动应用程序的下一个版本,再次运行测试然后最终停止应用程序。目前这可以按预期工作。

以下是startApp和stopApp函数:

startApp: function(appDir, version, callback) {

    console.log("App " + version + " starting...");

    var child = exec(
              config.env().path.scripts + "/appstart.sh " +
              appDir + " " + 
              version + " " + 
              "127.0.0.1" 
            );

    child.stdout.on('data', function(data) {
      console.log(data);
    });
    child.stderr.on('data', function(data) {
      console.log(data);
    });

    child.on('close', function(code) {
      callback(code);
    });
  },

  stopApp: function(appDir, version, callback) {
    console.log("App " + version + " stopping...")

    var child = exec(
              config.env().path.scripts + "/appstop.sh " +
              appDir + " " + 
              version + " " 
            );

    child.stdout.on('data', function(data) {
      console.log(data);
    });
    child.stderr.on('data', function(data) {
      console.log(data);
    });

    child.on('close', function(code) {
      callback(code);
    });
  }

我想为此添加一些功能。这项工作的第一部分是确保应用程序停止。为此我希望只是调用startApp和stopApp函数的调用顺序。这是我的新代码:

function asyncFunction (version, xmlConfig, resolve) {
  runningService.stopApp(appDir, version, function(exitCode){
    if (exitCode == 0) {
      console.log("App " + version + " stopped");
      resolve();
    } else {
      console.log("App " + version + " failed to stop. Exit code " + exitCode);
    }
  });
  var xmlFileName = "/path/to/file/file.xml";
  fileService.generateXmlToFile(xmlFileName, xmlConfig, function(){

    runningService.startApp(appDir, version, function(exitCode){
      if (exitCode == 0) {
        console.log("App " + version + " started");

        multiJobService.runMultipleJobTypes(id, version, allJobsConfig, appDir, function(){
        });
      } else {
        console.log("App " + version + " failed to start. Exit code " + exitCode);
      }

    });

  });
}

当我运行它时,startApp在stopApp完成之前开始。这会导致startApp失败,因为检测到正在运行的应用程序进程。我该如何解决这个问题?

0 个答案:

没有答案