Description
The updateAllUserStatus function runs as a cron job to transition users' statuses. Currently, the query retrieves every user status document where a future status state exists:
const userStatusDocs = await userStatusModel
.where("futureStatus.state", "in", ["ACTIVE", "IDLE", "OOO"])
.get();
This fetches all future status documents, regardless of whether their transition start date has arrived. We should add a timestamp-based filter to retrieve only the documents that actually need to transition:
const userStatusDocs = await userStatusModel
.where("futureStatus.state", "in", ["ACTIVE", "IDLE", "OOO"])
.where("futureStatus.from", "<=", Date.now())
.get();
(Note: A composite index on futureStatus.state and futureStatus.from will be required).
Why do we need to fix this?
- Cost Efficiency: Currently, we pay for Firestore reads on every single future status document, even if they aren't scheduled to change for weeks. Adding this filter reduces reads to only the documents transitioning today.
- Performance: Prevents iterating over and processing thousands of irrelevant documents in memory, reducing the memory footprint of the cron service.
- Scalability: Prevents the cron job from slowing down as the community user base and scheduled statuses grow over time.
Description
The
updateAllUserStatusfunction runs as a cron job to transition users' statuses. Currently, the query retrieves every user status document where a future status state exists:This fetches all future status documents, regardless of whether their transition start date has arrived. We should add a timestamp-based filter to retrieve only the documents that actually need to transition:
(Note: A composite index on
futureStatus.stateandfutureStatus.fromwill be required).Why do we need to fix this?