zephyr-agent
Version:
Zephyr plugin agent
51 lines (50 loc) • 1.62 kB
JavaScript
import "node:module";
import { mkdir, open as promises_open } from "node:fs/promises";
import node_path from "node:path";
import { lock } from "@bybrave/proper-lockfile2";
import { ze_log } from "../logging/debug.mjs";
import { ZE_LOCKS_PATH, ensurePrivateFilePermissions } from "./storage-keys.mjs";
import { storage } from "./storage.mjs";
const STORAGE_LOCK_STALE_MS = 60000;
const STORAGE_LOCK_RETRIES = {
retries: 120,
factor: 1.2,
minTimeout: 25,
maxTimeout: 500,
randomize: true
};
function getStorageLockPath(name) {
return node_path.join(ZE_LOCKS_PATH, name);
}
async function withStorageLock(name, action, options = {}) {
const { staleMs = STORAGE_LOCK_STALE_MS, retries = STORAGE_LOCK_RETRIES, whenUnavailable = 'throw' } = options;
let release;
try {
release = await acquireStorageLock(name, staleMs, retries);
} catch (error) {
if ('throw' === whenUnavailable) throw error;
ze_log.misc(`Proceeding without the ${name} storage lock:`, error);
}
try {
return await action();
} finally{
await release?.();
}
}
async function acquireStorageLock(name, staleMs, retries) {
await storage;
await mkdir(ZE_LOCKS_PATH, {
recursive: true,
mode: 448
});
const lockPath = getStorageLockPath(name);
await (await promises_open(lockPath, 'a', 384)).close();
ensurePrivateFilePermissions(lockPath);
return lock(lockPath, {
realpath: false,
retries,
stale: staleMs
});
}
export { getStorageLockPath, withStorageLock };
//# sourceMappingURL=storage-lock.mjs.map