Skip to content

Instantly share code, notes, and snippets.

@bankchart
Created December 13, 2019 10:55
Show Gist options
  • Select an option

  • Save bankchart/10c551fbcea48e9c5e26d38583aa49b2 to your computer and use it in GitHub Desktop.

Select an option

Save bankchart/10c551fbcea48e9c5e26d38583aa49b2 to your computer and use it in GitHub Desktop.
const admin = require('firebase-admin');
const appConstant = require('../const');
const QUERY_LIMIT = 1000;
const cleanTempSession = async () => {
const db = admin.firestore();
const now = new Date();
const query = await db.collection('tempSession')
.where('expiry', '<', now)
.limit(QUERY_LIMIT)
.get();
for (let i = 0; i < query.docs.length; i++) {
await query.docs[i].ref.delete();
}
console.log('### Clean ' + query.docs.length + ' tempSession(s) ###');
};
const forcePostTrigger = async () => {
const db = admin.firestore();
const now = new Date();
let postQuery = null;
const postIdSet = new Set();
postQuery = await db.collection('post')
.where('postContentChanged', '==', true)
.limit(QUERY_LIMIT)
.get();
for (let i = 0; i < postQuery.docs.length; i++) {
const postId = postQuery.docs[i].data().id;
if (!postIdSet.has(postId)) {
postIdSet.add(postId);
await postQuery.docs[i].ref.update({
forceTriggerDatetime: now
});
}
}
postQuery = await db.collection('post')
.where('tagIndexChanged', '==', true)
.limit(QUERY_LIMIT)
.get();
for (let i = 0; i < postQuery.docs.length; i++) {
const postId = postQuery.docs[i].data().id;
if (!postIdSet.has(postId)) {
postIdSet.add(postId);
await postQuery.docs[i].ref.update({
forceTriggerDatetime: now
});
}
}
postQuery = await db.collection('post')
.where('questionStatusChanged', '==', true)
.limit(QUERY_LIMIT)
.get();
for (let i = 0; i < postQuery.docs.length; i++) {
const postId = postQuery.docs[i].data().id;
if (!postIdSet.has(postId)) {
postIdSet.add(postId);
await postQuery.docs[i].ref.update({
forceTriggerDatetime: now
});
}
}
console.log('### Force trigger ' + postIdSet.size + ' posts ###');
};
const updateReplyCount = async() => {
const db = admin.firestore();
const now = new Date();
const replyQuery = await db.collectionGroup('reply')
.where('tempFlag', '==', appConstant.REPLY_TEMP_FLAG.SCHEDULED)
.where('replyDatetime', '<=', now)
.limit(QUERY_LIMIT)
.get();
for (let i = 0; i < replyQuery.docs.length; i++) {
const postRef = replyQuery.docs[i].ref.parent.parent;
await db.runTransaction((tx) => {
return tx.get(postRef)
.then(async postDoc => {
tx.update(
replyQuery.docs[i].ref,
{ tempFlag: appConstant.REPLY_TEMP_FLAG.NORMAL }
);
tx.update(
postRef,
{ replyCount: admin.firestore.FieldValue.increment(1) }
);
})
}
);
}
console.log('### Update count for ' + replyQuery.docs.length + ' replies ###');
};
module.exports = async (context) => {
console.log('### Cleanup Scheduler Started ###');
await cleanTempSession();
await forcePostTrigger();
await updateReplyCount();
console.log('### Cleanup Scheduler Finished ###');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment