|
1 | | -// TODO: Rewrite for mongodb |
2 | | - |
| 1 | +const getDatabase = require('./mongodb'); |
3 | 2 | const getDayjs = require('./dayjs-wrapper'); |
| 3 | +const config = require('../config'); |
4 | 4 |
|
5 | | -async function checkSubscription(data, resourceUrl, apiurl) { |
6 | | - const dayjs = await getDayjs(); |
7 | | - const subscription = data.subscriptions[resourceUrl][apiurl]; |
8 | | - if (dayjs(subscription.whenExpires).isBefore(dayjs())) { |
9 | | - delete data.subscriptions[resourceUrl][apiurl]; |
10 | | - } else if (subscription.ctConsecutiveErrors > data.prefs.maxConsecutiveErrors) { |
11 | | - delete data.subscriptions[resourceUrl][apiurl]; |
12 | | - } |
13 | | -} |
| 5 | +/** |
| 6 | + * Removes expired and errored subscriptions from MongoDB |
| 7 | + * Works with the MongoDB schema: { _id: resourceUrl, pleaseNotify: [...] } |
| 8 | + */ |
| 9 | +async function removeExpiredSubscriptions() { |
| 10 | + try { |
| 11 | + const db = await getDatabase(); |
| 12 | + const dayjs = await getDayjs(); |
| 13 | + const collection = db.collection('subscriptions'); |
14 | 14 |
|
15 | | -async function scanApiUrls(data, resourceUrl) { |
16 | | - const subscriptions = data.subscriptions[resourceUrl]; |
17 | | - for (const apiurl in subscriptions) { |
18 | | - if (Object.prototype.hasOwnProperty.call(subscriptions, apiurl)) { |
19 | | - await checkSubscription(data, resourceUrl, apiurl); |
20 | | - } |
21 | | - } |
22 | | - if (0 === subscriptions.length) { |
23 | | - delete data.subscriptions[resourceUrl]; |
24 | | - } |
25 | | -} |
| 15 | + let totalRemoved = 0; |
| 16 | + let documentsProcessed = 0; |
| 17 | + let documentsDeleted = 0; |
| 18 | + |
| 19 | + // Find all subscription documents |
| 20 | + const cursor = collection.find({}); |
| 21 | + |
| 22 | + while (await cursor.hasNext()) { |
| 23 | + const doc = await cursor.next(); |
| 24 | + documentsProcessed++; |
26 | 25 |
|
27 | | -async function scanResources(data) { |
28 | | - for (const resourceUrl in data.subscriptions) { |
29 | | - if (Object.prototype.hasOwnProperty.call(data.subscriptions, resourceUrl)) { |
30 | | - await scanApiUrls(data, resourceUrl); |
| 26 | + if (!doc.pleaseNotify || !Array.isArray(doc.pleaseNotify)) { |
| 27 | + continue; |
| 28 | + } |
| 29 | + |
| 30 | + // Filter out expired and errored subscriptions |
| 31 | + const validSubscriptions = doc.pleaseNotify.filter(subscription => { |
| 32 | + // Remove if expired |
| 33 | + if (dayjs(subscription.whenExpires).isBefore(dayjs())) { |
| 34 | + totalRemoved++; |
| 35 | + return false; |
| 36 | + } |
| 37 | + |
| 38 | + // Remove if too many consecutive errors |
| 39 | + if (subscription.ctConsecutiveErrors > config.maxConsecutiveErrors) { |
| 40 | + totalRemoved++; |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + return true; |
| 45 | + }); |
| 46 | + |
| 47 | + // Update document if subscriptions were removed |
| 48 | + if (validSubscriptions.length !== doc.pleaseNotify.length) { |
| 49 | + if (validSubscriptions.length === 0) { |
| 50 | + // Remove entire document if no valid subscriptions remain |
| 51 | + await collection.deleteOne({ _id: doc._id }); |
| 52 | + documentsDeleted++; |
| 53 | + } else { |
| 54 | + // Update document with filtered subscriptions |
| 55 | + await collection.updateOne( |
| 56 | + { _id: doc._id }, |
| 57 | + { $set: { pleaseNotify: validSubscriptions } } |
| 58 | + ); |
| 59 | + } |
| 60 | + } |
31 | 61 | } |
32 | | - } |
33 | | -} |
34 | 62 |
|
35 | | -async function removeExpiredSubscriptions(data) { |
36 | | - await scanResources(data); |
| 63 | + console.log(`Subscription cleanup completed: ${totalRemoved} expired/errored subscriptions removed, ${documentsProcessed} documents processed, ${documentsDeleted} empty documents deleted`); |
| 64 | + |
| 65 | + return { |
| 66 | + subscriptionsRemoved: totalRemoved, |
| 67 | + documentsProcessed, |
| 68 | + documentsDeleted |
| 69 | + }; |
| 70 | + |
| 71 | + } catch (error) { |
| 72 | + console.error('Error removing expired subscriptions:', error); |
| 73 | + throw error; |
| 74 | + } |
37 | 75 | } |
38 | 76 |
|
39 | 77 | module.exports = removeExpiredSubscriptions; |
0 commit comments