@hackerrank/astra-cli 0.1.8 → 0.1.9
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.
- package/package.json +1 -1
- package/src/ledger.js +62 -19
package/package.json
CHANGED
package/src/ledger.js
CHANGED
|
@@ -15,6 +15,40 @@ function atomicWrite(file, value) {
|
|
|
15
15
|
fs.renameSync(temp, file);
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
function sleepSync(milliseconds) {
|
|
19
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, milliseconds);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function withLedgerLock(file, callback) {
|
|
23
|
+
const lock = `${file}.lock`;
|
|
24
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
25
|
+
const deadline = Date.now() + 30000;
|
|
26
|
+
let descriptor;
|
|
27
|
+
while (Date.now() < deadline) {
|
|
28
|
+
try {
|
|
29
|
+
descriptor = fs.openSync(lock, "wx");
|
|
30
|
+
break;
|
|
31
|
+
} catch (error) {
|
|
32
|
+
if (error?.code !== "EEXIST") throw error;
|
|
33
|
+
sleepSync(20);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (descriptor === undefined) throw new Error(`timed out waiting for benchmark ledger lock: ${file}`);
|
|
37
|
+
try {
|
|
38
|
+
return callback();
|
|
39
|
+
} finally {
|
|
40
|
+
try { fs.closeSync(descriptor); } catch {}
|
|
41
|
+
try { fs.rmSync(lock, { force: true }); } catch {}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function readLatest(ledger) {
|
|
46
|
+
if (!fs.existsSync(ledger.path)) return { version: LEDGER_VERSION, cells: {} };
|
|
47
|
+
const value = JSON.parse(fs.readFileSync(ledger.path, "utf8"));
|
|
48
|
+
if (value.version !== LEDGER_VERSION || !value.cells || typeof value.cells !== "object") throw new Error("invalid benchmark ledger");
|
|
49
|
+
return value;
|
|
50
|
+
}
|
|
51
|
+
|
|
18
52
|
export function loadLedger(file) {
|
|
19
53
|
if (!fs.existsSync(file)) return { version: LEDGER_VERSION, cells: {}, path: file };
|
|
20
54
|
const value = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
@@ -30,28 +64,37 @@ function nextRunPath(ledger, cell) {
|
|
|
30
64
|
}
|
|
31
65
|
|
|
32
66
|
export function claimCell(ledger, cell) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
67
|
+
return withLedgerLock(ledger.path, () => {
|
|
68
|
+
const latest = readLatest(ledger);
|
|
69
|
+
ledger.cells = latest.cells;
|
|
70
|
+
const existing = ledger.cells[cell.cellKey];
|
|
71
|
+
if (existing) return existing;
|
|
72
|
+
const record = {
|
|
73
|
+
...cell,
|
|
74
|
+
status: "pending",
|
|
75
|
+
runPath: nextRunPath(ledger, cell),
|
|
76
|
+
createdAt: new Date().toISOString(),
|
|
77
|
+
leaseAt: null,
|
|
78
|
+
};
|
|
79
|
+
ledger.cells[cell.cellKey] = record;
|
|
80
|
+
atomicWrite(ledger.path, { version: LEDGER_VERSION, cells: ledger.cells });
|
|
81
|
+
return record;
|
|
82
|
+
});
|
|
45
83
|
}
|
|
46
84
|
|
|
47
85
|
export function transitionCell(ledger, cellKey, status, patch = {}) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
86
|
+
return withLedgerLock(ledger.path, () => {
|
|
87
|
+
const localCell = ledger.cells[cellKey];
|
|
88
|
+
const latest = readLatest(ledger);
|
|
89
|
+
ledger.cells = latest.cells;
|
|
90
|
+
const cell = ledger.cells[cellKey] || localCell;
|
|
91
|
+
if (!cell) throw new Error(`unknown benchmark cell: ${cellKey}`);
|
|
92
|
+
const next = { ...cell, ...patch, status, updatedAt: new Date().toISOString() };
|
|
93
|
+
if (["generating", "verifying"].includes(status) && patch.leaseAt === undefined) next.leaseAt = next.updatedAt;
|
|
94
|
+
ledger.cells[cellKey] = next;
|
|
95
|
+
atomicWrite(ledger.path, { version: LEDGER_VERSION, cells: ledger.cells });
|
|
96
|
+
return next;
|
|
97
|
+
});
|
|
55
98
|
}
|
|
56
99
|
|
|
57
100
|
export function selectWork(ledger, desiredCells, now = Date.now(), { force = false, sessionId = null } = {}) {
|