diff --git a/services/calendar/pod-calendar/src/calendarController.ts b/services/calendar/pod-calendar/src/calendarController.ts index 18676b487b..563a4240a1 100644 --- a/services/calendar/pod-calendar/src/calendarController.ts +++ b/services/calendar/pod-calendar/src/calendarController.ts @@ -29,6 +29,8 @@ export class CalendarController { WorkspaceClient | Promise >() + private readonly workspacesByEmail = new Map() + private readonly syncedWorkspaces = new Set() private readonly tokens: Collection @@ -95,6 +97,7 @@ export class CalendarController { await limiter.add(async () => { const workspace = await this.startWorkspace(info.workspaceId, tokens) await workspace.sync() + await workspace.close() this.syncedWorkspaces.add(info.workspaceId) }) const newProgress = Math.round((i * 100) / infos.length) @@ -114,6 +117,9 @@ export class CalendarController { }, 60000) console.log('init client', token.workspace, token.userId) await workspaceClient.createCalendarClient(token, true) + const arr = this.workspacesByEmail.get(token.email) ?? [] + arr.push(token.workspace) + this.workspacesByEmail.set(token.email, arr) clearTimeout(timeout) } catch (err) { console.error(`Couldn't create client for ${workspace} ${token.userId} ${token.email}`) @@ -123,30 +129,25 @@ export class CalendarController { } async push (email: string, mode: 'events' | 'calendar', calendarId?: string): Promise { - const tokens = await this.tokens.find({ email, access_token: { $exists: true } }).toArray() - const token = generateToken(systemAccountEmail, { name: '' }) - const workspaces = [...new Set(tokens.map((p) => p.workspace).filter((p) => this.syncedWorkspaces.has(p)))] + const workspaces = this.workspacesByEmail.get(email)?.filter((p) => this.syncedWorkspaces.has(p)) ?? [] if (workspaces.length === 0) return - const infos = await getWorkspacesInfo(token, workspaces) - for (const token of tokens) { - const info = infos.find((p) => p.workspaceId === token.workspace) - if (info === undefined) { - continue + for (const workspace of workspaces) { + const workspaceClient = await this.getWorkspaceClient(workspace) + let calendar = workspaceClient.getCalendarClient(email) + if (calendar !== undefined) { + if (calendar instanceof Promise) { + calendar = await calendar + } + } else { + const token = await this.tokens.findOne({ email, workspace, access_token: { $exists: true } }) + if (token == null) continue + calendar = await workspaceClient.createCalendarClient(token) } - if (isDeletingMode(info.mode)) { - await this.tokens.deleteOne({ userId: token.userId, workspace: token.workspace }) - continue - } - if (!isActiveMode(info.mode)) { - continue - } - const workspace = await this.getWorkspaceClient(token.workspace) - const calendarClient = await workspace.createCalendarClient(token) if (mode === 'calendar') { - await calendarClient.syncCalendars(email) + await calendar.syncCalendars(email) } if (mode === 'events' && calendarId !== undefined) { - await calendarClient.sync(calendarId, email) + await calendar.sync(calendarId, email) } } } diff --git a/services/calendar/pod-calendar/src/googleClient.ts b/services/calendar/pod-calendar/src/googleClient.ts index a07158f948..50738131cc 100644 --- a/services/calendar/pod-calendar/src/googleClient.ts +++ b/services/calendar/pod-calendar/src/googleClient.ts @@ -41,7 +41,7 @@ export class GoogleClient { private refreshTimer: NodeJS.Timeout | undefined = undefined - readonly rateLimiter = new RateLimiter(100, 30) + readonly rateLimiter = new RateLimiter(60 * 1000, 600) constructor ( private readonly user: User, diff --git a/services/calendar/pod-calendar/src/watch.ts b/services/calendar/pod-calendar/src/watch.ts index 6229cc83e1..42323af625 100644 --- a/services/calendar/pod-calendar/src/watch.ts +++ b/services/calendar/pod-calendar/src/watch.ts @@ -143,7 +143,7 @@ export class WatchClient { export class WatchController { private readonly watches: Collection private readonly tokens: Collection - readonly rateLimiter = new RateLimiter(1000, 500) + readonly rateLimiter = new RateLimiter(60 * 1000, 600) private timer: NodeJS.Timeout | undefined = undefined protected static _instance: WatchController diff --git a/services/calendar/pod-calendar/src/workspaceClient.ts b/services/calendar/pod-calendar/src/workspaceClient.ts index ade9e4f5af..29c1d26f93 100644 --- a/services/calendar/pod-calendar/src/workspaceClient.ts +++ b/services/calendar/pod-calendar/src/workspaceClient.ts @@ -174,7 +174,7 @@ export class WorkspaceClient { }, 20000) } - private getCalendarClient (email: string): CalendarClient | Promise | undefined { + getCalendarClient (email: string): CalendarClient | Promise | undefined { return this.clients.get(email) } @@ -246,6 +246,7 @@ export class WorkspaceClient { await client.release() }) } + await limiter.waitProcessing() } // #region Events