serverManager.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. const { exec } = require('child_process');
  2. const axios = require('axios');
  3. const fs = require('fs');
  4. const path = require('path');
  5. const constants = require('./constants');
  6. const utils = require('./utils');
  7. let serverProcess;
  8. // 启动 Web API 的函数
  9. const startServer = () => {
  10. return new Promise((resolve, reject) => {
  11. try {
  12. console.log('Starting server...Promise');
  13. const serverExePath = path.join(constants.serverPath, 'server', 'IES.ExamServer.exe');
  14. if (!fs.existsSync(serverExePath)) {
  15. throw new Error(`IES.ExamServer.exe not found at ${serverExePath}`);
  16. }
  17. // 使用双引号包裹路径
  18. const serverCommand = `"${serverExePath}"`;
  19. console.log(`Server command: ${serverCommand}`);
  20. serverProcess = exec(serverCommand, {
  21. cwd: `${constants.serverPath}/server`, // 设置工作目录为 server 目录
  22. stdio: 'inherit'
  23. });
  24. serverProcess.stdout.on('data', (data) => {
  25. console.log(`Server stdout: ${data}`);
  26. });
  27. serverProcess.stderr.on('data', (data) => {
  28. console.log(`Server stderr: ${data}`);
  29. });
  30. serverProcess.on('close', (data) => {
  31. console.log(`Server process exited with code ${data}`);
  32. reject(new Error(`Server process exited with code ${data}`));
  33. });
  34. // 等待 Web API 启动成功
  35. const checkHealth = async () => {
  36. try {
  37. const response = await axios.get(`${constants.baseUrl}/index/health`, {
  38. httpsAgent: constants.agent
  39. });
  40. if (response.status === 200) {
  41. console.log('Server is up and running!');
  42. resolve();
  43. }
  44. } catch (error) {
  45. console.log('Waiting for server to start...', error);
  46. setTimeout(checkHealth, 1000); // 每隔 1 秒检查一次
  47. }
  48. };
  49. checkHealth();
  50. } catch (error)
  51. {
  52. console.error('Error starting server:', error);
  53. reject(error);
  54. }
  55. });
  56. };
  57. // 检查服务器健康状态的函数
  58. const checkServerHealth = async () => {
  59. try {
  60. const response = await axios.get(`${constants.baseUrl}/index/health`, {
  61. httpsAgent: constants.agent
  62. });
  63. if (response.status === 200) {
  64. console.log('Server is up and running!');
  65. return true;
  66. }
  67. } catch (error) {
  68. console.log('Server is not running yet.', error);
  69. return false;
  70. }
  71. };
  72. // 关闭服务器的函数
  73. const shutdownServer = async () => {
  74. try {
  75. console.log('index/shutdown api ...');
  76. const response = await axios.get(`${constants.baseUrl}/index/shutdown?delay=0`, {
  77. httpsAgent: constants.agent
  78. });
  79. if (response.status === 200) {
  80. console.log('Server is shutdown!');
  81. }
  82. } catch (error) {
  83. console.error('关闭.NET Core Web API 时出错:', error);
  84. }
  85. if (serverProcess) {
  86. console.log('Killing server process...');
  87. serverProcess.kill();
  88. }
  89. };
  90. module.exports = {
  91. startServer,
  92. checkServerHealth,
  93. shutdownServer
  94. };