fix: acknowledge pi maintenance barrier
This commit is contained in:
@@ -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 }; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user