Skip to content

Commit b858dd7

Browse files
authored
One more calendar fix (#8110)
Signed-off-by: Denis Bykhov <[email protected]>
1 parent 144b11d commit b858dd7

File tree

4 files changed

+24
-22
lines changed

4 files changed

+24
-22
lines changed

Diff for: services/calendar/pod-calendar/src/calendarController.ts

+20-19
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export class CalendarController {
2929
WorkspaceClient | Promise<WorkspaceClient>
3030
>()
3131

32+
private readonly workspacesByEmail = new Map<string, string[]>()
33+
3234
private readonly syncedWorkspaces = new Set<string>()
3335

3436
private readonly tokens: Collection<Token>
@@ -95,6 +97,7 @@ export class CalendarController {
9597
await limiter.add(async () => {
9698
const workspace = await this.startWorkspace(info.workspaceId, tokens)
9799
await workspace.sync()
100+
await workspace.close()
98101
this.syncedWorkspaces.add(info.workspaceId)
99102
})
100103
const newProgress = Math.round((i * 100) / infos.length)
@@ -114,6 +117,9 @@ export class CalendarController {
114117
}, 60000)
115118
console.log('init client', token.workspace, token.userId)
116119
await workspaceClient.createCalendarClient(token, true)
120+
const arr = this.workspacesByEmail.get(token.email) ?? []
121+
arr.push(token.workspace)
122+
this.workspacesByEmail.set(token.email, arr)
117123
clearTimeout(timeout)
118124
} catch (err) {
119125
console.error(`Couldn't create client for ${workspace} ${token.userId} ${token.email}`)
@@ -123,30 +129,25 @@ export class CalendarController {
123129
}
124130

125131
async push (email: string, mode: 'events' | 'calendar', calendarId?: string): Promise<void> {
126-
const tokens = await this.tokens.find({ email, access_token: { $exists: true } }).toArray()
127-
const token = generateToken(systemAccountEmail, { name: '' })
128-
const workspaces = [...new Set(tokens.map((p) => p.workspace).filter((p) => this.syncedWorkspaces.has(p)))]
132+
const workspaces = this.workspacesByEmail.get(email)?.filter((p) => this.syncedWorkspaces.has(p)) ?? []
129133
if (workspaces.length === 0) return
130-
const infos = await getWorkspacesInfo(token, workspaces)
131-
for (const token of tokens) {
132-
const info = infos.find((p) => p.workspaceId === token.workspace)
133-
if (info === undefined) {
134-
continue
135-
}
136-
if (isDeletingMode(info.mode)) {
137-
await this.tokens.deleteOne({ userId: token.userId, workspace: token.workspace })
138-
continue
139-
}
140-
if (!isActiveMode(info.mode)) {
141-
continue
134+
for (const workspace of workspaces) {
135+
const workspaceClient = await this.getWorkspaceClient(workspace)
136+
let calendar = workspaceClient.getCalendarClient(email)
137+
if (calendar !== undefined) {
138+
if (calendar instanceof Promise) {
139+
calendar = await calendar
140+
}
141+
} else {
142+
const token = await this.tokens.findOne({ email, workspace, access_token: { $exists: true } })
143+
if (token == null) continue
144+
calendar = await workspaceClient.createCalendarClient(token)
142145
}
143-
const workspace = await this.getWorkspaceClient(token.workspace)
144-
const calendarClient = await workspace.createCalendarClient(token)
145146
if (mode === 'calendar') {
146-
await calendarClient.syncCalendars(email)
147+
await calendar.syncCalendars(email)
147148
}
148149
if (mode === 'events' && calendarId !== undefined) {
149-
await calendarClient.sync(calendarId, email)
150+
await calendar.sync(calendarId, email)
150151
}
151152
}
152153
}

Diff for: services/calendar/pod-calendar/src/googleClient.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class GoogleClient {
4141

4242
private refreshTimer: NodeJS.Timeout | undefined = undefined
4343

44-
readonly rateLimiter = new RateLimiter(100, 30)
44+
readonly rateLimiter = new RateLimiter(60 * 1000, 600)
4545

4646
constructor (
4747
private readonly user: User,

Diff for: services/calendar/pod-calendar/src/watch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export class WatchClient {
143143
export class WatchController {
144144
private readonly watches: Collection<Watch>
145145
private readonly tokens: Collection<Token>
146-
readonly rateLimiter = new RateLimiter(1000, 500)
146+
readonly rateLimiter = new RateLimiter(60 * 1000, 600)
147147

148148
private timer: NodeJS.Timeout | undefined = undefined
149149
protected static _instance: WatchController

Diff for: services/calendar/pod-calendar/src/workspaceClient.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export class WorkspaceClient {
174174
}, 20000)
175175
}
176176

177-
private getCalendarClient (email: string): CalendarClient | Promise<CalendarClient> | undefined {
177+
getCalendarClient (email: string): CalendarClient | Promise<CalendarClient> | undefined {
178178
return this.clients.get(email)
179179
}
180180

@@ -246,6 +246,7 @@ export class WorkspaceClient {
246246
await client.release()
247247
})
248248
}
249+
await limiter.waitProcessing()
249250
}
250251

251252
// #region Events

0 commit comments

Comments
 (0)