Skip to content

Refactor: Optimize updateAllUserStatus Cron Job Query to Prevent Full Collection Scan #2597

Description

@Soumava-221B

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions