123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- const { exec } = require('child_process');
- const axios = require('axios');
- const fs = require('fs');
- const path = require('path');
- const constants = require('./constants');
- const utils = require('./utils');
- let serverProcess;
- // 启动 Web API 的函数
- const startServer = () => {
- return new Promise((resolve, reject) => {
- try {
- console.log('Starting server...Promise');
- const serverExePath = path.join(constants.serverPath, 'server', 'IES.ExamServer.exe');
- if (!fs.existsSync(serverExePath)) {
- throw new Error(`IES.ExamServer.exe not found at ${serverExePath}`);
- }
- // 使用双引号包裹路径
- const serverCommand = `"${serverExePath}"`;
- console.log(`Server command: ${serverCommand}`);
- serverProcess = exec(serverCommand, {
- cwd: `${constants.serverPath}/server`, // 设置工作目录为 server 目录
- stdio: 'inherit'
- });
- serverProcess.stdout.on('data', (data) => {
- console.log(`Server stdout: ${data}`);
- });
- serverProcess.stderr.on('data', (data) => {
- console.log(`Server stderr: ${data}`);
- });
- serverProcess.on('close', (data) => {
- console.log(`Server process exited with code ${data}`);
- reject(new Error(`Server process exited with code ${data}`));
- });
- // 等待 Web API 启动成功
- const checkHealth = async () => {
- try {
- const response = await axios.get(`${constants.baseUrl}/index/health`, {
- httpsAgent: constants.agent,
- timeout: 5000 // 设置超时时间为 5 秒
- });
- if (response.status === 200) {
- console.log('Server is up and running!');
- resolve();
- }
- } catch (error) {
- console.log('Waiting for server to start...', error);
- setTimeout(checkHealth, 10000); // 每隔 10 秒检查一次
- }
- };
- checkHealth();
- } catch (error)
- {
- console.error('Error starting server:', error);
- reject(error);
- }
- });
- };
- // 检查服务器健康状态的函数
- const checkServerHealth = async () => {
- try {
-
- const response = await axios.get(`${constants.baseUrl}/index/health`, {
- httpsAgent: constants.agent
- });
- if (response.status === 200) {
- console.log('Server is up and running!');
- return true;
- }
- } catch (error) {
- console.log('Server is not running yet.', error);
- return false;
- }
- };
- // 关闭服务器的函数
- const shutdownServer = async () => {
-
- try {
- console.log('index/shutdown api ...');
- const response = await axios.get(`${constants.baseUrl}/index/shutdown?delay=0`, {
- httpsAgent: constants.agent,
- validateStatus: (status) => status === 200 || status === 404, // 允许 404 状态码
- timeout: 5000 // 设置超时时间为 5 秒
- });
- if (response.status === 404) {
- console.error('API: index/shutdown not found.');
- }
- if (response.status === 200) {
- console.log('Server is shutdown!');
- }
- } catch (error) {
- console.error('关闭.NET Core Web API 时出错:', error);
- }
- if (serverProcess) {
- console.log('Killing server process...');
- serverProcess.kill();
- }
- };
- module.exports = {
- startServer,
- checkServerHealth,
- shutdownServer
- };
|