Control.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. const { exec } = require('child_process');
  2. const path = require('path');
  3. const GLOBAL_DATA = {
  4. gitLock: false,
  5. buildLock: false,
  6. };
  7. /**
  8. * 拉取指定仓库的代码
  9. * @param {string} repoPath 仓库路径
  10. * @returns {Promise<string>} 拉取结果
  11. */
  12. const gitPull = (repoPath) => {
  13. if (GLOBAL_DATA.gitLock) {
  14. return Promise.reject(new Error('git is locked'));
  15. }
  16. GLOBAL_DATA.gitLock = true;
  17. return new Promise((resolve, reject) => {
  18. const cwd = path.resolve(repoPath);
  19. exec("git pull", { cwd }, (error, stdout, stderr) => {
  20. GLOBAL_DATA.gitLock = false;
  21. if (error) {
  22. reject(new Error(stderr) || error);
  23. return;
  24. }
  25. resolve(stdout || "No output");
  26. });
  27. });
  28. }
  29. /**
  30. * 拉当前项目的代码
  31. */
  32. const gitPullCurrent = () => {
  33. const rootPath = path.resolve(__dirname, '../../');
  34. return gitPull(rootPath);
  35. }
  36. /**
  37. * 启动服务
  38. * @param {string} serviceName 服务名称
  39. * @returns {Promise<string>} 启动结果
  40. */
  41. const pm2Start = (serviceName) => {
  42. // if (process.env.NODE_ENV == 'development') {
  43. // return Promise.resolve('development mode, skip');
  44. // }
  45. return new Promise((resolve, reject) => {
  46. exec(`pm2 start ${serviceName}`, (error, stdout, stderr) => {
  47. if (error) {
  48. reject(new Error(stderr) || error);
  49. return;
  50. }
  51. resolve(stdout || "No output");
  52. });
  53. });
  54. }
  55. /**
  56. * 停止服务
  57. * @param {string} serviceName 服务名称
  58. * @returns {Promise<string>} 停止结果
  59. */
  60. const pm2Stop = (serviceName) => {
  61. // if (process.env.NODE_ENV == 'development') {
  62. // return Promise.resolve('development mode, skip');
  63. // }
  64. return new Promise((resolve, reject) => {
  65. exec(`pm2 stop ${serviceName}`, (error, stdout, stderr) => {
  66. if (error) {
  67. reject(new Error(stderr) || error);
  68. return;
  69. }
  70. resolve(stdout || "No output");
  71. });
  72. });
  73. }
  74. /**
  75. * PM2 重启服务
  76. * @param {string} serviceName 服务名称
  77. * @returns {Promise<string>} 重启结果
  78. */
  79. const pm2Restart = (serviceName) => {
  80. // if (process.env.NODE_ENV == 'development') {
  81. // return Promise.resolve('development mode, skip');
  82. // }
  83. return new Promise((resolve, reject) => {
  84. exec(`pm2 restart ${serviceName}`, (error, stdout, stderr) => {
  85. if (error) {
  86. reject(new Error(stderr) || error);
  87. return;
  88. }
  89. resolve(stdout || "No output");
  90. });
  91. });
  92. }
  93. /**
  94. * 清除缓存
  95. */
  96. const clearCache = (cachePath) => {
  97. return new Promise((resolve, reject) => {
  98. exec(`rm -rf ${cachePath}/*`, (error, stdout, stderr) => {
  99. if (error) {
  100. reject(new Error(stderr) || error);
  101. return;
  102. }
  103. resolve(stdout || "No output");
  104. });
  105. });
  106. }
  107. /**
  108. * 清除缓存并重启服务
  109. */
  110. const pm2RestartClearCache = async (serviceName, cachePath) => {
  111. return pm2Stop(serviceName)
  112. .then(() => {
  113. return clearCache(cachePath);
  114. })
  115. .then(() => {
  116. return pm2Start(serviceName);
  117. });
  118. }
  119. /**
  120. * PM2 重启 sporttery 服务
  121. * @param {boolean} hot 是否热重启
  122. * @returns {Promise<string>} 重启结果
  123. */
  124. const pm2RestartSporttery = (hot) => {
  125. if (hot) {
  126. return pm2Restart('sporttery');
  127. }
  128. else {
  129. return pm2RestartClearCache('sporttery', path.resolve(__dirname, '../../server/data'));
  130. }
  131. }
  132. /**
  133. * PM2 重启 pinnacle 服务
  134. * @param {boolean} hot 是否热重启
  135. * @returns {Promise<string>} 重启结果
  136. */
  137. const pm2RestartPinnacle = (hot) => {
  138. if (hot) {
  139. return pm2Restart('pinnacle');
  140. }
  141. else {
  142. return pm2RestartClearCache('pinnacle', path.resolve(__dirname, '../../pinnacle/data'));
  143. }
  144. }
  145. /**
  146. * 发布 web 服务
  147. */
  148. const releaseWeb = () => {
  149. if (GLOBAL_DATA.buildLock) {
  150. return Promise.reject(new Error('build is locked'));
  151. }
  152. GLOBAL_DATA.buildLock = true;
  153. const rootPath = path.resolve(__dirname, '../../web');
  154. return new Promise((resolve, reject) => {
  155. exec('npm run build', { cwd: rootPath }, (error, stdout, stderr) => {
  156. GLOBAL_DATA.buildLock = false;
  157. if (error) {
  158. reject(new Error(stderr) || error);
  159. return;
  160. }
  161. resolve(stdout || "No output");
  162. });
  163. });
  164. }
  165. module.exports = { gitPullCurrent, pm2RestartSporttery, pm2RestartPinnacle, releaseWeb };