Newer
Older
restart-li-session / restart_session.js
/**
 * @prettier
 */
"use strict";

/* Module Require */
const child_process = require("child_process"),
  path = require("path");

const exec = function (cmdLine, opts) {
    console.log(cmdLine, typeof opts !== "undefined" ? opts : "");
    console.log(child_process.execSync(cmdLine, opts).toString());
  },
  getFiles = function (module_name, dir) {
    return child_process
      .execSync(`find ${path.join(process.env.LI_HOME, module_name, dir, session_name)} -name "*json"`)
      .toString()
      .split("\n")
      .slice(0, -1)
      .map(function (item) {
        return item.split(session_name)[1];
      });
  },
  buildArgs = function (args) {
    let result = {};
    for (let i = 0; i < args.length; i += 2) {
      if (args[i].substring(0, 2) === "--") result[args[i].substring(2)] = args[i + 1];
    }
    return result;
  };

let myArgs = buildArgs(process.argv.slice(2)),
  modules = typeof myArgs.modules !== undefined ? myArgs.modules.split(",") : [],
  session_name = myArgs.session,
  i = 0;

if (modules.length <= 0) {
  console.log("use --modules <modules>\twhere modules is like module1,module2,...");
  process.exit(0);
}
if (typeof session_name === "undefined" || session_name === "") {
  console.log("use --session <session>\twhere session is like XXX_XXXX-XX-XX_XXX");
  process.exit(0);
}

while (i < modules.length) {
  let files = getFiles(modules[i], "in");
  exec(`./run --standalone ${session_name}`, {
    cwd: `${path.join(process.env.LI_HOME, modules[i])}`
  });
  let filesStillIn = getFiles(modules[i], "in"),
    filesProceed = files.filter(function (x) {
      return !filesStillIn.includes(x);
    });
  if (i < modules.length - 1)
    for (let j = 0; j < filesProceed.length; j++) {
      let a = path.join(process.env.LI_HOME, modules[i], "out", session_name, filesProceed[j]),
        b = path.join(process.env.LI_HOME, modules[i + 1], "in", session_name, filesProceed[j]);
      exec(`cp ${a} ${b}`);
    }
  if (filesStillIn.length === 0) i++;
  else console.log("restart " + modules[i]);
}