clavarium 0.2.521-next.1 → 0.2.522-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.
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.521-next.1",
3
+ "version": "0.2.522-next.1",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "clavarium",
9
- "version": "0.2.521-next.1",
9
+ "version": "0.2.522-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.521-next.1",
3
+ "version": "0.2.522-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.",
@@ -21,6 +21,9 @@ export const EVERY_MS = 60_000;
21
21
  has said nothing at all for this long: mute (30 s each) on three looks in a row. */
22
22
  export const STUCK_MS = 2 * 60_000;
23
23
  export const REVIVE_EVERY_MS = 6 * 3600_000;
24
+ /* After a cut, how long a machine is given to answer before it is restarted: 2 min. */
25
+ export const ANSWER_TRIES = 5;
26
+ export const ANSWER_EVERY_MS = 30_000;
24
27
  export const MANAGER_MODES = Object.freeze(['night', 'always', 'ask']);
25
28
  export const NIGHT_FROM = 0;
26
29
  export const NIGHT_UNTIL = 7;
@@ -37,10 +40,11 @@ export function statesOf(csv) {
37
40
  }
38
41
 
39
42
  export class VmWatch {
40
- #run; #store; #now; #hour; #log; #alert; #stuckMs; #reviveEveryMs; #downSince = null; #reviving = null; #running = new Set();
43
+ #run; #store; #now; #hour; #log; #alert; #stuckMs; #reviveEveryMs; #downSince = null; #reviving = null; #running = new Set(); #wait;
41
44
  ok = true;
42
45
  /* `hour()`: this server's local hour, 0-23 (injected for tests). */
43
- constructor({ run, store, now = () => Date.now(), hour = () => new Date().getHours(), log = (line) => console.error(line), alert = () => {}, stuckMs = STUCK_MS, reviveEveryMs = REVIVE_EVERY_MS }) {
46
+ constructor({ run, store, now = () => Date.now(), hour = () => new Date().getHours(), log = (line) => console.error(line), alert = () => {}, stuckMs = STUCK_MS, reviveEveryMs = REVIVE_EVERY_MS, wait = (ms) => new Promise((r) => { setTimeout(r, ms).unref?.(); }) }) {
47
+ this.#wait = wait;
44
48
  this.#run = run; this.#store = store; this.#now = now; this.#hour = hour; this.#log = log; this.#alert = alert; this.#stuckMs = stuckMs; this.#reviveEveryMs = reviveEveryMs;
45
49
  }
46
50
 
@@ -101,19 +105,36 @@ export class VmWatch {
101
105
  return result === 'done' ? 'revived' : 'revive failed';
102
106
  }
103
107
 
104
- /* BACK AFTER A CUT (23-11, on a real server: Multipass updated by snap left every machine suspended, and a machine
105
- suspended reads as a conversation asleep: nobody woke them). Only those that ran at the last look before it. */
108
+ /* BACK AFTER A CUT (23-11, on real machines: Multipass updated by snap left every machine suspended, and a machine
109
+ suspended reads as a conversation asleep; and Multipass can say « Running » of a machine whose process is gone,
110
+ which nothing can reach). Only those that ran at the last look before it: the suspended started again, then each
111
+ asked if it answers, for a while; one that never does is stopped for sure and started. */
106
112
  async #bringBack(states) {
107
- const left = [...this.#running].filter((n) => states.get(n) === 'Suspended');
108
- this.#running = new Set([...states].filter(([, s]) => s === 'Running').map(([n]) => n));
109
- if (!left.length) return;
110
- this.#log(`vm manager: ${left.length} machine(s) left suspended by the cut: started again`);
113
+ const ran = [...this.#running].filter((n) => states.has(n));
114
+ this.#running = new Set(ran);
115
+ if (!ran.length) return;
111
116
  const failed = [];
112
- for (const name of left) {
117
+ const start = async (name) => {
113
118
  const done = await this.#run(['vm', 'start', name], { timeoutMs: 300_000 }).catch((e) => ({ code: 1, stderr: e.message }));
114
- if (done.code === 0) this.#running.add(name); else { failed.push(name); this.#log(`vm manager: ${name} could not be started again (${said(done)})`); }
119
+ if (done.code !== 0) { failed.push(name); this.#log(`vm manager: ${name} could not be started again (${said(done)})`); }
120
+ return done.code === 0;
121
+ };
122
+ const asleep = ran.filter((n) => states.get(n) === 'Suspended');
123
+ if (asleep.length) this.#log(`vm manager: ${asleep.length} machine(s) left suspended by the cut: started again`);
124
+ for (const name of asleep) await start(name);
125
+ let silent = ran.filter((n) => !failed.includes(n));
126
+ for (let i = 0; i < ANSWER_TRIES && silent.length; i += 1) {
127
+ if (i) await this.#wait(ANSWER_EVERY_MS);
128
+ const said_ = await Promise.all(silent.map(async (n) => (await this.#run(['vm', 'exec', n, '--', 'true'], { timeoutMs: 20_000 }).catch(() => ({ code: 1 }))).code === 0));
129
+ silent = silent.filter((_, k) => !said_[k]);
115
130
  }
116
- if (failed.length) this.#alert({ title: 'Machines still asleep', body: `After the machine manager was cut, ${failed.length} could not be started again: open them.`, url: '/?open=settings&at=machines' });
131
+ for (const name of silent) {
132
+ this.#log(`vm manager: ${name} does not answer after the cut: stopped for sure and started`);
133
+ await this.#run(['vm', 'halt', name, 'force', 'kill'], { timeoutMs: 180_000 }).catch(() => {});
134
+ await start(name);
135
+ }
136
+ for (const n of failed) this.#running.delete(n);
137
+ if (failed.length) this.#alert({ title: 'Machines still down', body: `After the machine manager was cut, ${failed.length} could not be started again: open them.`, url: '/?open=settings&at=machines' });
117
138
  }
118
139
 
119
140
  /* RESTARTED BY ITS OWNER (Settings, Machines; confirmed there): now, whatever the hour or the last one.