2022-03-02 22:16:30 +01:00
|
|
|
import prefs from "./Prefs";
|
2022-03-03 22:52:07 +01:00
|
|
|
import subscriptionManager from "./SubscriptionManager";
|
2022-03-02 22:16:30 +01:00
|
|
|
|
2022-03-27 15:20:25 +02:00
|
|
|
const delayMillis = 25000; // 25 seconds
|
2022-03-02 22:16:30 +01:00
|
|
|
const intervalMillis = 1800000; // 30 minutes
|
|
|
|
|
|
|
|
class Pruner {
|
|
|
|
constructor() {
|
|
|
|
this.timer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
startWorker() {
|
|
|
|
if (this.timer !== null) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-08 17:33:17 +01:00
|
|
|
console.log(`[Pruner] Starting worker`);
|
2022-03-02 22:16:30 +01:00
|
|
|
this.timer = setInterval(() => this.prune(), intervalMillis);
|
|
|
|
setTimeout(() => this.prune(), delayMillis);
|
|
|
|
}
|
|
|
|
|
|
|
|
async prune() {
|
|
|
|
const deleteAfterSeconds = await prefs.deleteAfter();
|
|
|
|
const pruneThresholdTimestamp = Math.round(Date.now()/1000) - deleteAfterSeconds;
|
|
|
|
if (deleteAfterSeconds === 0) {
|
|
|
|
console.log(`[Pruner] Pruning is disabled. Skipping.`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log(`[Pruner] Pruning notifications older than ${deleteAfterSeconds}s (timestamp ${pruneThresholdTimestamp})`);
|
|
|
|
try {
|
2022-03-03 22:52:07 +01:00
|
|
|
await subscriptionManager.pruneNotifications(pruneThresholdTimestamp);
|
2022-03-02 22:16:30 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.log(`[Pruner] Error pruning old subscriptions`, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const pruner = new Pruner();
|
|
|
|
export default pruner;
|