| 1 |
import prefs from "./Prefs"; |
| 2 |
import subscriptionManager from "./SubscriptionManager"; |
| 3 |
|
| 4 |
const delayMillis = 25000; // 25 seconds |
| 5 |
const intervalMillis = 1800000; // 30 minutes |
| 6 |
|
| 7 |
class Pruner { |
| 8 |
constructor() { |
| 9 |
this.timer = null; |
| 10 |
} |
| 11 |
|
| 12 |
startWorker() { |
| 13 |
if (this.timer !== null) { |
| 14 |
return; |
| 15 |
} |
| 16 |
console.log(`[Pruner] Starting worker`); |
| 17 |
this.timer = setInterval(() => this.prune(), intervalMillis); |
| 18 |
setTimeout(() => this.prune(), delayMillis); |
| 19 |
} |
| 20 |
|
| 21 |
stopWorker() { |
| 22 |
if (this.timer) { |
| 23 |
clearTimeout(this.timer); |
| 24 |
this.timer = null; |
| 25 |
} |
| 26 |
console.log("[Pruner] Stopped worker"); |
| 27 |
} |
| 28 |
|
| 29 |
async prune() { |
| 30 |
const deleteAfterSeconds = await prefs.deleteAfter(); |
| 31 |
const pruneThresholdTimestamp = Math.round(Date.now() / 1000) - deleteAfterSeconds; |
| 32 |
if (deleteAfterSeconds === 0) { |
| 33 |
console.log(`[Pruner] Pruning is disabled. Skipping.`); |
| 34 |
return; |
| 35 |
} |
| 36 |
console.log(`[Pruner] Pruning notifications older than ${deleteAfterSeconds}s (timestamp ${pruneThresholdTimestamp})`); |
| 37 |
try { |
| 38 |
await subscriptionManager.pruneNotifications(pruneThresholdTimestamp); |
| 39 |
} catch (e) { |
| 40 |
console.log(`[Pruner] Error pruning old subscriptions`, e); |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
const pruner = new Pruner(); |
| 46 |
export default pruner; |