javaServer.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. "use strict";
  2. const _ = require("lodash");
  3. const assert = require("assert");
  4. const fs = require("fs");
  5. const os = require("os");
  6. const path = require("path");
  7. const { execSync } = require("child_process");
  8. const Utils = require("ee-core").Utils;
  9. const ps = require("./ps");
  10. function getCoreDB() {
  11. const Storage = require("ee-core").Storage;
  12. return Storage.JsonDB.connection("system");
  13. }
  14. function getJavaPort() {
  15. const cdb = getCoreDB();
  16. const port = cdb.getItem("config").javaServer.port;
  17. return port;
  18. }
  19. function getJarName() {
  20. const cdb = getCoreDB();
  21. return cdb.getItem("config").javaServer.name;
  22. }
  23. function getOpt(port, path) {
  24. const cdb = getCoreDB();
  25. const opt = cdb.getItem("config").javaServer.opt;
  26. let javaOpt = _.replace(opt, "${port}", port);
  27. return _.replace(javaOpt, "${path}", path);
  28. }
  29. function getJreVersion() {
  30. const cdb = getCoreDB();
  31. return cdb.getItem("config").javaServer.jreVersion;
  32. }
  33. async function start(app) {
  34. const options = app.config.javaServer;
  35. if (!options.enable) {
  36. return;
  37. }
  38. let port = process.env.EE_JAVA_PORT ? parseInt(process.env.EE_JAVA_PORT) : parseInt(getJavaPort());
  39. assert(typeof port === "number", "java port required, and must be a number");
  40. app.logger.info("[javaServer] java server port is:", port);
  41. const jarName = getJarName();
  42. let softwarePath = path.join(Utils.getExtraResourcesDir(), jarName);
  43. app.logger.info("[javaServer] jar存放路径:", softwarePath);
  44. const logPath = Utils.getLogDir()
  45. // 检查程序是否存在
  46. if (!fs.existsSync(softwarePath)) {
  47. app.logger.info("[javaServer] java程序不存在", softwarePath);
  48. }
  49. const JAVA_OPT = getOpt(port, logPath);
  50. if (os.platform() === "win32") {
  51. let jrePath = path.join(
  52. Utils.getExtraResourcesDir(),
  53. getJreVersion(),
  54. "bin",
  55. "javaw.exe"
  56. );
  57. // 命令行字符串 并 执行
  58. let cmdStr = `start ${jrePath} -jar ${JAVA_OPT} ${softwarePath}`;
  59. app.logger.info("[javaServer] cmdStr:", cmdStr);
  60. await execSync(cmdStr);
  61. } else {
  62. // 不受信任请执行: sudo spctl --master-disable
  63. let jrePath = path.join(
  64. Utils.getExtraResourcesDir(),
  65. getJreVersion(),
  66. "Contents",
  67. "Home",
  68. "bin",
  69. "java"
  70. );
  71. // 命令行字符串 并 执行
  72. // let cmdStr = `${jrePath} -jar ${JAVA_OPT} ${softwarePath} > /Users/tangyh/Downloads/app.log`;
  73. let cmdStr = `nohup ${jrePath} -jar ${JAVA_OPT} ${softwarePath} >/dev/null 2>&1 &`;
  74. app.logger.info("[javaServer] cmdStr:", cmdStr);
  75. await execSync(cmdStr);
  76. }
  77. }
  78. async function kill(app) {
  79. const port = getJavaPort();
  80. const jarName = getJarName();
  81. app.logger.info("[javaServer] kill port: ", port);
  82. if (os.platform() === "win32") {
  83. const resultList = ps.lookup({
  84. command: "java",
  85. where: 'caption="javaw.exe"',
  86. arguments: jarName,
  87. });
  88. app.logger.info("[javaServer] resultList:", resultList);
  89. resultList.forEach((item) => {
  90. ps.kill(item.pid, "SIGKILL", (err) => {
  91. if (err) {
  92. throw new Error(err);
  93. }
  94. app.logger.info("[javaServer] 已经退出后台程序: %O", item);
  95. });
  96. });
  97. // const cmd = `for /f "tokens=1-5" %i in ('netstat -ano ^| findstr ":${port}"') do taskkill /F /T /PID %m`;
  98. // const a = await execSync(cmd, {encoding: 'utf-8'});
  99. // app.logger.info("[javaServer] kill:", a);
  100. } else {
  101. const cmd = `ps -ef | grep java | grep ${jarName} | grep -v grep | awk '{print $2}' | xargs kill -9`;
  102. const result = await execSync(cmd);
  103. app.logger.info("[javaServer] kill:", result != null ? result.toString(): '');
  104. }
  105. }
  106. module.exports.start = start;
  107. module.exports.kill = kill;