Reputation: 2066
exports.removeOldBattles = globalFunctions.runWith(runtimeOpts).pubsub.schedule('every 1 minutes').onRun((context) => {
var coll = db.collection("Battles").where("timeStamp", "<", today);
const promises = [];
coll.get().then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
var battleId = doc.id;
const promise = db.collection("Battles").doc(battleId).delete();
promises.push(promise);
});
Promise.all(promises).then(() => {
mailMessage("Battles Removal!", "This is a confirmation of battle removal.");
console.log("Yesterday battles successfully deleted!");
});
}).catch(function (error) {
mailMessage("Failed Battles Removal!", "This is a confirmation of failed battle removal.");
console.error("Error getting battle snapshots: ", error);
});
return null;
});
I have the above code to delete documents from yesterday in a Battles collection but doesn't seem to be working... I get this error 9 FAILED_PRECONDITION: The requested snapshot version is too old.
and I am not sure how to resolve it.
Any recommendation?
Upvotes: 0
Views: 1927
Reputation: 4164
According to the documentation:
If the backlog in the subscription is too old -- and the resulting snapshot would expire in less than 1 hour -- then FAILED_PRECONDITION is returned. See also the Snapshot.expire_time field.
Unfortunately, this doesn't give us a lot to work with, but I can assume that this may have to do with cold starts and running multiple instances ontop of each other.
Some users found returning a promise was the most effective solution since this allows Cloud Functions to hook into resolve and reject rather than finalizing on a null return.
Source: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.snapshots/create
Upvotes: 1