@dibssoftware/dibs 0.0.0-stage → 0.0.178
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/CHANGELOG.md +577 -0
- package/LICENSE +670 -0
- package/README.md +234 -2
- package/bin/dibs-guard.mjs +7 -0
- package/bin/dibs.mjs +526 -0
- package/docs/benchmarks.md +193 -0
- package/docs/files.md +30 -0
- package/docs/guard.md +91 -0
- package/docs/later.md +43 -0
- package/docs/machines.md +147 -0
- package/docs/questions.md +78 -0
- package/docs/relay.md +196 -0
- package/docs/releasing.md +25 -0
- package/docs/scheduling.md +163 -0
- package/docs/simulators.md +17 -0
- package/package.json +44 -4
- package/skills/dibs/SKILL.md +162 -0
- package/src/acquire.mjs +697 -0
- package/src/admin.mjs +145 -0
- package/src/agents.mjs +145 -0
- package/src/alerts.mjs +437 -0
- package/src/answers.mjs +65 -0
- package/src/board.mjs +202 -0
- package/src/broker.mjs +369 -0
- package/src/commands.mjs +801 -0
- package/src/config.mjs +320 -0
- package/src/connect.mjs +106 -0
- package/src/cool.mjs +160 -0
- package/src/diskwarn.mjs +161 -0
- package/src/events.mjs +94 -0
- package/src/files.mjs +1100 -0
- package/src/guard.mjs +583 -0
- package/src/history.mjs +247 -0
- package/src/hogs.mjs +214 -0
- package/src/later.mjs +487 -0
- package/src/measure.mjs +262 -0
- package/src/nodecheck.mjs +17 -0
- package/src/notices.mjs +121 -0
- package/src/ondemand.mjs +572 -0
- package/src/overlap.mjs +151 -0
- package/src/override.mjs +351 -0
- package/src/owner.mjs +30 -0
- package/src/plain.mjs +467 -0
- package/src/platform.mjs +357 -0
- package/src/preempt.mjs +438 -0
- package/src/primer.mjs +61 -0
- package/src/proc.mjs +98 -0
- package/src/progress.mjs +158 -0
- package/src/qr.mjs +204 -0
- package/src/questions.mjs +389 -0
- package/src/quota.mjs +81 -0
- package/src/reads.mjs +54 -0
- package/src/relay.mjs +779 -0
- package/src/remote.mjs +584 -0
- package/src/reset.mjs +127 -0
- package/src/schedule.mjs +995 -0
- package/src/scratch.mjs +61 -0
- package/src/serve.mjs +736 -0
- package/src/service.mjs +221 -0
- package/src/shard.mjs +59 -0
- package/src/simctl.mjs +42 -0
- package/src/simpool.mjs +258 -0
- package/src/state.mjs +201 -0
- package/src/stats.mjs +328 -0
- package/src/style.mjs +44 -0
- package/src/talk.mjs +523 -0
- package/src/thin.sh +130 -0
- package/src/trust.mjs +152 -0
- package/src/util.mjs +184 -0
- package/src/version.mjs +2 -0
- package/src/web.mjs +1117 -0
- package/src/worker.mjs +318 -0
package/src/reset.mjs
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// onRelease: a command that puts a resource back after a lease on it ends,
|
|
2
|
+
// however it ends, so the next user finds it the way the config says (the
|
|
3
|
+
// shared simulator back at the default text size, say).
|
|
4
|
+
//
|
|
5
|
+
// The end is seen inside withState by whichever dibs call writes the state
|
|
6
|
+
// without the lease: dibs run as its command exits, dibs release, or any call
|
|
7
|
+
// that reaps a crashed holder or an expired hold. That call queues one entry
|
|
8
|
+
// per resource in s.resets, and once the lock is free it starts a supervisor
|
|
9
|
+
// (this file, run as a script) that runs the command with a timeout, logs any
|
|
10
|
+
// failure, and removes the entry. Queued under the lock, so each lease end
|
|
11
|
+
// queues exactly once; run outside it, so a slow command blocks nobody's state.
|
|
12
|
+
//
|
|
13
|
+
// While an entry is there, the next lease on that resource waits. Otherwise the
|
|
14
|
+
// reset races the next user, who'd screenshot a simulator still changing text
|
|
15
|
+
// size under them. The wait is bounded: the command is killed at its timeout,
|
|
16
|
+
// and an entry whose supervisor died or never started is dropped by the next
|
|
17
|
+
// state read.
|
|
18
|
+
import fs from 'node:fs';
|
|
19
|
+
import path from 'node:path';
|
|
20
|
+
import { spawn } from 'node:child_process';
|
|
21
|
+
import { fileURLToPath } from 'node:url';
|
|
22
|
+
import { paths, HOME } from './config.mjs';
|
|
23
|
+
import { withState } from './state.mjs';
|
|
24
|
+
import { pstart } from './proc.mjs';
|
|
25
|
+
import { stopTree, OS } from './platform.mjs';
|
|
26
|
+
import { coveredNow } from './schedule.mjs';
|
|
27
|
+
import { ms, dur, now, LEAN } from './util.mjs';
|
|
28
|
+
|
|
29
|
+
export const RESET_TIMEOUT = 30e3; // when the resource's onReleaseTimeout doesn't say
|
|
30
|
+
// A supervisor checks in within a second, even on a loaded machine. One that
|
|
31
|
+
// hasn't by this time never will, and its entry stops holding the resource.
|
|
32
|
+
const START_GRACE = 15e3;
|
|
33
|
+
const LOG = path.join(HOME, 'onrelease.log');
|
|
34
|
+
const KEEP_OUTPUT = 2000; // characters of a failed command's output kept in the log
|
|
35
|
+
const SELF = fileURLToPath(import.meta.url);
|
|
36
|
+
|
|
37
|
+
let starting = [];
|
|
38
|
+
|
|
39
|
+
// The user's file only: onRelease has no default. Read raw, since this runs
|
|
40
|
+
// under the lock, where dying on a bad file would leave the lock behind.
|
|
41
|
+
function settings() {
|
|
42
|
+
try { return JSON.parse(fs.readFileSync(paths.config, 'utf8')).resources || {}; } catch { return {}; }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// A run under its own session's hold isn't the end of that session's turn,
|
|
46
|
+
// and neither is a hold whose covered run is still going: the last of the two
|
|
47
|
+
// to end resets the resource.
|
|
48
|
+
function stillTheirs(s, l, r) {
|
|
49
|
+
if (l.kind === 'hold') return s.leases.some(x => x.kind === 'run' && x.covered?.includes(r) && x.spid && x.spid === l.pid);
|
|
50
|
+
return coveredNow(s, l, r);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Called by withState with the leases this change removed.
|
|
54
|
+
export function queueResets(s, gone) {
|
|
55
|
+
if (!gone.length) return;
|
|
56
|
+
const conf = settings();
|
|
57
|
+
for (const l of gone) for (const r of l.res) {
|
|
58
|
+
const c = conf[r];
|
|
59
|
+
if (typeof c?.onRelease !== 'string' || stillTheirs(s, l, r)) continue;
|
|
60
|
+
const timeout = /^[\d.]+\s*(ms|s|m|h)?$/.test(String(c.onReleaseTimeout ?? '')) ? ms(c.onReleaseTimeout) : RESET_TIMEOUT;
|
|
61
|
+
const x = { id: ++s.seq, res: r, cmd: c.onRelease, timeout, lease: l.id, who: l.who, agent: l.agent, at: now() };
|
|
62
|
+
(s.resets ||= []).push(x);
|
|
63
|
+
starting.push(x.id);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// After the lock is released: start a supervisor for each reset this change
|
|
68
|
+
// queued. A change that wasn't written starts nothing.
|
|
69
|
+
export function startResets(written) {
|
|
70
|
+
const ids = starting.splice(0);
|
|
71
|
+
if (!written) return;
|
|
72
|
+
for (const id of ids) {
|
|
73
|
+
const p = spawn(process.execPath, [...LEAN, SELF, String(id)], { detached: true, stdio: 'ignore', windowsHide: true, env: { ...process.env, DIBS_HOME: HOME } });
|
|
74
|
+
p.on('error', () => {}); // its entry is dropped once START_GRACE passes
|
|
75
|
+
p.unref();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// On every state read: drop entries nothing is working on any more. A
|
|
80
|
+
// command whose supervisor is gone is killed with the entry, so the next user
|
|
81
|
+
// never gets the resource while it's still being reset under them.
|
|
82
|
+
export function reapResets(s, alive) {
|
|
83
|
+
if (!s.resets?.length) return;
|
|
84
|
+
const t = now();
|
|
85
|
+
s.resets = s.resets.filter(x => {
|
|
86
|
+
if (x.pid ? alive(x.pid, x.pstart) && t < x.started + x.timeout + START_GRACE : t - x.at < START_GRACE) return true;
|
|
87
|
+
if (x.cpid && alive(x.cpid, x.cpstart)) stopTree(x.cpid, 'SIGKILL');
|
|
88
|
+
note(x, x.pid ? 'its supervisor died before the command finished' : "its supervisor didn't start, so the command never ran");
|
|
89
|
+
return false;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function note(x, what, output = '') {
|
|
94
|
+
const tail = output.trim() ? `\n ${output.trim().split('\n').join('\n ')}` : '';
|
|
95
|
+
try { fs.appendFileSync(LOG, `${new Date().toISOString()} ${x.res} after ${x.agent || x.who}: ${what}: ${x.cmd}${tail}\n`); } catch {}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// The supervisor: claim the entry, run the command, log a failure, let the resource go.
|
|
99
|
+
async function supervise(id) {
|
|
100
|
+
const started = pstart(process.pid);
|
|
101
|
+
const x = withState(s => {
|
|
102
|
+
const x = (s.resets || []).find(r => r.id === id && !r.pid);
|
|
103
|
+
if (x) Object.assign(x, { pid: process.pid, pstart: started, started: now() });
|
|
104
|
+
return x && { ...x };
|
|
105
|
+
});
|
|
106
|
+
if (!x) return;
|
|
107
|
+
const child = spawn(x.cmd, { shell: true, detached: OS !== 'windows', cwd: HOME, stdio: ['ignore', 'pipe', 'pipe'], windowsHide: true,
|
|
108
|
+
env: { ...process.env, DIBS_RESOURCE: x.res, DIBS_RELEASED_BY: x.agent || x.who } });
|
|
109
|
+
// The command's group, so a reap that finds this supervisor dead can stop it too.
|
|
110
|
+
if (child.pid) withState(s => { const r = (s.resets || []).find(r => r.id === id); if (r) Object.assign(r, { cpid: child.pid, cpstart: pstart(child.pid) }); });
|
|
111
|
+
let output = '';
|
|
112
|
+
const keep = d => { output = (output + d).slice(-KEEP_OUTPUT); };
|
|
113
|
+
child.stdout.on('data', keep);
|
|
114
|
+
child.stderr.on('data', keep);
|
|
115
|
+
let killed = false;
|
|
116
|
+
const timer = setTimeout(() => { killed = true; stopTree(child.pid, 'SIGKILL'); }, x.timeout);
|
|
117
|
+
const code = await new Promise(r => {
|
|
118
|
+
child.on('error', e => { keep(e.message); r(127); });
|
|
119
|
+
child.on('exit', (c, sig) => r(c ?? sig));
|
|
120
|
+
});
|
|
121
|
+
clearTimeout(timer);
|
|
122
|
+
if (killed) note(x, `killed after ${dur(x.timeout)} (onReleaseTimeout)`, output);
|
|
123
|
+
else if (code !== 0) note(x, `exited ${code}`, output);
|
|
124
|
+
withState(s => { s.resets = (s.resets || []).filter(r => r.id !== id); });
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (process.argv[1] && path.resolve(process.argv[1]) === SELF) supervise(Number(process.argv[2]));
|