ai-project-manage-cli 6.0.98 → 6.0.99

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +80 -36
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -3758,7 +3758,13 @@ function createRunSlotPool(maxConcurrent = DEFAULT_MAX_CONCURRENT) {
3758
3758
  }
3759
3759
 
3760
3760
  // src/commands/connect-lock.ts
3761
- import { existsSync as existsSync13, mkdirSync as mkdirSync6, readFileSync as readFileSync12, unlinkSync, writeFileSync as writeFileSync12 } from "fs";
3761
+ import {
3762
+ existsSync as existsSync13,
3763
+ mkdirSync as mkdirSync6,
3764
+ readFileSync as readFileSync12,
3765
+ unlinkSync,
3766
+ writeFileSync as writeFileSync12
3767
+ } from "fs";
3762
3768
  import { join as join14 } from "path";
3763
3769
  var CONNECT_LOCK_PATH = join14(APM_CONFIG_DIR, "connect.lock");
3764
3770
  function isProcessAlive(pid) {
@@ -3770,6 +3776,22 @@ function isProcessAlive(pid) {
3770
3776
  return false;
3771
3777
  }
3772
3778
  }
3779
+ function sleepSync(ms) {
3780
+ const deadline = Date.now() + ms;
3781
+ while (Date.now() < deadline) {
3782
+ }
3783
+ }
3784
+ function waitForPm2LockHandoff(timeoutMs = 5e3) {
3785
+ const deadline = Date.now() + timeoutMs;
3786
+ while (Date.now() < deadline) {
3787
+ pruneStaleConnectLock();
3788
+ const lock = readConnectLock();
3789
+ if (!lock || lock.mode !== "pm2" || lock.pid === process.pid || !isProcessAlive(lock.pid)) {
3790
+ return;
3791
+ }
3792
+ sleepSync(100);
3793
+ }
3794
+ }
3773
3795
  function readConnectLock() {
3774
3796
  if (!existsSync13(CONNECT_LOCK_PATH)) return null;
3775
3797
  try {
@@ -3796,10 +3818,17 @@ function acquireConnectLock(mode) {
3796
3818
  pruneStaleConnectLock();
3797
3819
  const existing = readConnectLock();
3798
3820
  if (existing && isProcessAlive(existing.pid)) {
3799
- console.error(
3800
- `[apm] \u5DF2\u6709 apm connect \u5728\u8FD0\u884C (pid=${existing.pid}, mode=${existing.mode})`
3801
- );
3802
- process.exit(1);
3821
+ if (existing.pid === process.pid) {
3822
+ return;
3823
+ }
3824
+ if (mode === "pm2" && existing.mode === "pm2") {
3825
+ forceReleaseConnectLock();
3826
+ } else {
3827
+ console.error(
3828
+ `[apm] \u5DF2\u6709 apm connect \u5728\u8FD0\u884C (pid=${existing.pid}, mode=${existing.mode})`
3829
+ );
3830
+ process.exit(1);
3831
+ }
3803
3832
  }
3804
3833
  mkdirSync6(APM_CONFIG_DIR, { recursive: true });
3805
3834
  const lock = {
@@ -3807,7 +3836,11 @@ function acquireConnectLock(mode) {
3807
3836
  mode,
3808
3837
  startedAt: (/* @__PURE__ */ new Date()).toISOString()
3809
3838
  };
3810
- writeFileSync12(CONNECT_LOCK_PATH, JSON.stringify(lock, null, 2) + "\n", "utf8");
3839
+ writeFileSync12(
3840
+ CONNECT_LOCK_PATH,
3841
+ JSON.stringify(lock, null, 2) + "\n",
3842
+ "utf8"
3843
+ );
3811
3844
  }
3812
3845
  function releaseConnectLock() {
3813
3846
  const lock = readConnectLock();
@@ -3855,24 +3888,28 @@ function buildConnectPm2Ecosystem(options) {
3855
3888
  if (baseUrl) {
3856
3889
  env.AI_PM_SERVER = baseUrl;
3857
3890
  }
3891
+ const app = {
3892
+ name: PM2_APP_NAME,
3893
+ script: options.apmScript,
3894
+ interpreter: options.nodePath,
3895
+ args: options.connectArgs,
3896
+ autorestart: true,
3897
+ min_uptime: "10s",
3898
+ max_restarts: 10,
3899
+ restart_delay: 3e3,
3900
+ exp_backoff_restart_delay: 1e3,
3901
+ max_memory_restart: "1G",
3902
+ kill_timeout: 5e3,
3903
+ shutdown_with_message: true,
3904
+ instances: 1,
3905
+ exec_mode: "fork",
3906
+ env
3907
+ };
3908
+ if (process.platform === "win32") {
3909
+ app.windowsHide = true;
3910
+ }
3858
3911
  return {
3859
- apps: [
3860
- {
3861
- name: PM2_APP_NAME,
3862
- script: options.apmScript,
3863
- interpreter: options.nodePath,
3864
- args: options.connectArgs,
3865
- autorestart: true,
3866
- min_uptime: "10s",
3867
- max_restarts: 0,
3868
- restart_delay: 3e3,
3869
- exp_backoff_restart_delay: 1e3,
3870
- max_memory_restart: "1G",
3871
- kill_timeout: 5e3,
3872
- shutdown_with_message: true,
3873
- env
3874
- }
3875
- ]
3912
+ apps: [app]
3876
3913
  };
3877
3914
  }
3878
3915
  function formatConnectPm2EcosystemFile(ecosystem) {
@@ -4076,7 +4113,8 @@ function deletePm2AppIfExists(name) {
4076
4113
  spawnSync2(pm2Bin, ["delete", name], {
4077
4114
  encoding: "utf8",
4078
4115
  shell: useNpmShell2,
4079
- stdio: "ignore"
4116
+ stdio: "ignore",
4117
+ windowsHide: useNpmShell2
4080
4118
  });
4081
4119
  }
4082
4120
  function removePm2ConnectProcesses() {
@@ -4098,7 +4136,8 @@ function runPm2(args, options) {
4098
4136
  const spawnOptions = {
4099
4137
  encoding: "utf8",
4100
4138
  stdio: options?.inherit ? "inherit" : ["ignore", "pipe", "pipe"],
4101
- shell: useNpmShell2
4139
+ shell: useNpmShell2,
4140
+ windowsHide: useNpmShell2
4102
4141
  };
4103
4142
  const result = spawnSync2(pm2Bin, args, spawnOptions);
4104
4143
  if (result.error) {
@@ -4122,7 +4161,8 @@ function runPm2Json(args) {
4122
4161
  const result = spawnSync2(pm2Bin, args, {
4123
4162
  encoding: "utf8",
4124
4163
  stdio: ["ignore", "pipe", "pipe"],
4125
- shell: useNpmShell2
4164
+ shell: useNpmShell2,
4165
+ windowsHide: useNpmShell2
4126
4166
  });
4127
4167
  if (result.status !== 0) return "[]";
4128
4168
  return result.stdout?.toString() ?? "[]";
@@ -4140,13 +4180,13 @@ function printConnectNotRunningHint() {
4140
4180
  function assertConnectNotRunning() {
4141
4181
  pruneStaleConnectLock();
4142
4182
  const lock = readConnectLock();
4143
- if (lock && isProcessAlive(lock.pid)) {
4183
+ if (lock && isProcessAlive(lock.pid) && lock.pid !== process.pid) {
4144
4184
  console.error(
4145
4185
  `[apm] \u5DF2\u6709 apm connect \u5728\u8FD0\u884C (pid=${lock.pid}, mode=${lock.mode})\uFF0C\u8BF7\u5148\u505C\u6B62\u540E\u518D\u542F\u52A8`
4146
4186
  );
4147
4187
  process.exit(1);
4148
4188
  }
4149
- if (isPm2ConnectOnline()) {
4189
+ if (!isRunningUnderPm2() && isPm2ConnectOnline()) {
4150
4190
  console.error(
4151
4191
  "[apm] \u5DF2\u6709 apm connect \u5B88\u62A4\u8FDB\u7A0B\u5728\u8FD0\u884C\uFF0C\u8BF7\u5148\u6267\u884C apm daemon stop"
4152
4192
  );
@@ -4177,10 +4217,8 @@ async function handleConnectAfterUpdate(options) {
4177
4217
  console.log("[apm] \u66F4\u65B0\u5B8C\u6210\uFF0C\u6B63\u5728\u4EE5\u65B0\u7248\u672C\u91CD\u65B0\u8FDE\u63A5\u2026");
4178
4218
  await prepareEcosystem(options.server);
4179
4219
  if (isRunningUnderPm2()) {
4180
- runPm2(["startOrRestart", PM2_ECOSYSTEM_PATH, "--update-env"], {
4181
- inherit: true
4182
- });
4183
- console.log("[apm] \u5DF2\u901A\u77E5 PM2 \u4F7F\u7528\u65B0\u7248\u672C\u91CD\u542F connect");
4220
+ runPm2(["reload", PM2_ECOSYSTEM_PATH, "--update-env"], { inherit: true });
4221
+ console.log("[apm] \u5DF2\u901A\u77E5 PM2 \u4F7F\u7528\u65B0\u7248\u672C\u91CD\u65B0\u52A0\u8F7D connect");
4184
4222
  process.exit(0);
4185
4223
  }
4186
4224
  reexecConnectWithResolvedPath(options);
@@ -4684,9 +4722,12 @@ function connectOnce(url, ctx, connectionAbort, onConnected) {
4684
4722
  });
4685
4723
  }
4686
4724
  async function runConnect(options) {
4687
- const { didUpdate } = await runUpdate();
4688
- if (didUpdate) {
4689
- await handleConnectAfterUpdate(options);
4725
+ const underPm2 = isRunningUnderPm2();
4726
+ if (!underPm2) {
4727
+ const { didUpdate } = await runUpdate();
4728
+ if (didUpdate) {
4729
+ await handleConnectAfterUpdate(options);
4730
+ }
4690
4731
  }
4691
4732
  const cfg = await ensureLoggedConfig();
4692
4733
  if (options.server?.trim()) {
@@ -4698,7 +4739,10 @@ async function runConnect(options) {
4698
4739
  process.exit(1);
4699
4740
  }
4700
4741
  assertConnectNotRunning();
4701
- const lockMode = isRunningUnderPm2() ? "pm2" : "foreground";
4742
+ if (underPm2) {
4743
+ waitForPm2LockHandoff();
4744
+ }
4745
+ const lockMode = underPm2 ? "pm2" : "foreground";
4702
4746
  acquireConnectLock(lockMode);
4703
4747
  process.on("exit", releaseConnectLock);
4704
4748
  let shuttingDown = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai-project-manage-cli",
3
- "version": "6.0.98",
3
+ "version": "6.0.99",
4
4
  "description": "命令行工具:后续用于调用平台后端 API 完成运维与自动化操作",
5
5
  "type": "module",
6
6
  "private": false,