[messaging] add better logs to messaging sync jobs (#4245)

This commit is contained in:
Weiko
2024-02-29 17:30:42 +01:00
committed by GitHub
parent 30df6c10ea
commit 8a669cc540
6 changed files with 45 additions and 25 deletions

View File

@ -60,7 +60,9 @@ export class ConnectedAccountService {
); );
if (!connectedAccounts || connectedAccounts.length === 0) { 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]; return connectedAccounts[0];

View File

@ -38,7 +38,9 @@ export class MessageChannelService {
); );
if (!messageChannels || messageChannels.length === 0) { 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]; return messageChannels[0];

View File

@ -44,7 +44,9 @@ export class WorkspaceMemberService {
); );
if (!workspaceMembers || workspaceMembers.length === 0) { 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]; return workspaceMembers[0];

View File

@ -47,7 +47,9 @@ export class GmailFullSyncService {
const workspaceMemberId = connectedAccount.accountOwnerId; const workspaceMemberId = connectedAccount.accountOwnerId;
if (!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 full-sync`,
);
} }
const gmailMessageChannel = const gmailMessageChannel =
@ -149,33 +151,36 @@ export class GmailFullSyncService {
}ms.`, }ms.`,
); );
if (messagesToSave.length === 0) { if (messagesToSave.length > 0) {
if (errors.length) throw new Error('Error fetching messages'); this.saveMessagesAndCreateContactsService.saveMessagesAndCreateContacts(
messagesToSave,
connectedAccount,
workspaceId,
gmailMessageChannelId,
'gmail full-sync',
);
} else {
this.logger.log( this.logger.log(
`gmail full-sync for workspace ${workspaceId} and account ${connectedAccountId} done with nothing to import.`, `gmail full-sync for workspace ${workspaceId} and account ${connectedAccountId} done with nothing to import.`,
); );
return;
} }
this.saveMessagesAndCreateContactsService.saveMessagesAndCreateContacts( if (errors.length) {
messagesToSave, throw new Error(
connectedAccount, `Error fetching messages for ${connectedAccountId} in workspace ${workspaceId} during full-sync`,
workspaceId, );
gmailMessageChannelId, }
'gmail full-sync',
);
if (errors.length) throw new Error('Error fetching messages');
const lastModifiedMessageId = messagesToFetch[0]; const lastModifiedMessageId = messagesToFetch[0];
const historyId = messagesToSave.find( const historyId = messagesToSave.find(
(message) => message.externalId === lastModifiedMessageId, (message) => message.externalId === lastModifiedMessageId,
)?.historyId; )?.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(); startTime = Date.now();

View File

@ -61,7 +61,9 @@ export class GmailPartialSyncService {
const refreshToken = connectedAccount.refreshToken; const refreshToken = connectedAccount.refreshToken;
if (!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(); let startTime = Date.now();
@ -96,7 +98,9 @@ export class GmailPartialSyncService {
} }
if (!historyId) { 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) { 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(); startTime = Date.now();
await this.connectedAccountService.updateLastSyncHistoryId( await this.connectedAccountService.updateLastSyncHistoryId(

View File

@ -24,7 +24,9 @@ export class GmailRefreshAccessTokenService {
const refreshToken = connectedAccount.refreshToken; const refreshToken = connectedAccount.refreshToken;
if (!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); const accessToken = await this.refreshAccessToken(refreshToken);