Pruner.js 1.2 KB

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