e2e-step-process.mjs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { spawn } from "node:child_process";
  2. import process from "node:process";
  3. import { resolveSpawnRuntimeOptions } from "../../../scripts/spawn-command.mjs";
  4. function formatStepError(error) {
  5. return error instanceof Error ? error.message : String(error);
  6. }
  7. function createStepStartFailure(error) {
  8. return { exitCode: 1, error: formatStepError(error) };
  9. }
  10. function normalizeStepExecution(value) {
  11. if (value && typeof value === "object" && "result" in value) {
  12. return {
  13. result: Promise.resolve(value.result),
  14. terminate: typeof value.terminate === "function" ? () => value.terminate() : undefined,
  15. };
  16. }
  17. return { result: Promise.resolve(value), terminate: undefined };
  18. }
  19. function normalizeStepResult(result) {
  20. if (!result || typeof result !== "object") {
  21. return createStepStartFailure(new Error("step returned no result"));
  22. }
  23. return {
  24. exitCode: Number.isInteger(result.exitCode) ? result.exitCode : 1,
  25. ...(result.signal ? { signal: String(result.signal) } : {}),
  26. ...(result.error ? { error: String(result.error) } : {}),
  27. };
  28. }
  29. export function startStepExecution(runStep, step) {
  30. let execution;
  31. try {
  32. execution = normalizeStepExecution(runStep(step));
  33. } catch (error) {
  34. execution = normalizeStepExecution(createStepStartFailure(error));
  35. }
  36. return {
  37. result: execution.result.then(normalizeStepResult, (error) => createStepStartFailure(error)),
  38. terminate: execution.terminate,
  39. };
  40. }
  41. export function spawnCancellableStep(step) {
  42. let child;
  43. try {
  44. child = spawn(step.command, step.args, {
  45. cwd: step.cwd,
  46. env: step.env,
  47. stdio: "inherit",
  48. ...resolveSpawnRuntimeOptions(step.command),
  49. });
  50. } catch (error) {
  51. return {
  52. result: Promise.resolve(createStepStartFailure(error)),
  53. terminate() {},
  54. };
  55. }
  56. let forceKillTimer;
  57. let terminationRequested = false;
  58. const result = new Promise((resolveRun) => {
  59. let settled = false;
  60. const settle = (value) => {
  61. if (settled) return;
  62. settled = true;
  63. if (forceKillTimer) clearTimeout(forceKillTimer);
  64. resolveRun(value);
  65. };
  66. // 修复原因:spawn 的 error 过去会 reject Promise.all,导致其他 shard 未等待、manifest 未写入。
  67. child.once("error", (error) => settle(createStepStartFailure(error)));
  68. child.once("close", (code, signal) => {
  69. settle({ exitCode: code ?? 1, signal: signal ?? undefined });
  70. });
  71. });
  72. return {
  73. result,
  74. terminate() {
  75. if (
  76. terminationRequested ||
  77. !child.pid ||
  78. child.exitCode !== null ||
  79. child.signalCode !== null
  80. ) {
  81. return;
  82. }
  83. terminationRequested = true;
  84. if (process.platform === "win32") {
  85. // pnpm 下还有 WDIO/Electron 等后代进程,Windows 必须按进程树强制终止。
  86. const killChildFallback = () => {
  87. try {
  88. child.kill();
  89. } catch {
  90. // 原进程可能已在 taskkill 启动失败前退出。
  91. }
  92. };
  93. let taskkill;
  94. try {
  95. taskkill = spawn("taskkill", ["/pid", String(child.pid), "/T", "/F"], {
  96. stdio: "ignore",
  97. windowsHide: true,
  98. });
  99. } catch {
  100. killChildFallback();
  101. return;
  102. }
  103. taskkill.once("error", killChildFallback);
  104. taskkill.once("close", (code) => {
  105. if (code !== 0 && child.exitCode === null && child.signalCode === null) {
  106. killChildFallback();
  107. }
  108. });
  109. return;
  110. }
  111. try {
  112. child.kill("SIGTERM");
  113. forceKillTimer = setTimeout(() => {
  114. try {
  115. child.kill("SIGKILL");
  116. } catch {
  117. // SIGTERM 后已经退出时无需继续处理。
  118. }
  119. }, 5_000);
  120. forceKillTimer.unref?.();
  121. } catch {
  122. // close/error 会负责产生最终结果,终止请求本身不应绕过收口。
  123. }
  124. },
  125. };
  126. }