Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion ee/packages/presence/src/Presence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export class Presence extends ServiceClass implements IPresence {

private expirationTimeout?: NodeJS.Timeout;

private expirationScheduleToken?: symbol;

constructor() {
super();

Expand Down Expand Up @@ -131,10 +133,19 @@ export class Presence extends ServiceClass implements IPresence {
}

private async setupNextExpiration(): Promise<void> {
const token = Symbol();
this.expirationScheduleToken = token;

const next = await Users.findNextStatusExpiration();
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.

// A newer reschedule replaced our token while we awaited the lookup; let it arm the timer.
if (this.expirationScheduleToken !== token) {
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
return;
}

clearTimeout(this.expirationTimeout);
this.expirationTimeout = undefined;

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
const next = await Users.findNextStatusExpiration();
if (!next?.statusExpiresAt) {
return;
}
Expand Down Expand Up @@ -174,7 +185,9 @@ export class Presence extends ServiceClass implements IPresence {

override async stopped(): Promise<void> {
this.reaper.stop();
this.expirationScheduleToken = undefined;
clearTimeout(this.expirationTimeout);
this.expirationTimeout = undefined;
clearTimeout(this.lostConTimeout);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/models/src/models/Users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ export class UsersRaw extends BaseRaw<IUser, DefaultFields<IUser>> implements IU

findNextStatusExpiration() {
return this.findOne<Pick<IUser, '_id' | 'statusExpiresAt'>>(
{ statusExpiresAt: { $gte: new Date() } },
{ statusExpiresAt: { $exists: true } },
{ projection: { _id: 1, statusExpiresAt: 1 }, sort: { statusExpiresAt: 1 } },
);
}
Expand Down
Loading