tracking-retention.js 776 B

12345678910111213141516171819202122232425262728293031
  1. import { pruneTrackingEvents } from './db.js';
  2. const defaultIntervalMs = 24 * 60 * 60 * 1000;
  3. export function startTrackingRetentionWorker({
  4. enabled = true,
  5. days = 180,
  6. intervalMs = defaultIntervalMs,
  7. prune = pruneTrackingEvents,
  8. logger = console
  9. } = {}) {
  10. if (!enabled) return null;
  11. const run = () => {
  12. try {
  13. Promise.resolve(prune({ days })).catch((error) => {
  14. logger.warn?.(`Tracking retention cleanup failed: ${error.message}`);
  15. });
  16. } catch (error) {
  17. logger.warn?.(`Tracking retention cleanup failed: ${error.message}`);
  18. }
  19. };
  20. run();
  21. const timer = setInterval(run, Math.max(10, Number(intervalMs) || defaultIntervalMs));
  22. timer.unref?.();
  23. return {
  24. run,
  25. stop() {
  26. clearInterval(timer);
  27. }
  28. };
  29. }