@volter/teams 0.2.48 → 0.2.50
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/board-owner.mjs +36 -10
- package/home.mjs +2 -0
- package/machine-core.mjs +17 -0
- 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
|
-
*
|
|
59
|
-
*
|
|
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
|
|
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
|
|
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
|
-
|
|
87
|
-
lost(
|
|
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/home.mjs
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
// machine.key / machine.pub the host key; it signs `describe`, nothing else
|
|
6
6
|
// machine.sock the local door: owner-only, the OS user is the caller
|
|
7
7
|
// machine.json the running daemon: pid, key id, socket, linked server
|
|
8
|
+
// machine-link.json that daemon's link to its server as it is now: up, down or replaced, and since when
|
|
8
9
|
// launches.jsonl launch key → pane contextKey
|
|
9
10
|
// log.jsonl hash-chained accountability, no bodies
|
|
10
11
|
// log.<ts>.jsonl its rotations, oldest pruned past `logKeep`
|
|
@@ -66,6 +67,7 @@ export function ensureSocketDirectory(socketPath) {
|
|
|
66
67
|
export const paths = (home) => ({
|
|
67
68
|
machineKey: join(home, 'machine'),
|
|
68
69
|
machine: join(home, 'machine.json'),
|
|
70
|
+
link: join(home, 'machine-link.json'),
|
|
69
71
|
socket: machineSocketPath(home),
|
|
70
72
|
launches: join(home, 'launches.jsonl'),
|
|
71
73
|
log: join(home, 'log.jsonl'),
|
package/machine-core.mjs
CHANGED
|
@@ -98,6 +98,7 @@ export class MachineCore extends EventEmitter {
|
|
|
98
98
|
}
|
|
99
99
|
await this.afterListen();
|
|
100
100
|
if (this.link) {
|
|
101
|
+
this.#publishLink('down', null);
|
|
101
102
|
await this.refreshLinkConfiguration().catch((error) => this.emit('link', { event: 'configuration-failed', error: error.message }));
|
|
102
103
|
this.#connectLink();
|
|
103
104
|
}
|
|
@@ -157,6 +158,7 @@ export class MachineCore extends EventEmitter {
|
|
|
157
158
|
try { if (statSync(this.socketPath).ino === this.socketInode) rmSync(this.socketPath, { force: true }); } catch { /* already gone */ }
|
|
158
159
|
}
|
|
159
160
|
try { if (JSON.parse(readFileSync(this.p.machine, 'utf8')).pid === process.pid) rmSync(this.p.machine); } catch { /* absent or unreadable */ }
|
|
161
|
+
try { if (JSON.parse(readFileSync(this.p.link, 'utf8')).pid === process.pid) rmSync(this.p.link); } catch { /* absent or unreadable */ }
|
|
160
162
|
}
|
|
161
163
|
|
|
162
164
|
/** Before the connections close: a product finishes or abandons what is in flight. */
|
|
@@ -164,6 +166,18 @@ export class MachineCore extends EventEmitter {
|
|
|
164
166
|
|
|
165
167
|
// ---- the server link ----------------------------------------------------
|
|
166
168
|
|
|
169
|
+
/**
|
|
170
|
+
* The link as it is now, in a file beside the machine record (machine-link.json): `state` up, down or replaced, and
|
|
171
|
+
* since when. The record's `server` says only which server this daemon was started for; whether it reaches that
|
|
172
|
+
* server is read here, by what runs outside this process (an upgrade's activation watch, the service's next start),
|
|
173
|
+
* because a daemon that answers its socket with no link is unreachable to everything remote.
|
|
174
|
+
*/
|
|
175
|
+
#publishLink(state, error) {
|
|
176
|
+
if (this.linkPublished?.state === state) return;
|
|
177
|
+
this.linkPublished = { pid: process.pid, server: this.link?.server ?? null, state, since_ms: Date.now(), error: error ?? null };
|
|
178
|
+
try { writeJson(this.p.link, this.linkPublished); } catch { /* a reading for others; the link itself is unaffected */ }
|
|
179
|
+
}
|
|
180
|
+
|
|
167
181
|
/** Re-read the machine's custodian and grants from the server. */
|
|
168
182
|
async refreshLinkConfiguration() {
|
|
169
183
|
const configuration = await this.link.configuration();
|
|
@@ -187,6 +201,7 @@ export class MachineCore extends EventEmitter {
|
|
|
187
201
|
if (settled) return;
|
|
188
202
|
settled = true;
|
|
189
203
|
if (this.linkSocket === socket) { this.linkSocket = null; this.linkChannel = null; }
|
|
204
|
+
if (!this.linkReplaced) this.#publishLink('down', error?.message ?? null);
|
|
190
205
|
this.emit('link', { event: 'dropped', error: error?.message ?? null });
|
|
191
206
|
this.#retryLink();
|
|
192
207
|
};
|
|
@@ -198,6 +213,7 @@ export class MachineCore extends EventEmitter {
|
|
|
198
213
|
channel.on('stream', (stream, accept, refuse) => { void this.#onLinkStream(channel, stream, accept, refuse); });
|
|
199
214
|
channel.on('close', () => dropped(null));
|
|
200
215
|
channel.on('error', () => {});
|
|
216
|
+
this.#publishLink('up', null);
|
|
201
217
|
this.emit('link', { event: 'up', server });
|
|
202
218
|
} catch (error) { socket.terminate(); dropped(error); }
|
|
203
219
|
});
|
|
@@ -218,6 +234,7 @@ export class MachineCore extends EventEmitter {
|
|
|
218
234
|
// this one stops and says so.
|
|
219
235
|
if (String(reason ?? '') === 'replaced') {
|
|
220
236
|
this.linkReplaced = true;
|
|
237
|
+
this.#publishLink('replaced', 'another connector for this machine took over its link');
|
|
221
238
|
this.emit('link', { event: 'replaced', error: 'another connector for this machine took over its link; this one stopped linking' });
|
|
222
239
|
}
|
|
223
240
|
dropped(null);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@volter/teams",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.50",
|
|
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",
|