clavarium 0.2.532-next.1 → 0.2.535-next.1

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.
@@ -409,8 +409,9 @@ export class Updater {
409
409
  #o; #run; #fetch;
410
410
  constructor({ code = PATHS.code, conf = PATHS.conf, stateDir = PATHS.stateDir, data = PATHS.data, hostTarget = PATHS.host, gitReadTarget = PATHS.gitRead, tunnelTarget = PATHS.tunnel, registry = REGISTRY, run = runCommand, fetcher = fetch, now = () => new Date(), log = (line) => console.log(line),
411
411
  verifySignatures, restart, health, backup, restoreData, installPrivileged, ensureNode, serviceState, answers, restarts, mainPid, pidCwd, diagnose, dropIn = DROP_IN,
412
+ bootId = () => { try { return readFileSync('/proc/sys/kernel/random/boot_id', 'utf8').trim(); } catch { return ''; } },
412
413
  clock = () => Date.now(), sleep = (ms) => new Promise((r) => setTimeout(r, ms)), minFreeMb = 1024, healthMs = 60_000, watchMs = 120_000, settleMs = 10_000, noticeMs = NOTICE_MS, releaseSettleMs = RELEASE_SETTLE_MS } = {}) {
413
- this.#o = { code, conf, stateDir, data, hostTarget, gitReadTarget, tunnelTarget, registry: registry.replace(/\/$/, ''), now, log, minFreeMb, healthMs, watchMs, settleMs, noticeMs, releaseSettleMs, clock, sleep, dropIn };
414
+ this.#o = { code, conf, stateDir, data, hostTarget, gitReadTarget, tunnelTarget, registry: registry.replace(/\/$/, ''), now, log, bootId, minFreeMb, healthMs, watchMs, settleMs, noticeMs, releaseSettleMs, clock, sleep, dropIn };
414
415
  this.#run = run; this.#fetch = fetcher;
415
416
  this.verifySignatures = verifySignatures ?? ((dir) => this.#npm(['audit', 'signatures'], { cwd: dir, timeoutMs: 300_000 }));
416
417
  this.restart = restart ?? (() => this.#restart());
@@ -668,15 +669,35 @@ export class Updater {
668
669
  }
669
670
  #dataFormat() { try { return JSON.parse(readFileSync(join(this.#o.data, 'data-format.json'), 'utf8')).format; } catch { return undefined; } }
670
671
 
672
+ /* Whether the lock is held by a process alive now -- of THIS start of the machine (25-11, on a test machine: frozen
673
+ in the middle of an update, started again; its lock and its "under way" stayed, and a number taken by another
674
+ process after the start would have looked alive for ever). */
675
+ #lockHeld() {
676
+ let text = '';
677
+ try { text = readFileSync(join(this.#o.stateDir, 'lock'), 'utf8').trim(); } catch { return false; }
678
+ const [pidText, boot] = text.split(/\s+/);
679
+ const pid = Number(pidText) || 0;
680
+ if (boot && boot !== this.#o.bootId()) return false;
681
+ let alive = false; try { if (pid) { process.kill(pid, 0); alive = true; } } catch (e) { alive = e.code === 'EPERM'; }
682
+ return alive;
683
+ }
684
+ /* An update or a return cut by the machine stopping: no longer said "under way"; written down as cut. */
685
+ clearCut() {
686
+ const running = this.#state().running;
687
+ if (!running || this.#lockHeld()) return false;
688
+ this.#o.log(`${running.action} ${running.version}: cut at "${running.step}" (the machine stopped): no longer under way`);
689
+ this.#history({ action: running.action === 'back' ? 'back' : 'update', to: running.version ?? null, result: 'failed', why: `cut at "${running.step}": the machine stopped in the middle; nothing was switched` });
690
+ return true;
691
+ }
671
692
  #lock() {
672
693
  mkdirSync(this.#o.stateDir, { recursive: true, mode: 0o755 });
673
694
  const file = join(this.#o.stateDir, 'lock');
674
695
  for (let i = 0; i < 2; i += 1) {
675
- try { const fd = openSync(file, 'wx'); writeFileSync(fd, String(process.pid)); closeSync(fd); return () => rmSync(file, { force: true }); } catch (error) {
696
+ try { const fd = openSync(file, 'wx'); writeFileSync(fd, `${process.pid} ${this.#o.bootId()}`); closeSync(fd); return () => rmSync(file, { force: true }); } catch (error) {
676
697
  if (error.code !== 'EEXIST') throw error;
677
- const pid = Number(readFileSync(file, 'utf8')) || 0;
698
+ const pid = Number(readFileSync(file, 'utf8').trim().split(/\s+/)[0]) || 0;
678
699
  /* A lock left by a process that is gone (killed, the machine stopped) is taken over. */
679
- let alive = false; try { if (pid) { process.kill(pid, 0); alive = true; } } catch (e) { alive = e.code === 'EPERM'; }
700
+ const alive = this.#lockHeld();
680
701
  if (!alive) this.#o.log(`a stale lock (process ${pid || '?'} is gone): taken over`);
681
702
  if (alive) throw new Refused('an update or an installation is running already');
682
703
  rmSync(file, { force: true });
@@ -890,6 +911,7 @@ export class Updater {
890
911
 
891
912
  /* What the host program asked (a request file), or the timer's turn. */
892
913
  async runOnce() {
914
+ this.clearCut();
893
915
  const file = join(this.#o.stateDir, 'request');
894
916
  let request = '';
895
917
  try { request = readFileSync(file, 'utf8').trim(); rmSync(file, { force: true }); } catch { /* none */ }
@@ -1086,7 +1108,7 @@ async function main(argv) {
1086
1108
  const u = new Updater({ log: (line) => console.error(line) });
1087
1109
  const print = (x) => console.log(JSON.stringify(x, null, 2));
1088
1110
  switch (verb) {
1089
- case 'check': await u.check().catch(() => {}); return print(u.about());
1111
+ case 'check': u.clearCut(); await u.check().catch(() => {}); return print(u.about());
1090
1112
  case 'status': return print(u.about());
1091
1113
  case 'run': {
1092
1114
  /* What this release sets up beside itself (the certificate timer), at its first turn: the
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.532-next.1",
3
+ "version": "0.2.535-next.1",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "clavarium",
9
- "version": "0.2.532-next.1",
9
+ "version": "0.2.535-next.1",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "@xterm/addon-fit": "^0.11.0",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.532-next.1",
3
+ "version": "0.2.535-next.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "description": "A conversation is a VM with a terminal in it. Everything else is a folder.",