|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Data retention policies service (#567). |
| 5 | + * |
| 6 | + * Manages automatic cleanup of old records based on configured retention periods. |
| 7 | + * Prevents storage bloat and helps with compliance/privacy concerns. |
| 8 | + */ |
| 9 | + |
| 10 | +const cache = require('./cache'); |
| 11 | +const logger = require('../logger'); |
| 12 | + |
| 13 | +function getEnvNumber(key, defaultValue) { |
| 14 | + const val = process.env[key]; |
| 15 | + if (val !== undefined && val !== '') { |
| 16 | + const num = Number(val); |
| 17 | + if (Number.isSafeInteger(num) && num > 0) return num; |
| 18 | + } |
| 19 | + return defaultValue; |
| 20 | +} |
| 21 | + |
| 22 | +const DELIVERY_RETENTION_DAYS = getEnvNumber('DELIVERY_LOG_RETENTION_DAYS', 30); |
| 23 | +const SESSION_RETENTION_DAYS = getEnvNumber('SESSION_RETENTION_DAYS', 7); |
| 24 | + |
| 25 | +const DELIVERY_TTL_MS = DELIVERY_RETENTION_DAYS * 24 * 60 * 60 * 1000; |
| 26 | +const SESSION_TTL_MS = SESSION_RETENTION_DAYS * 24 * 60 * 60 * 1000; |
| 27 | + |
| 28 | +/** |
| 29 | + * Applies retention policy to a cache record by setting its TTL. |
| 30 | + * Records older than the retention period are automatically removed by Redis. |
| 31 | + * |
| 32 | + * @param {string} key - Cache key |
| 33 | + * @param {number} ttlSeconds - TTL in seconds |
| 34 | + * @returns {Promise<void>} |
| 35 | + */ |
| 36 | +async function applyRetentionPolicy(key, ttlSeconds) { |
| 37 | + try { |
| 38 | + const redis = cache.getClient(); |
| 39 | + if (redis) { |
| 40 | + await redis.expire(key, ttlSeconds); |
| 41 | + } |
| 42 | + } catch (err) { |
| 43 | + logger.warn('Failed to apply retention policy', { key, error: err.message }); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Applies delivery log retention policy. |
| 49 | + * Keeps delivery records for the configured retention period. |
| 50 | + * |
| 51 | + * @param {string} deliveryId - Delivery ID |
| 52 | + * @returns {Promise<void>} |
| 53 | + */ |
| 54 | +async function applyDeliveryRetention(deliveryId) { |
| 55 | + await applyRetentionPolicy(`webhook_delivery:${deliveryId}`, DELIVERY_RETENTION_DAYS * 24 * 60 * 60); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Applies session retention policy. |
| 60 | + * Expires old session records to prevent unbounded accumulation. |
| 61 | + * |
| 62 | + * @param {string} sessionId - Session ID |
| 63 | + * @returns {Promise<void>} |
| 64 | + */ |
| 65 | +async function applySessionRetention(sessionId) { |
| 66 | + await applyRetentionPolicy(`session:${sessionId}`, SESSION_RETENTION_DAYS * 24 * 60 * 60); |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Cleanup callback for removing expired records from secondary indexes. |
| 71 | + * Called when a record expires, removes its entries from any associated indexes. |
| 72 | + * |
| 73 | + * @param {string} recordKey - The key of the expired record |
| 74 | + * @returns {Promise<void>} |
| 75 | + */ |
| 76 | +async function cleanupExpiredRecordIndexes(recordKey) { |
| 77 | + try { |
| 78 | + const redis = cache.getClient(); |
| 79 | + if (!redis) return; |
| 80 | + |
| 81 | + if (recordKey.startsWith('webhook_delivery:')) { |
| 82 | + const deliveryId = recordKey.substring('webhook_delivery:'.length); |
| 83 | + const pattern = `webhook:*:deliveries*`; |
| 84 | + const scan = (cursor) => |
| 85 | + redis.scan(cursor, 'MATCH', pattern, 'COUNT', 100); |
| 86 | + |
| 87 | + let nextCursor = '0'; |
| 88 | + do { |
| 89 | + const [cursor, keys] = await scan(nextCursor); |
| 90 | + nextCursor = cursor; |
| 91 | + |
| 92 | + if (keys.length > 0) { |
| 93 | + const pipeline = redis.multi(); |
| 94 | + keys.forEach((key) => { |
| 95 | + pipeline.zrem(key, deliveryId); |
| 96 | + }); |
| 97 | + await pipeline.exec(); |
| 98 | + } |
| 99 | + } while (nextCursor !== '0'); |
| 100 | + } |
| 101 | + } catch (err) { |
| 102 | + logger.warn('Cleanup of record indexes failed', { recordKey, error: err.message }); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +module.exports = { |
| 107 | + DELIVERY_RETENTION_DAYS, |
| 108 | + SESSION_RETENTION_DAYS, |
| 109 | + DELIVERY_TTL_MS, |
| 110 | + SESSION_TTL_MS, |
| 111 | + applyRetentionPolicy, |
| 112 | + applyDeliveryRetention, |
| 113 | + applySessionRetention, |
| 114 | + cleanupExpiredRecordIndexes, |
| 115 | +}; |
0 commit comments