MCPcopy Create free account
hub / github.com/coder/mux / acquireLeaseMutationLock

Function acquireLeaseMutationLock

src/node/services/workflows/WorkflowRunStore.ts:1231–1259  ·  view source on GitHub ↗
(
  lockDir: string,
  nowMs: number,
  staleLeaseMs: number
)

Source from the content-addressed store, hash-verified

1229}
1230
1231async function acquireLeaseMutationLock(
1232 lockDir: string,
1233 nowMs: number,
1234 staleLeaseMs: number
1235): Promise<boolean> {
1236 try {
1237 await fs.mkdir(lockDir);
1238 return true;
1239 } catch (error) {
1240 if (!isErrno(error, "EEXIST")) {
1241 throw error;
1242 }
1243 }
1244
1245 try {
1246 const stat = await fs.stat(lockDir);
1247 if (nowMs - stat.mtimeMs <= staleLeaseMs) {
1248 return false;
1249 }
1250 await fs.rm(lockDir, { recursive: true, force: true });
1251 await fs.mkdir(lockDir);
1252 return true;
1253 } catch (error) {
1254 if (isErrno(error, "EEXIST") || isErrno(error, "ENOENT")) {
1255 return false;
1256 }
1257 throw error;
1258 }
1259}
1260
1261function isErrno(error: unknown, code: string): boolean {
1262 return error instanceof Error && "code" in error && error.code === code;

Callers 2

acquireLeaseMethod · 0.85

Calls 2

isErrnoFunction · 0.85
statMethod · 0.65

Tested by

no test coverage detected