@volter/teams 0.2.48 → 0.2.49

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/board-owner.mjs +36 -10
  2. package/package.json +1 -1
package/board-owner.mjs CHANGED
@@ -16,7 +16,7 @@
16
16
  // cwd the caller's working directory, against which a path it names is read
17
17
  // stdin the caller's standard input, when it names `-` as a file (else null)
18
18
  // answer: one JSON line { code, stdout, stderr }
19
- import { realpathSync } from 'node:fs';
19
+ import { existsSync, realpathSync } from 'node:fs';
20
20
  import { connect } from 'node:net';
21
21
  import { join, resolve } from 'node:path';
22
22
 
@@ -55,12 +55,40 @@ export function unansweredText(answer, what) {
55
55
  }
56
56
 
57
57
  /**
58
- * Send one verb to the owner of the home at `root`: answers `{ code, stdout, stderr }`. A home whose owner is not
59
- * running answers that, with what starts it; nothing else runs the verb.
58
+ * How long a verb waits for an absent owner to come back. An owner stops when a newer orchestrator is installed under
59
+ * it, and its keeper starts the new one ten seconds later (the Open Autonomy kit's keeper/start.ts: "the board returns
60
+ * in 10 s"); the new owner answers within a second of starting. Three times that restart.
60
61
  */
61
- export function askBoardOwner(root, request) {
62
+ export const OWNER_RETURN_MS = 30_000;
63
+ const OWNER_RETRY_MS = 200;
64
+
65
+ /**
66
+ * Send one verb to the owner of the home at `root`: answers `{ code, stdout, stderr }`. Nothing else runs the verb.
67
+ * A home whose owner is not answering, and has had one (its lock `<home>/board.lock` is there), is an owner between
68
+ * two runs: the connection is tried again until the owner is back, for `waitMs` at most, and the verb is sent once,
69
+ * to the owner that took the connection. Only then, or at once in a home no owner ever held, is the answer that the
70
+ * home has no running owner, with what starts it.
71
+ */
72
+ export async function askBoardOwner(root, request, { waitMs = OWNER_RETURN_MS } = {}) {
62
73
  let path;
63
- try { path = boardOwnerSocket(root); } catch (error) { return Promise.resolve({ code: 1, stdout: '', stderr: `${error.message}\n` }); }
74
+ try { path = boardOwnerSocket(root); } catch (error) { return { code: 1, stdout: '', stderr: `${error.message}
75
+ ` }; }
76
+ const began = Date.now();
77
+ for (;;) {
78
+ const answer = await askOnce(path, root, request);
79
+ if (!answer.down) return answer;
80
+ const waited = Date.now() - began;
81
+ const held = existsSync(join(boardHome(root), 'board.lock'));
82
+ if (!held || waited >= waitMs) {
83
+ const none = held && waitMs > 0 ? `, and none came back within ${Math.round(waited / 1000)} seconds` : '';
84
+ return { code: 1, stdout: '', stderr: `the boards of ${root} have no running owner${none}: \`supercode workflow serve --root ${root}\` is it (an install's keeper runs it)\n` };
85
+ }
86
+ await new Promise((again) => setTimeout(again, Math.min(OWNER_RETRY_MS, waitMs - waited)));
87
+ }
88
+ }
89
+
90
+ /** One connection to the owner's socket: its answer, or `{ down: true }` when nothing is listening there (nothing sent). */
91
+ function askOnce(path, root, request) {
64
92
  return new Promise((done) => {
65
93
  let answer = '';
66
94
  let settled = false;
@@ -81,12 +109,10 @@ export function askBoardOwner(root, request) {
81
109
  socket.end();
82
110
  try { finish(JSON.parse(answer.slice(0, end))); } catch { lost(`the board owner of ${root} answered nothing readable\n`); }
83
111
  });
84
- socket.on('close', () => lost(`the board owner of ${root} closed the call before answering\n`));
85
112
  socket.on('error', (error) => {
86
- const down = !sent && ['ENOENT', 'ECONNREFUSED'].includes(error.code);
87
- lost(down
88
- ? `the boards of ${root} have no running owner: \`supercode workflow serve --root ${root}\` is it (an install's keeper runs it)\n`
89
- : sent ? `the board owner of ${root} dropped the call after it was sent: ${error.message}\n` : `the board owner of ${root} could not be reached: ${error.message}\n`);
113
+ if (!sent && ['ENOENT', 'ECONNREFUSED'].includes(error.code)) return finish({ down: true });
114
+ lost(sent ? `the board owner of ${root} dropped the call after it was sent: ${error.message}\n` : `the board owner of ${root} could not be reached: ${error.message}\n`);
90
115
  });
116
+ socket.on('close', () => lost(`the board owner of ${root} closed the call before answering\n`));
91
117
  });
92
118
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volter/teams",
3
- "version": "0.2.48",
3
+ "version": "0.2.49",
4
4
  "type": "module",
5
5
  "description": "Volter Teams, the neutral team plane: machine identity and daemon core, the local door and server link, admission from machine grants, product doors, and the Teams server's neutral routes (company RFC 0008).",
6
6
  "license": "MIT",