From 8a669cc540ac3e108c4b72e60eb243f1c905d044 Mon Sep 17 00:00:00 2001 From: Weiko Date: Thu, 29 Feb 2024 17:30:42 +0100 Subject: [PATCH] [messaging] add better logs to messaging sync jobs (#4245) --- .../connected-account.service.ts | 4 +- .../message-channel.service.ts | 4 +- .../workspace-member.service.ts | 4 +- .../services/gmail-full-sync.service.ts | 39 +++++++++++-------- .../services/gmail-partial-sync.service.ts | 15 +++++-- .../gmail-refresh-access-token.service.ts | 4 +- 6 files changed, 45 insertions(+), 25 deletions(-) diff --git a/packages/twenty-server/src/workspace/messaging/repositories/connected-account/connected-account.service.ts b/packages/twenty-server/src/workspace/messaging/repositories/connected-account/connected-account.service.ts index 5137d4bea..c20fd9529 100644 --- a/packages/twenty-server/src/workspace/messaging/repositories/connected-account/connected-account.service.ts +++ b/packages/twenty-server/src/workspace/messaging/repositories/connected-account/connected-account.service.ts @@ -60,7 +60,9 @@ export class ConnectedAccountService { ); if (!connectedAccounts || connectedAccounts.length === 0) { - throw new NotFoundException('No connected account found'); + throw new NotFoundException( + `No connected account found for id ${connectedAccountId} in workspace ${workspaceId}`, + ); } return connectedAccounts[0]; diff --git a/packages/twenty-server/src/workspace/messaging/repositories/message-channel/message-channel.service.ts b/packages/twenty-server/src/workspace/messaging/repositories/message-channel/message-channel.service.ts index 60e635a8b..1ca85c5b1 100644 --- a/packages/twenty-server/src/workspace/messaging/repositories/message-channel/message-channel.service.ts +++ b/packages/twenty-server/src/workspace/messaging/repositories/message-channel/message-channel.service.ts @@ -38,7 +38,9 @@ export class MessageChannelService { ); if (!messageChannels || messageChannels.length === 0) { - throw new Error('No message channel found'); + throw new Error( + `No message channel found for connected account ${connectedAccountId} in workspace ${workspaceId}`, + ); } return messageChannels[0]; diff --git a/packages/twenty-server/src/workspace/messaging/repositories/workspace-member/workspace-member.service.ts b/packages/twenty-server/src/workspace/messaging/repositories/workspace-member/workspace-member.service.ts index ab39b2764..42b670599 100644 --- a/packages/twenty-server/src/workspace/messaging/repositories/workspace-member/workspace-member.service.ts +++ b/packages/twenty-server/src/workspace/messaging/repositories/workspace-member/workspace-member.service.ts @@ -44,7 +44,9 @@ export class WorkspaceMemberService { ); if (!workspaceMembers || workspaceMembers.length === 0) { - throw new NotFoundException('No workspace member found'); + throw new NotFoundException( + `No workspace member found for user ${userId} in workspace ${workspaceId}`, + ); } return workspaceMembers[0]; diff --git a/packages/twenty-server/src/workspace/messaging/services/gmail-full-sync.service.ts b/packages/twenty-server/src/workspace/messaging/services/gmail-full-sync.service.ts index dd8cace8d..fd4d81391 100644 --- a/packages/twenty-server/src/workspace/messaging/services/gmail-full-sync.service.ts +++ b/packages/twenty-server/src/workspace/messaging/services/gmail-full-sync.service.ts @@ -47,7 +47,9 @@ export class GmailFullSyncService { const workspaceMemberId = connectedAccount.accountOwnerId; if (!refreshToken) { - throw new Error('No refresh token found'); + throw new Error( + `No refresh token found for connected account ${connectedAccountId} in workspace ${workspaceId} during full-sync`, + ); } const gmailMessageChannel = @@ -149,33 +151,36 @@ export class GmailFullSyncService { }ms.`, ); - if (messagesToSave.length === 0) { - if (errors.length) throw new Error('Error fetching messages'); - + if (messagesToSave.length > 0) { + this.saveMessagesAndCreateContactsService.saveMessagesAndCreateContacts( + messagesToSave, + connectedAccount, + workspaceId, + gmailMessageChannelId, + 'gmail full-sync', + ); + } else { this.logger.log( `gmail full-sync for workspace ${workspaceId} and account ${connectedAccountId} done with nothing to import.`, ); - - return; } - this.saveMessagesAndCreateContactsService.saveMessagesAndCreateContacts( - messagesToSave, - connectedAccount, - workspaceId, - gmailMessageChannelId, - 'gmail full-sync', - ); - - if (errors.length) throw new Error('Error fetching messages'); - + if (errors.length) { + throw new Error( + `Error fetching messages for ${connectedAccountId} in workspace ${workspaceId} during full-sync`, + ); + } const lastModifiedMessageId = messagesToFetch[0]; const historyId = messagesToSave.find( (message) => message.externalId === lastModifiedMessageId, )?.historyId; - if (!historyId) throw new Error('No history id found'); + if (!historyId) { + throw new Error( + `No historyId found for ${connectedAccountId} in workspace ${workspaceId} during full-sync`, + ); + } startTime = Date.now(); diff --git a/packages/twenty-server/src/workspace/messaging/services/gmail-partial-sync.service.ts b/packages/twenty-server/src/workspace/messaging/services/gmail-partial-sync.service.ts index fbd03010d..889346d4a 100644 --- a/packages/twenty-server/src/workspace/messaging/services/gmail-partial-sync.service.ts +++ b/packages/twenty-server/src/workspace/messaging/services/gmail-partial-sync.service.ts @@ -61,7 +61,9 @@ export class GmailPartialSyncService { const refreshToken = connectedAccount.refreshToken; if (!refreshToken) { - throw new Error('No refresh token found'); + throw new Error( + `No refresh token found for connected account ${connectedAccountId} in workspace ${workspaceId} during partial-sync`, + ); } let startTime = Date.now(); @@ -96,7 +98,9 @@ export class GmailPartialSyncService { } if (!historyId) { - throw new Error('No history id found'); + throw new Error( + `No historyId found for ${connectedAccountId} in workspace ${workspaceId} during partial-sync`, + ); } if (historyId === lastSyncHistoryId || !history?.length) { @@ -173,8 +177,11 @@ export class GmailPartialSyncService { ); } - if (errors.length) throw new Error('Error fetching messages'); - + if (errors.length) { + throw new Error( + `Error fetching messages for ${connectedAccountId} in workspace ${workspaceId} during partial-sync`, + ); + } startTime = Date.now(); await this.connectedAccountService.updateLastSyncHistoryId( diff --git a/packages/twenty-server/src/workspace/messaging/services/gmail-refresh-access-token.service.ts b/packages/twenty-server/src/workspace/messaging/services/gmail-refresh-access-token.service.ts index eeb25de9a..7a01b663b 100644 --- a/packages/twenty-server/src/workspace/messaging/services/gmail-refresh-access-token.service.ts +++ b/packages/twenty-server/src/workspace/messaging/services/gmail-refresh-access-token.service.ts @@ -24,7 +24,9 @@ export class GmailRefreshAccessTokenService { const refreshToken = connectedAccount.refreshToken; if (!refreshToken) { - throw new Error('No refresh token found'); + throw new Error( + `No refresh token found for connected account ${connectedAccountId} in workspace ${workspaceId}`, + ); } const accessToken = await this.refreshAccessToken(refreshToken);