Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 1x 1x 12x 12x 11x 5x 5x 6x 7x 7x 7x 608x 7x 5x 1x | /**
* Cron: Delete expired backpack items.
*
* Uses a collection group query on 'backpack' to find all expired items
* in a single Firestore read instead of N+1 queries.
*/
const { db } = require('../utils/firebase');
const log = require('../utils/log');
async function backpackCleanup() {
const timestamp = Date.now();
// Single collection group query instead of reading all users + their backpacks
const snapshot = await db
.collectionGroup('backpack')
.where('expiresAt', '<=', timestamp)
.limit(500)
.get();
if (snapshot.empty) {
log.info('cron', 'backpackCleanup: no expired items');
return;
}
// Batch delete expired items
for (let i = 0; i < snapshot.docs.length; i += 500) {
const batch = db.batch();
const chunk = snapshot.docs.slice(i, i + 500);
for (const doc of chunk) {
batch.delete(doc.ref);
}
await batch.commit();
}
log.info('cron', 'backpackCleanup: cleaned expired items', { count: snapshot.docs.length });
}
module.exports = backpackCleanup;
|