Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a field with future timestamp to use as firestore ttl #105

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 8 additions & 35 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { Logging } = require('@google-cloud/logging');
const functions = require('firebase-functions');
const { initializeApp } = require('firebase-admin/app');
const { getFirestore } = require('firebase-admin/firestore');
const { getFirestore, Timestamp } = require('firebase-admin/firestore');

Check warning on line 6 in functions/index.js

View check run for this annotation

Codecov / codecov/patch

functions/index.js#L6

Added line #L6 was not covered by tests
const { getMessaging } = require('firebase-admin/messaging');

const android = require('./android');
Expand Down Expand Up @@ -71,40 +71,6 @@
});
});

exports.cleanupOldRateLimits = regionalFunctions.pubsub.schedule('every day 01:00').timeZone('Etc/UTC').onRun(async (context) => {
var today = getToday();
var ref = db.collection('rateLimits').where('__name__', '<', today);
try {
await deleteQueryBatch(db, ref);
} catch (err) {
console.error('Error when deleting older documents!', err);
throw err;
}
});

async function deleteQueryBatch(db, query) {
const snapshot = await query.get();

const batchSize = snapshot.size;
if (batchSize === 0) {
// When there are no documents left, we are done
return;
}

// Delete documents in a batch
const batch = db.batch();
snapshot.docs.forEach((doc) => {
batch.delete(doc.ref);
});
await batch.commit();

// Recurse on the next process tick, to avoid
// exploding the stack.
process.nextTick(() => {
deleteQueryBatch(db, query);
});
}

async function handleRequest(req, res, payloadHandler) {
if (debug) functions.logger.info('Handling request', { requestBody: JSON.stringify(req.body) });
var today = getToday();
Expand All @@ -130,6 +96,7 @@
deliveredCount: 0,
errorCount: 0,
totalCount: 0,
expiresAt: getFirestoreTimestamp(),
};

try {
Expand Down Expand Up @@ -307,6 +274,12 @@
return yyyy + mm + dd;
}

function getFirestoreTimestamp() {
const now = new Date().getTime();
let endDate = new Date(now - (now % 86400000) + 86400000);
return Timestamp.fromDate(endDate);

Check warning on line 280 in functions/index.js

View check run for this annotation

Codecov / codecov/patch

functions/index.js#L278-L280

Added lines #L278 - L280 were not covered by tests
}

function getRateLimitsObject(doc) {
var d = new Date();
var remainingCount = (MAX_NOTIFICATIONS_PER_DAY - doc.deliveredCount);
Expand Down