Skip to content

Commit 4839c3b

Browse files
authored
Fix calendar freeze (#9286)
Signed-off-by: Denis Bykhov <[email protected]>
1 parent e2c677a commit 4839c3b

File tree

5 files changed

+25
-17
lines changed

5 files changed

+25
-17
lines changed

services/calendar/pod-calendar/src/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export class AuthController {
122122
const secret = await this.accountClient.getIntegrationSecret(data)
123123
if (secret == null) return
124124
const token = JSON.parse(secret.secret)
125-
const watchController = WatchController.get(this.accountClient)
125+
const watchController = WatchController.get(this.ctx, this.accountClient)
126126
await watchController.unsubscribe(token)
127127
await removeIntegrationSecret(this.ctx, this.accountClient, data)
128128
}

services/calendar/pod-calendar/src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ export const main = async (): Promise<void> => {
6161
const accountClient = getAccountClient(getServiceToken())
6262

6363
const pushHandler = new PushHandler(ctx, accountClient)
64+
const watchController = WatchController.get(ctx, accountClient)
65+
6466
const calendarController = CalendarController.getCalendarController(ctx, accountClient)
6567
await calendarController.startAll()
66-
const watchController = WatchController.get(accountClient)
6768
watchController.startCheck()
6869
const endpoints: Endpoint[] = [
6970
{

services/calendar/pod-calendar/src/sync.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export class IncomingSyncManager {
170170

171171
private async sync (calendarId: string): Promise<void> {
172172
await this.syncEvents(calendarId)
173-
const watchController = WatchController.get(this.accountClient)
173+
const watchController = WatchController.get(this.ctx, this.accountClient)
174174
await this.rateLimiter.take(1)
175175
await watchController.addWatch(this.user, this.email, calendarId, this.googleClient)
176176
}
@@ -564,7 +564,7 @@ export class IncomingSyncManager {
564564
async syncCalendars (): Promise<void> {
565565
const history = await getCalendarsSyncHistory(this.user, this.email)
566566
await this.calendarSync(history)
567-
const watchController = WatchController.get(this.accountClient)
567+
const watchController = WatchController.get(this.ctx, this.accountClient)
568568
await this.rateLimiter.take(1)
569569
await watchController.addWatch(this.user, this.email, null, this.googleClient)
570570
}

services/calendar/pod-calendar/src/watch.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AccountClient } from '@hcengineering/account-client'
2-
import { generateId, isActiveMode, PersonId, WorkspaceUuid } from '@hcengineering/core'
2+
import { generateId, isActiveMode, MeasureContext, PersonId, WorkspaceUuid } from '@hcengineering/core'
33
import { Credentials, OAuth2Client } from 'google-auth-library'
44
import { calendar_v3 } from 'googleapis'
55
import config from './config'
@@ -151,15 +151,18 @@ export class WatchController {
151151
private timer: NodeJS.Timeout | undefined = undefined
152152
protected static _instance: WatchController
153153

154-
private constructor (private readonly accountClient: AccountClient) {
155-
console.log('watch started')
154+
private constructor (
155+
private readonly ctx: MeasureContext,
156+
private readonly accountClient: AccountClient
157+
) {
158+
this.ctx.info('watch started')
156159
}
157160

158-
static get (accountClient: AccountClient): WatchController {
161+
static get (ctx: MeasureContext, accountClient: AccountClient): WatchController {
159162
if (WatchController._instance !== undefined) {
160163
return WatchController._instance
161164
}
162-
return new WatchController(accountClient)
165+
return new WatchController(ctx, accountClient)
163166
}
164167

165168
private async getUserWatches (userId: PersonId, workspace: WorkspaceUuid): Promise<Record<string, Watch>> {
@@ -213,7 +216,7 @@ export class WatchController {
213216
toRefresh.push(watch)
214217
}
215218
}
216-
console.log('watch, found for update', toRefresh.length)
219+
this.ctx.info('watch, found for update', { count: toRefresh.length })
217220
if (toRefresh.length === 0) return
218221
const groups = new Map<string, WatchBase[]>()
219222
const workspaces = new Set<WorkspaceUuid>()
@@ -255,7 +258,7 @@ export class WatchController {
255258
await watchClient.subscribe(group)
256259
} catch {}
257260
}
258-
console.log('watch check done')
261+
this.ctx.info('watch check done')
259262
}
260263

261264
async addWatch (
@@ -280,7 +283,12 @@ export class WatchController {
280283
await watchCalendars(user, email, googleClient)
281284
}
282285
} catch (err: any) {
283-
console.error('Watch add error', user.workspace, user.userId, calendarId, err)
286+
this.ctx.error('Watch add error', {
287+
workspace: user.workspace,
288+
user: user.userId,
289+
calendar: calendarId,
290+
err: err.message
291+
})
284292
}
285293
}
286294
}

services/calendar/pod-calendar/src/workspaceClient.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ export class WorkspaceClient {
114114
await client.startSync()
115115
})
116116
}
117-
await limiter.waitProcessing()
118117
}
119118

120119
private async createCalendarClient (user: Token): Promise<CalendarClient | undefined> {
@@ -158,7 +157,7 @@ export class WorkspaceClient {
158157

159158
async pushEvent (user: Token, event: Event, type: 'create' | 'update' | 'delete'): Promise<void> {
160159
const client =
161-
(await this.getCalendarClientByCalendar(event.calendar as Ref<ExternalCalendar>)) ??
160+
this.getCalendarClientByCalendar(event.calendar as Ref<ExternalCalendar>) ??
162161
(await this.createCalendarClient(user))
163162
if (client === undefined) {
164163
console.warn('Client not found', event.calendar, this.workspace)
@@ -185,7 +184,7 @@ export class WorkspaceClient {
185184
const query = lastSync !== undefined ? { modifiedOn: { $gt: lastSync } } : {}
186185
const newEvents = await this.client.findAll(calendar.class.Event, query)
187186
for (const newEvent of newEvents) {
188-
const client = await this.getCalendarClientByCalendar(newEvent.calendar as Ref<ExternalCalendar>)
187+
const client = this.getCalendarClientByCalendar(newEvent.calendar as Ref<ExternalCalendar>)
189188
if (client === undefined) {
190189
this.ctx.warn('Client not found', { calendar: newEvent.calendar, workspace: this.workspace })
191190
continue
@@ -196,10 +195,10 @@ export class WorkspaceClient {
196195
this.ctx.info('all outcoming messages synced', { workspace: this.workspace })
197196
}
198197

199-
private async getCalendarClientByCalendar (id: Ref<ExternalCalendar>): Promise<CalendarClient | undefined> {
198+
private getCalendarClientByCalendar (id: Ref<ExternalCalendar>): CalendarClient | undefined {
200199
const calendar = this.calendarsById.get(id)
201200
if (calendar === undefined) {
202-
console.warn("couldn't find calendar by id", id)
201+
this.ctx.warn("couldn't find calendar by id", { id })
203202
return
204203
}
205204
return this.clients.get(calendar.externalUser as GoogleEmail)

0 commit comments

Comments
 (0)