File size: 4,561 Bytes
96e86e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env -S node --import tsx
import { execFile } from "node:child_process";
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
import path from "node:path";
import { setTimeout as delay } from "node:timers/promises";
import { promisify } from "node:util";
import {
  forceKillLocalServiceProcessTree,
  isPidAlive,
  listLocalServiceRegistryRecords,
  removeLocalServiceRegistryRecord,
  terminateLocalService,
} from "../server/src/services/local-service-supervisor.ts";
import { getDevServiceControlFilePath, repoRoot } from "./dev-service-profile.ts";

const execFileAsync = promisify(execFile);

function toDisplayLines(records: Awaited<ReturnType<typeof listLocalServiceRegistryRecords>>) {
  return records.map((record) => {
    const childPid = typeof record.metadata?.childPid === "number" ? ` child=${record.metadata.childPid}` : "";
    const url = typeof record.metadata?.url === "string" ? ` url=${record.metadata.url}` : "";
    return `${record.serviceName} pid=${record.pid}${childPid} cwd=${record.cwd}${url}`;
  });
}

const command = process.argv[2] ?? "list";
const records = await listLocalServiceRegistryRecords({
  profileKind: "paperclip-dev",
  metadata: { repoRoot },
});

function getRecordChildPid(record: (typeof records)[number]) {
  return typeof record.metadata?.childPid === "number" && record.metadata.childPid > 0
    ? record.metadata.childPid
    : null;
}

async function isDevServiceHealthy(port: number | null) {
  if (!port || port <= 0) return false;

  try {
    const response = await fetch(`http://127.0.0.1:${port}/api/health`, {
      signal: AbortSignal.timeout(1_500),
    });
    return response.ok;
  } catch {
    return false;
  }
}

async function findListeningPid(port: number | null) {
  if (!port || port <= 0) return null;

  try {
    const { stdout } = await execFileAsync("netstat", ["-ano", "-p", "tcp"], {
      windowsHide: true,
    });

    const match = stdout
      .split(/\r?\n/)
      .map((line) => line.trim())
      .find((line) => {
        const parts = line.split(/\s+/);
        return (
          parts.length >= 5 &&
          /^tcp$/i.test(parts[0]) &&
          parts[1]?.endsWith(`:${port}`) &&
          /^listening$/i.test(parts[3])
        );
      });

    if (!match) return null;
    const pid = Number.parseInt(match.split(/\s+/).at(-1) ?? "", 10);
    return Number.isInteger(pid) && pid > 0 ? pid : null;
  } catch {
    return null;
  }
}

async function stopRecordGracefullyOnWindows(record: (typeof records)[number]) {
  const controlFilePath = getDevServiceControlFilePath(record.serviceKey);
  const childPid = getRecordChildPid(record);

  mkdirSync(path.dirname(controlFilePath), { recursive: true });
  writeFileSync(
    controlFilePath,
    `${JSON.stringify({ requestedAt: new Date().toISOString(), command: "stop" })}\n`,
    "utf8",
  );

  const deadline = Date.now() + 20_000;
  while (Date.now() < deadline) {
    const wrapperAlive = isPidAlive(record.pid);
    const childAlive = childPid ? isPidAlive(childPid) : false;
    const serviceHealthy = await isDevServiceHealthy(record.port);

    if (!wrapperAlive && !childAlive && !serviceHealthy) {
      rmSync(controlFilePath, { force: true });
      return;
    }
    await delay(200);
  }

  rmSync(controlFilePath, { force: true });
  if (childPid && isPidAlive(childPid)) {
    await forceKillLocalServiceProcessTree({ pid: childPid, processGroupId: null });
  }
  if (isPidAlive(record.pid)) {
    await forceKillLocalServiceProcessTree(record);
  }
  const listeningPid = await findListeningPid(record.port);
  if (listeningPid && isPidAlive(listeningPid)) {
    await forceKillLocalServiceProcessTree({ pid: listeningPid, processGroupId: null });
  }
}

if (command === "list") {
  if (records.length === 0) {
    console.log("No Paperclip dev services registered for this repo.");
    process.exit(0);
  }
  for (const line of toDisplayLines(records)) {
    console.log(line);
  }
  process.exit(0);
}

if (command === "stop") {
  if (records.length === 0) {
    console.log("No Paperclip dev services registered for this repo.");
    process.exit(0);
  }
  for (const record of records) {
    if (process.platform === "win32") {
      await stopRecordGracefullyOnWindows(record);
    } else {
      await terminateLocalService(record);
    }
    await removeLocalServiceRegistryRecord(record.serviceKey);
    console.log(`Stopped ${record.serviceName} (pid ${record.pid})`);
  }
  process.exit(0);
}

console.error(`Unknown dev-service command: ${command}`);
process.exit(1);