-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsweeper.js
More file actions
72 lines (64 loc) · 2.19 KB
/
Copy pathsweeper.js
File metadata and controls
72 lines (64 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// ipfs-gate v1 — sweeper / reconcile loop.
// Periodic background job that:
// 1. Marks expired reservations as 'expired'
// 2. Expires CLAIMS past their timer (the v1 lifecycle authority) + their pins
// 3. Expires any legacy pins past their own clock
// 4. Unpins from Kubo + GCs any CIDs that now have no active pin records
// (last-funder unpin; Stage 1b will promote a dormant backstop here instead)
//
// Runs every SWEEPER_INTERVAL_MS (default 60s).
const quota = require('./quota');
const kubo = require('./backends/kubo');
const SWEEPER_INTERVAL_MS = parseInt(process.env.SWEEPER_INTERVAL_MS || '60000', 10);
let timer = null;
let running = false;
async function runOnce() {
if (running) {
console.log('[sweeper] previous run still in progress, skipping');
return;
}
running = true;
try {
const result = quota.sweep();
if (result.expired_reservations || result.expired_claims || result.expired_pins || result.cids_to_unpin.length) {
console.log(`[sweeper] expired_reservations=${result.expired_reservations} expired_claims=${result.expired_claims} expired_pins=${result.expired_pins} cids_to_unpin=${result.cids_to_unpin.length}`);
}
if (result.cids_to_unpin.length > 0) {
let unpinned = 0;
for (const cid of result.cids_to_unpin) {
try {
await kubo.unpin(cid);
unpinned++;
} catch (e) {
console.warn(`[sweeper] failed to unpin ${cid}: ${e.message}`);
}
}
if (unpinned > 0) {
try {
await kubo.gc();
console.log(`[sweeper] unpinned ${unpinned}, ran GC`);
} catch (e) {
console.warn(`[sweeper] GC failed: ${e.message}`);
}
}
}
} catch (e) {
console.error(`[sweeper] run failed: ${e.message}`);
} finally {
running = false;
}
}
function start() {
if (timer) return;
console.log(`[sweeper] starting, interval = ${SWEEPER_INTERVAL_MS}ms`);
// Run once on boot, then on interval
runOnce().catch(() => {});
timer = setInterval(() => runOnce().catch(() => {}), SWEEPER_INTERVAL_MS);
}
function stop() {
if (timer) {
clearInterval(timer);
timer = null;
}
}
module.exports = { start, stop, runOnce };