From fed9ef4e5aa964c9c535e4100b2da1eac5e2f4cb Mon Sep 17 00:00:00 2001 From: Artyom Savchenko Date: Wed, 23 Jul 2025 19:05:01 +0700 Subject: [PATCH] Fix duplicated gmail sync on startup (#9590) Signed-off-by: Artem Savchenko --- services/gmail/pod-gmail/src/gmail.ts | 41 +++++++++++++++---- .../gmail/pod-gmail/src/workspaceClient.ts | 8 +++- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/services/gmail/pod-gmail/src/gmail.ts b/services/gmail/pod-gmail/src/gmail.ts index 5e479e25b7..672950bd6c 100644 --- a/services/gmail/pod-gmail/src/gmail.ts +++ b/services/gmail/pod-gmail/src/gmail.ts @@ -102,6 +102,7 @@ export class GmailClient { private readonly syncManager: SyncManager private readonly integrationToken: string private integration: Integration | undefined = undefined + private syncStarted: boolean = false private constructor ( private readonly ctx: MeasureContext, @@ -388,13 +389,34 @@ export class GmailClient { } async startSync (): Promise { - this.ctx.info('Start sync', { workspaceUuid: this.user.workspace, userId: this.user.userId, email: this.email }) - await this.syncManager.sync(this.socialId._id, { noNotify: true }, this.email) - await this.watch() - // recall every 24 hours https://developers.google.com/gmail/api/guides/push - this.watchTimer = setInterval(() => { - void this.watch() - }, 86400000) + if (this.syncStarted) { + this.ctx.info('Sync already started, skipping duplicate call', { + workspaceUuid: this.user.workspace, + userId: this.user.userId, + email: this.email + }) + return + } + + try { + this.syncStarted = true + this.ctx.info('Start sync', { workspaceUuid: this.user.workspace, userId: this.user.userId, email: this.email }) + await this.syncManager.sync(this.socialId._id, { noNotify: true }, this.email) + await this.watch() + // recall every 24 hours https://developers.google.com/gmail/api/guides/push + if (this.watchTimer !== undefined) clearInterval(this.watchTimer) + this.watchTimer = setInterval(() => { + void this.watch() + }, 86400000) + } catch (err: any) { + this.ctx.error('Failed to start sync', { + workspaceUuid: this.user.workspace, + userId: this.user.userId, + email: this.email, + error: err.message + }) + this.syncStarted = false + } } async sync (options: SyncOptions): Promise { @@ -533,6 +555,11 @@ export class GmailClient { topicName: config.WATCH_TOPIC_NAME } }) + this.ctx.info('Gmail watch established successfully', { + workspaceUuid: this.user.workspace, + userId: this.user.userId, + topicName: config.WATCH_TOPIC_NAME + }) } catch (err) { this.ctx.error('Watch error', { workspaceUuid: this.user.workspace, diff --git a/services/gmail/pod-gmail/src/workspaceClient.ts b/services/gmail/pod-gmail/src/workspaceClient.ts index 3f50d0704b..cdd464228d 100644 --- a/services/gmail/pod-gmail/src/workspaceClient.ts +++ b/services/gmail/pod-gmail/src/workspaceClient.ts @@ -70,8 +70,12 @@ export class WorkspaceClient { async createGmailClient (user: User, authCode?: string): Promise { const current = user.socialId?._id !== undefined ? this.getGmailClient(user.socialId?._id) : undefined if (current !== undefined) return current - this.ctx.info('Creating new gmail client', { workspaceUuid: this.workspace, userId: user.userId }) - this.ctx.info('Creating new gmail user', { user }) + this.ctx.info('Creating new gmail client', { + workspaceUuid: this.workspace, + userId: user.userId, + email: user.email, + socialId: user.socialId?._id + }) const newClient = await GmailClient.create( this.ctx, this.credentials,