From 2ad63ebeea4ab3c7b0bd96b5401d7845007f1773 Mon Sep 17 00:00:00 2001 From: Denis Bykhov Date: Thu, 26 Jun 2025 16:54:36 +0500 Subject: [PATCH] Extra calendar logs (#9390) Signed-off-by: Denis Bykhov --- .../calendar/pod-calendar/src/pushHandler.ts | 40 +++++++----- services/calendar/pod-calendar/src/sync.ts | 65 ++++++++++++++----- services/calendar/pod-calendar/src/watch.ts | 10 +++ 3 files changed, 84 insertions(+), 31 deletions(-) diff --git a/services/calendar/pod-calendar/src/pushHandler.ts b/services/calendar/pod-calendar/src/pushHandler.ts index 2299f26e4c..b421f05e92 100644 --- a/services/calendar/pod-calendar/src/pushHandler.ts +++ b/services/calendar/pod-calendar/src/pushHandler.ts @@ -14,7 +14,7 @@ // import { AccountClient } from '@hcengineering/account-client' -import core, { isActiveMode, MeasureContext, TxOperations } from '@hcengineering/core' +import { isActiveMode, MeasureContext, TxOperations } from '@hcengineering/core' import { getClient } from './client' import { getUserByEmail, removeUserByEmail } from './kvsUtils' import { IncomingSyncManager } from './sync' @@ -32,22 +32,31 @@ export class PushHandler { 'Push handler', {}, async () => { - const client = await getClient(getWorkspaceToken(token.workspace)) - const txOp = new TxOperations(client, core.account.System) - const res = getGoogleClient() - const authSuccess = await setCredentials(res.auth, token) - if (!authSuccess) { - await removeUserByEmail(token, token.email) - await removeIntegrationSecret(this.ctx, this.accountClient, { - kind: CALENDAR_INTEGRATION, - workspaceUuid: token.workspace, - socialId: token.userId, - key: token.email + try { + const client = await getClient(getWorkspaceToken(token.workspace)) + const res = getGoogleClient() + const authSuccess = await setCredentials(res.auth, token) + if (!authSuccess) { + await removeUserByEmail(token, token.email) + await removeIntegrationSecret(this.ctx, this.accountClient, { + kind: CALENDAR_INTEGRATION, + workspaceUuid: token.workspace, + socialId: token.userId, + key: token.email + }) + } else { + const txOp = new TxOperations(client, token.userId) + await IncomingSyncManager.push(this.ctx, this.accountClient, txOp, token, res.google, calendarId) + await txOp.close() + } + } catch (err) { + this.ctx.error('Push sync error', { + user: token.userId, + workspace: token.workspace, + email: token.email, + error: err instanceof Error ? err.message : String(err) }) - } else { - await IncomingSyncManager.push(this.ctx, this.accountClient, txOp, token, res.google, calendarId) } - await txOp.close() }, { workspace: token.workspace, user: token.userId } ) @@ -55,6 +64,7 @@ export class PushHandler { async push (email: GoogleEmail, mode: 'events' | 'calendar', calendarId?: string): Promise { const tokens = await getUserByEmail(email) + this.ctx.info('push', { email, mode, calendarId, tokens: tokens.length }) const workspaces = [...new Set(tokens.map((p) => p.workspace))] const infos = await this.accountClient.getWorkspacesInfo(workspaces) for (const token of tokens) { diff --git a/services/calendar/pod-calendar/src/sync.ts b/services/calendar/pod-calendar/src/sync.ts index 60298d3b9c..dca1006553 100644 --- a/services/calendar/pod-calendar/src/sync.ts +++ b/services/calendar/pod-calendar/src/sync.ts @@ -13,6 +13,16 @@ // limitations under the License. // +import { AccountClient } from '@hcengineering/account-client' +import calendar, { + Calendar, + Event, + ExternalCalendar, + ReccuringEvent, + ReccuringInstance, + Visibility +} from '@hcengineering/calendar' +import contact, { Contact, getPersonRefsBySocialIds, Person } from '@hcengineering/contact' import core, { AttachedData, Data, @@ -27,24 +37,14 @@ import core, { TxOperations, TxProcessor } from '@hcengineering/core' -import { getCalendarsSyncHistory, getEventHistory, setCalendarsSyncHistory, setEventHistory } from './kvsUtils' -import { GoogleEmail, Token, User } from './types' -import { calendar_v3 } from 'googleapis' -import { getRateLimitter, RateLimiter } from './rateLimiter' -import calendar, { - Calendar, - Event, - ExternalCalendar, - ReccuringEvent, - ReccuringInstance, - Visibility -} from '@hcengineering/calendar' -import { parseRecurrenceStrings } from './utils' +import setting from '@hcengineering/setting' import { htmlToMarkup } from '@hcengineering/text' import { deepEqual } from 'fast-equals' -import contact, { Contact, getPersonRefsBySocialIds, Person } from '@hcengineering/contact' -import setting from '@hcengineering/setting' -import { AccountClient } from '@hcengineering/account-client' +import { calendar_v3 } from 'googleapis' +import { getCalendarsSyncHistory, getEventHistory, setCalendarsSyncHistory, setEventHistory } from './kvsUtils' +import { getRateLimitter, RateLimiter } from './rateLimiter' +import { GoogleEmail, Token, User } from './types' +import { parseRecurrenceStrings } from './utils' import { WatchController } from './watch' const locks = new Map>() @@ -171,7 +171,19 @@ export class IncomingSyncManager { } private async sync (calendarId: string): Promise { + this.ctx.info('Sync calendar', { + workspace: this.user.workspace, + user: this.user.userId, + emai: this.email, + calendarId + }) await this.syncEvents(calendarId) + this.ctx.info('Sync calendar finished', { + workspace: this.user.workspace, + user: this.user.userId, + emai: this.email, + calendarId + }) const watchController = WatchController.get(this.ctx, this.accountClient) await this.rateLimiter.take(1) await watchController.addWatch(this.user, this.email, calendarId, this.googleClient) @@ -182,11 +194,22 @@ export class IncomingSyncManager { await this.getMyCalendars() await this.syncCalendars() await this.getMyCalendars() + this.ctx.info('Sync started for calendars', { + workspace: this.user.workspace, + user: this.user.userId, + emai: this.email, + count: this.calendars.length + }) for (const calendar of this.calendars) { if (calendar.externalId !== undefined) { await this.sync(calendar.externalId) } } + this.ctx.info('Incoming sync finished', { + workspace: this.user.workspace, + user: this.user.userId, + emai: this.email + }) } catch (err) { this.ctx.error('Start sync error', { workspace: this.user.workspace, user: this.user.userId, err }) } @@ -565,7 +588,17 @@ export class IncomingSyncManager { async syncCalendars (): Promise { const history = await getCalendarsSyncHistory(this.user, this.email) + this.ctx.info('Sync calendars', { + workspace: this.user.workspace, + user: this.user.userId, + email: this.email + }) await this.calendarSync(history) + this.ctx.info('Sync calendars finished', { + workspace: this.user.workspace, + user: this.user.userId, + email: this.email + }) const watchController = WatchController.get(this.ctx, this.accountClient) await this.rateLimiter.take(1) await watchController.addWatch(this.user, this.email, null, this.googleClient) diff --git a/services/calendar/pod-calendar/src/watch.ts b/services/calendar/pod-calendar/src/watch.ts index 41cd05db24..87f6d1544e 100644 --- a/services/calendar/pod-calendar/src/watch.ts +++ b/services/calendar/pod-calendar/src/watch.ts @@ -272,6 +272,11 @@ export class WatchController { const key = `${CALENDAR_INTEGRATION}:watch:${email}:${calendarId ?? 'null'}` const exists = await client.getValue(key) if (exists != null) { + this.ctx.info('Watch already exists', { + workspace: user.workspace, + user: user.userId, + calendar: calendarId + }) return } try { @@ -280,6 +285,11 @@ export class WatchController { } else { await watchCalendars(email, googleClient) } + this.ctx.info('Watch added', { + workspace: user.workspace, + user: user.userId, + calendar: calendarId + }) } catch (err: any) { this.ctx.error('Watch add error', { workspace: user.workspace,