modelstat 0.0.30 → 0.0.31
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.
- package/dist/cli.mjs +8 -4
- package/dist/cli.mjs.map +1 -1
- package/package.json +1 -1
- package/scripts/postinstall.mjs +51 -1
package/package.json
CHANGED
package/scripts/postinstall.mjs
CHANGED
|
@@ -167,12 +167,20 @@ async function rebootServiceIfInstalled() {
|
|
|
167
167
|
const { spawnSync } = await import("node:child_process");
|
|
168
168
|
console.log("[modelstat] refreshing background service…");
|
|
169
169
|
|
|
170
|
-
// Stop
|
|
170
|
+
// Stop the SERVICE (launchctl bootout / systemctl --user disable)
|
|
171
|
+
// via the fresh bundle's own knowledge of each platform.
|
|
171
172
|
spawnSync(process.execPath, [freshBundle, "stop"], {
|
|
172
173
|
stdio: "ignore",
|
|
173
174
|
timeout: 30_000,
|
|
174
175
|
});
|
|
175
176
|
|
|
177
|
+
// Belt-and-braces: kill any stray daemon process that the service
|
|
178
|
+
// supervisor didn't reap (stale lock, killed parent, KeepAlive
|
|
179
|
+
// race during the bundle swap below). Lock file at
|
|
180
|
+
// ~/.modelstat/daemon.lock has the live PID; SIGTERM it gently
|
|
181
|
+
// first, escalate to SIGKILL if it's still around 2 s later.
|
|
182
|
+
await killStaleDaemon(stateDir);
|
|
183
|
+
|
|
176
184
|
// Copy the new bundle over the old `~/.modelstat/bin/modelstat.mjs`
|
|
177
185
|
// so the service supervisor (launchd/systemd) loads the new code on
|
|
178
186
|
// next launch. The fresh bundle's `connect` / `start` does this on
|
|
@@ -201,6 +209,48 @@ async function rebootServiceIfInstalled() {
|
|
|
201
209
|
console.log("[modelstat] ✓ background service restarted with new build");
|
|
202
210
|
}
|
|
203
211
|
|
|
212
|
+
/**
|
|
213
|
+
* Kill any daemon process the service supervisor didn't reap.
|
|
214
|
+
* Reads ~/.modelstat/daemon.lock for the PID, sends SIGTERM, then
|
|
215
|
+
* SIGKILL after a short grace period. Tolerates a missing/stale
|
|
216
|
+
* lock file — we just want to make sure the new bundle is the only
|
|
217
|
+
* thing holding the lock when we restart.
|
|
218
|
+
*/
|
|
219
|
+
async function killStaleDaemon(stateDir) {
|
|
220
|
+
const { readFileSync } = await import("node:fs");
|
|
221
|
+
const lockPath = join(stateDir, "daemon.lock");
|
|
222
|
+
let payload;
|
|
223
|
+
try {
|
|
224
|
+
payload = JSON.parse(readFileSync(lockPath, "utf8"));
|
|
225
|
+
} catch {
|
|
226
|
+
return; // no lock file — nothing to do
|
|
227
|
+
}
|
|
228
|
+
const pid = Number(payload?.pid);
|
|
229
|
+
if (!Number.isInteger(pid) || pid <= 0) return;
|
|
230
|
+
if (pid === process.pid) return; // shouldn't happen, but defensive
|
|
231
|
+
try {
|
|
232
|
+
process.kill(pid, "SIGTERM");
|
|
233
|
+
} catch (err) {
|
|
234
|
+
if (err && err.code === "ESRCH") return; // already gone
|
|
235
|
+
// EPERM: not our process — leave it alone.
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
// Wait up to 2 s for graceful exit.
|
|
239
|
+
for (let i = 0; i < 20; i++) {
|
|
240
|
+
try {
|
|
241
|
+
process.kill(pid, 0); // signal 0 = existence check
|
|
242
|
+
} catch {
|
|
243
|
+
return; // exited
|
|
244
|
+
}
|
|
245
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
246
|
+
}
|
|
247
|
+
try {
|
|
248
|
+
process.kill(pid, "SIGKILL");
|
|
249
|
+
} catch {
|
|
250
|
+
/* gone or denied */
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
204
254
|
main().catch((err) => {
|
|
205
255
|
console.warn(
|
|
206
256
|
`[modelstat] postinstall failed: ${err && err.message ? err.message : err}`,
|