Skip to content

chore: use @rocket.chat/cron for presence status expiration in microservices - #41153

Merged
dionisio-bot[bot] merged 3 commits into
developfrom
chore/presence-expiration-cron
Jul 20, 2026
Merged

chore: use @rocket.chat/cron for presence status expiration in microservices#41153
dionisio-bot[bot] merged 3 commits into
developfrom
chore/presence-expiration-cron

Conversation

@ricardogarim

@ricardogarim ricardogarim commented Jul 2, 2026

Copy link
Copy Markdown
Member

Proposed changes (including videos or screenshots)

Move presence status expiration off a hand-rolled setTimeout and onto @rocket.chat/cron, plus the wiring cron needs to run in a microservice:

  • Register ICronHistoryModel in registerServiceModels (services never did, so cron's CronHistory write threw).
  • cronJobs.start(db) in the presence-service entrypoint.
  • setTimeoutcronJobs.addAtTimestamp — so one instance runs each expiration (agenda locks the job in Mongo), instead of every presence instance running its own timer and reverting independently.

Issue(s)

Steps to test or reproduce

  1. Run in microservices mode (yarn ms).
  2. POST /api/v1/users.setStatus with status: "busy" and a short expiresAt.
  3. rocketchat_cron gets a presence-status-expiration job at that time; after it fires the status reverts and rocketchat_cron_history gets a run row.

Further comments

Review in cubic

Summary by CodeRabbit

  • New Features

    • Presence updates now use a shared scheduled-job system, improving how status expirations are handled across instances.
    • The presence service now starts its scheduling subsystem automatically during startup.
  • Bug Fixes

    • Reduced the risk of missed or duplicated presence expiration handling by replacing local timers with centralized job scheduling.
    • Improved cleanup when presence-related jobs are stopped or rescheduled.

@dionisio-bot

dionisio-bot Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Looks like this PR is ready to merge! 🎉
If you have any trouble, please check the PR guidelines

@changeset-bot

changeset-bot Bot commented Jul 2, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: a9077ec

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai

coderabbitai Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 77f0af2e-5eb0-47bd-a436-c72482cf0ac7

📥 Commits

Reviewing files that changed from the base of the PR and between 60f8f1d and d7cecad.

📒 Files selected for processing (1)
  • ee/packages/presence/src/Presence.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • ee/packages/presence/src/Presence.ts
📜 Recent review details
⏰ Context from checks skipped due to timeout. (2)
  • GitHub Check: cubic · AI code reviewer
  • GitHub Check: Hacktron Security Check

Walkthrough

Presence status expiration scheduling is migrated from per-instance setTimeout-based timers to a shared cron-job mechanism using cronJobs.addAtTimestamp/cronJobs.remove. Supporting changes add a @rocket.chat/cron dependency, register ICronHistoryModel, and start cron jobs during presence-service startup.

Changes

Cron-based presence expiration

Layer / File(s) Summary
Cron history model registration
packages/models/src/index.ts
Imports CronHistoryRaw and registers it as ICronHistoryModel in registerServiceModels.
Presence expiration scheduling migrated to cronJobs
ee/packages/presence/src/Presence.ts, ee/packages/presence/package.json
Replaces expirationTimeout/delay-cap timer logic with a STATUS_EXPIRATION_JOB cron entry managed via cronJobs.remove/cronJobs.addAtTimestamp; stopped() removes the job instead of clearing a timeout; adds @rocket.chat/cron dependency.
Presence service cron startup
ee/apps/presence-service/src/service.ts, ee/apps/presence-service/package.json
Imports cronJobs, adds @rocket.chat/cron dependency, and awaits cronJobs.start(db) during service startup after model registration.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant PresenceService
  participant cronJobs
  participant Presence
  participant Database

  PresenceService->>Database: register service models
  PresenceService->>cronJobs: start(db)
  Presence->>Presence: setupNextExpiration()
  Presence->>cronJobs: remove(STATUS_EXPIRATION_JOB)
  Presence->>cronJobs: addAtTimestamp(STATUS_EXPIRATION_JOB, statusExpiresAt)
  cronJobs-->>Presence: trigger expiration job
  Presence->>Presence: processExpiredStatuses()
  Presence->>cronJobs: remove(STATUS_EXPIRATION_JOB) on stopped()
Loading

Possibly related PRs

Suggested labels: type: chore

Suggested reviewers: KevLehman, sampaiodiego

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: switching presence expiration in microservices to @rocket.chat/cron.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Warning

Review ran into problems

🔥 Problems

Errors were encountered while retrieving linked issues.

Errors (1)
  • PRES-32: Request failed with status code 401

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@ee/packages/presence/src/Presence.ts`:
- Around line 154-156: The expiration callback in Presence.addAtTimestamp is
swallowing failures because the .catch() logs and then resolves, causing the
scheduler to treat a failed status expiration as successful. Update the callback
passed to cronJobs.addAtTimestamp so that handleExpirationJob still logs via
logger.error but rethrows the caught error or otherwise returns a rejected
promise, preserving the failure signal.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4f8b71fa-773c-42a2-bc67-192893e99562

📥 Commits

Reviewing files that changed from the base of the PR and between 6da12d7 and 60f8f1d.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (5)
  • ee/apps/presence-service/package.json
  • ee/apps/presence-service/src/service.ts
  • ee/packages/presence/package.json
  • ee/packages/presence/src/Presence.ts
  • packages/models/src/index.ts
📜 Review details
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx,js}

📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)

**/*.{ts,tsx,js}: Write concise, technical TypeScript/JavaScript with accurate typing in Playwright tests
Avoid code comments in the implementation

Files:

  • ee/apps/presence-service/src/service.ts
  • packages/models/src/index.ts
  • ee/packages/presence/src/Presence.ts
🧠 Learnings (3)
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In the Rocket.Chat repository, do not reference Biome lint rules in code review feedback. Biome is not used even if biome.json exists; only reference Biome rules if there is explicit, project-wide usage documented. For TypeScript files, review lint implications without Biome guidance unless the project enables Biome rules.

Applied to files:

  • ee/apps/presence-service/src/service.ts
  • packages/models/src/index.ts
  • ee/packages/presence/src/Presence.ts
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In this repository (RocketChat/Rocket.Chat), Biome lint rules are not used even if a biome.json exists. When reviewing TypeScript files (e.g., packages/ui-voip/src/providers/useMediaSession.ts), ensure lint suggestions do not reference Biome-specific rules. Rely on general ESLint/TypeScript lint rules and project conventions instead.

Applied to files:

  • ee/apps/presence-service/src/service.ts
  • packages/models/src/index.ts
  • ee/packages/presence/src/Presence.ts
📚 Learning: 2026-05-06T12:21:44.083Z
Learnt from: juliajforesti
Repo: RocketChat/Rocket.Chat PR: 40256
File: apps/meteor/client/components/CreateDiscussion/CreateDiscussion.tsx:121-149
Timestamp: 2026-05-06T12:21:44.083Z
Learning: Field wrappers in rocket.chat/fuselage-forms (Field, FieldLabel, FieldRow, FieldError, FieldHint) auto-create htmlFor/id associations, aria-describedby, and role="alert" for errors. Do not manually set htmlFor, id, aria-describedby, or role attributes when using these wrappers. This automatic wiring does not apply to plain rocket.chat/fuselage components, which require explicit ID wiring per the accessibility docs. In code reviews, prefer using fuselage-forms wrappers for form fields and verify there is no unnecessary manual ID/aria wiring in files that use these wrappers. If a component uses plain fuselage components, ensure proper id wiring as per docs.

Applied to files:

  • ee/apps/presence-service/src/service.ts
  • packages/models/src/index.ts
  • ee/packages/presence/src/Presence.ts
🔇 Additional comments (7)
packages/models/src/index.ts (1)

111-111: LGTM!

Also applies to: 249-249

ee/packages/presence/src/Presence.ts (3)

7-18: LGTM!


182-186: 🩺 Stability & Availability

Don’t remove the shared STATUS_EXPIRATION_JOB here unless it is instance-scoped. If that cron name is shared, one shutdown will stop expiration processing for the other running presence instances until something reschedules it.


53-53: 🩺 Stability & Availability

Guard the remove/add reschedule path. If setupNextExpiration() can overlap with a later call, serialize the cron mutation and re-check the token before removing or re-adding the job.

ee/packages/presence/package.json (1)

21-21: LGTM!

ee/apps/presence-service/package.json (1)

24-24: LGTM!

ee/apps/presence-service/src/service.ts (1)

2-2: LGTM!

Also applies to: 17-18

Comment thread ee/packages/presence/src/Presence.ts Outdated
@codecov

codecov Bot commented Jul 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 40.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.38%. Comparing base (0faaaeb) to head (a9077ec).
⚠️ Report is 49 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop   #41153      +/-   ##
===========================================
+ Coverage    68.21%   68.38%   +0.16%     
===========================================
  Files         3968     3970       +2     
  Lines       154740   154931     +191     
  Branches     27825    27892      +67     
===========================================
+ Hits        105557   105945     +388     
+ Misses       44392    44244     -148     
+ Partials      4791     4742      -49     
Flag Coverage Δ
e2e 59.22% <ø> (-0.03%) ⬇️
e2e-api 44.04% <ø> (-0.02%) ⬇️
unit 70.29% <40.00%> (-0.36%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ricardogarim ricardogarim added this to the 8.7.0 milestone Jul 2, 2026
@ricardogarim
ricardogarim marked this pull request as ready for review July 3, 2026 12:34
@ricardogarim
ricardogarim requested a review from a team as a code owner July 3, 2026 12:34

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 6 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread ee/packages/presence/src/Presence.ts
@ricardogarim
ricardogarim force-pushed the chore/presence-expiration-cron branch from d7cecad to 66e61a1 Compare July 15, 2026 02:21
Comment thread ee/packages/presence/src/Presence.ts Outdated
Co-authored-by: Kevin Aleman <kaleman960@gmail.com>
@ricardogarim
ricardogarim requested a review from KevLehman July 15, 2026 16:27
@tassoevan tassoevan added the stat: QA assured Means it has been tested and approved by a company insider label Jul 17, 2026
@dionisio-bot dionisio-bot Bot added the stat: ready to merge PR tested and approved waiting for merge label Jul 17, 2026
@dionisio-bot
dionisio-bot Bot added this pull request to the merge queue Jul 17, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 17, 2026
@ricardogarim ricardogarim removed this from the 8.7.0 milestone Jul 17, 2026
@dionisio-bot dionisio-bot Bot removed the stat: ready to merge PR tested and approved waiting for merge label Jul 17, 2026
@ricardogarim ricardogarim added this to the 8.7.0 milestone Jul 17, 2026
@dionisio-bot dionisio-bot Bot added the stat: ready to merge PR tested and approved waiting for merge label Jul 17, 2026
@dionisio-bot
dionisio-bot Bot added this pull request to the merge queue Jul 17, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 17, 2026
@dionisio-bot
dionisio-bot Bot added this pull request to the merge queue Jul 17, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 18, 2026
@dionisio-bot
dionisio-bot Bot added this pull request to the merge queue Jul 18, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 18, 2026
@tassoevan
tassoevan added this pull request to the merge queue Jul 18, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 19, 2026
@dionisio-bot
dionisio-bot Bot added this pull request to the merge queue Jul 19, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 19, 2026
@ricardogarim ricardogarim removed this from the 8.7.0 milestone Jul 20, 2026
@dionisio-bot dionisio-bot Bot removed the stat: ready to merge PR tested and approved waiting for merge label Jul 20, 2026
@ricardogarim ricardogarim added this to the 8.7.0 milestone Jul 20, 2026
@dionisio-bot dionisio-bot Bot added the stat: ready to merge PR tested and approved waiting for merge label Jul 20, 2026
@dionisio-bot
dionisio-bot Bot added this pull request to the merge queue Jul 20, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 20, 2026
@ricardogarim ricardogarim removed this from the 8.7.0 milestone Jul 20, 2026
@dionisio-bot dionisio-bot Bot removed the stat: ready to merge PR tested and approved waiting for merge label Jul 20, 2026
@ricardogarim ricardogarim added this to the 8.7.0 milestone Jul 20, 2026
@dionisio-bot dionisio-bot Bot added the stat: ready to merge PR tested and approved waiting for merge label Jul 20, 2026
@dionisio-bot
dionisio-bot Bot added this pull request to the merge queue Jul 20, 2026
Merged via the queue into develop with commit 82ae946 Jul 20, 2026
84 of 86 checks passed
@dionisio-bot
dionisio-bot Bot deleted the chore/presence-expiration-cron branch July 20, 2026 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stat: QA assured Means it has been tested and approved by a company insider stat: ready to merge PR tested and approved waiting for merge type: chore

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants