fix: acknowledge pi maintenance barrier

This commit is contained in:
2026-08-04 19:32:05 +02:00
parent 0b9ad7f53f
commit 5b3ce93e31
11 changed files with 168 additions and 52 deletions
+25 -8
View File
@@ -1,10 +1,27 @@
import { existsSync } from "node:fs";
/** An in-process admission barrier. A lease spans the complete create/resume decision. */
export class MaintenanceBarrier {
private active = false;
private admissions = 0;
private waiters: (() => void)[] = [];
/**
* A host-side lifecycle transaction creates this marker before it checks for active sessions.
* The marker is intentionally only an admission gate: it must never terminate existing Pi
* processes or make their in-flight work unavailable.
*/
export function createMaintenanceGate(markerFile: string): () => boolean {
return () => existsSync(markerFile);
acquire(): (() => void) | undefined {
if (this.active) return undefined;
this.admissions += 1;
let released = false;
return () => {
if (released) return;
released = true;
this.admissions -= 1;
if (this.admissions === 0) this.waiters.splice(0).forEach((resolve) => resolve());
};
}
async activate(): Promise<void> {
this.active = true;
if (this.admissions === 0) return;
await new Promise<void>((resolve) => this.waiters.push(resolve));
}
deactivate(): void { this.active = false; }
status(): { active: boolean; admissions: number } { return { active: this.active, admissions: this.admissions }; }
}