Skip to content

Commit fb65165

Browse files
authored
qfix: connect timeout + service ws info cache (#9622)
1. Fix connect timeout in case of maitenance 2. Introduce workspace info cache for service accounts 3. Fix null in github integration Signed-off-by: Andrey Sobolev <[email protected]>
1 parent 6c6436d commit fb65165

File tree

4 files changed

+72
-10
lines changed

4 files changed

+72
-10
lines changed

plugins/client-resources/src/index.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ import core, {
3636
type PluginConfiguration,
3737
type Ref,
3838
type TxCUD,
39-
platformNow
39+
platformNow,
40+
ClientConnectEvent
4041
} from '@hcengineering/core'
4142
import platform, { Severity, Status, getMetadata, getPlugins, setPlatformStatus } from '@hcengineering/platform'
4243
import { connect } from './connection'
@@ -140,10 +141,20 @@ export default async () => {
140141
}
141142
}, connectTimeout)
142143
newOpt.onConnect = async (event, lastTx, data) => {
143-
// Any event is fine, it means server is alive.
144-
clearTimeout(connectTO)
145-
await opt?.onConnect?.(event, lastTx, data)
146-
resolve()
144+
try {
145+
await opt?.onConnect?.(event, lastTx, data)
146+
} catch (error) {
147+
void clientConnection?.close()
148+
void opt?.onDialTimeout?.()
149+
reject(error)
150+
return
151+
}
152+
153+
if (event !== ClientConnectEvent.Maintenance) {
154+
// Any event is fine, it means server is alive.
155+
clearTimeout(connectTO)
156+
resolve()
157+
}
147158
}
148159
})
149160
}

server/server/src/sessionManager.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import {
6363
type AddSessionResponse,
6464
type ClientSessionCtx,
6565
type ConnectionSocket,
66+
type ConsumerHandle,
6667
type GetWorkspaceResponse,
6768
LOGGING_ENABLED,
6869
pingConst,
@@ -72,6 +73,7 @@ import {
7273
type PlatformQueueProducer,
7374
QueueTopic,
7475
type QueueUserMessage,
76+
QueueWorkspaceEvent,
7577
type QueueWorkspaceMessage,
7678
type Session,
7779
type SessionManager,
@@ -116,6 +118,7 @@ export class TSessionManager implements SessionManager {
116118

117119
workspaceProducer: PlatformQueueProducer<QueueWorkspaceMessage>
118120
usersProducer: PlatformQueueProducer<QueueUserMessage>
121+
workspaceConsumer: ConsumerHandle
119122

120123
now: number = Date.now()
121124

@@ -141,8 +144,28 @@ export class TSessionManager implements SessionManager {
141144
this.handleTick()
142145
}, 1000 / ticksPerSecond)
143146
}
144-
this.workspaceProducer = this.queue.getProducer(ctx.newChild('queue', {}, { span: false }), QueueTopic.Workspace)
145-
this.usersProducer = this.queue.getProducer(ctx.newChild('queue', {}, { span: false }), QueueTopic.Users)
147+
this.workspaceProducer = this.queue.getProducer(ctx.newChild('ws-queue', {}, { span: false }), QueueTopic.Workspace)
148+
this.usersProducer = this.queue.getProducer(ctx.newChild('user-queue', {}, { span: false }), QueueTopic.Users)
149+
150+
this.workspaceConsumer = this.queue.createConsumer<QueueWorkspaceMessage>(
151+
ctx.newChild('ws-queue-consume', {}, { span: false }),
152+
QueueTopic.Workspace,
153+
generateId(),
154+
async (messages) => {
155+
for (const msg of messages) {
156+
for (const m of msg.value) {
157+
if (
158+
m.type === QueueWorkspaceEvent.Upgraded ||
159+
m.type === QueueWorkspaceEvent.Restored ||
160+
m.type === QueueWorkspaceEvent.Deleted
161+
) {
162+
// Handle workspace messages
163+
this.workspaceInfoCache.delete(msg.workspace)
164+
}
165+
}
166+
}
167+
}
168+
)
146169

147170
this.ticksContext = ctx.newChild('ticks', {}, { span: false })
148171
}
@@ -523,6 +546,8 @@ export class TSessionManager implements SessionManager {
523546

524547
maintenanceWorkspaces = new Set<WorkspaceUuid>()
525548

549+
workspaceInfoCache = new Map<WorkspaceUuid, WorkspaceInfoWithStatus>()
550+
526551
async addSession (
527552
ctx: MeasureContext,
528553
ws: ConnectionSocket,
@@ -552,10 +577,13 @@ export class TSessionManager implements SessionManager {
552577
if (wsInfo === undefined) {
553578
// In case of guest or system account
554579
// We need to get workspace info for system account.
555-
const workspaceInfo = await this.getWorkspaceInfo(ctx, rawToken, false)
580+
const workspaceInfo =
581+
this.workspaceInfoCache.get(token.workspace) ?? (await this.getWorkspaceInfo(ctx, rawToken, false))
556582
if (workspaceInfo === undefined) {
557583
return { error: new Error('Workspace not found or not available'), terminate: true }
558584
}
585+
this.workspaceInfoCache.set(token.workspace, workspaceInfo)
586+
559587
wsInfo = {
560588
url: workspaceInfo.url,
561589
mode: workspaceInfo.mode,
@@ -569,6 +597,8 @@ export class TSessionManager implements SessionManager {
569597
endpoint: { externalUrl: '', internalUrl: '', region: workspaceInfo.region ?? '' },
570598
progress: workspaceInfo.processingProgress
571599
}
600+
} else {
601+
this.workspaceInfoCache.delete(token.workspace)
572602
}
573603
const { workspace, resp } = await this.getWorkspace(ctx.parent ?? ctx, token.workspace, wsInfo, token, ws)
574604
if (resp !== undefined) {
@@ -961,6 +991,7 @@ export class TSessionManager implements SessionManager {
961991
async forceClose (wsId: WorkspaceUuid, ignoreSocket?: ConnectionSocket): Promise<void> {
962992
const ws = this.workspaces.get(wsId)
963993
this.maintenanceWorkspaces.delete(wsId)
994+
this.workspaceInfoCache.delete(wsId)
964995
if (ws !== undefined) {
965996
this.ctx.warn('force-close', { name: ws.wsId.url })
966997
ws.maintenance = true // We need to similare upgrade to refresh all clients.

services/github/pod-github/src/platform.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ export class PlatformWorker {
134134
workspace: i.workspaceUuid,
135135
installationId: Array.isArray(installationId) ? installationId : [installationId]
136136
})
137+
} else {
138+
ctx.warn('Integration without installationId', {
139+
accountId: i.socialId,
140+
workspace: i.workspaceUuid
141+
})
142+
await accountsClient.deleteIntegration({
143+
kind: 'github',
144+
workspaceUuid: i.workspaceUuid,
145+
socialId: i.socialId
146+
})
137147
}
138148
}
139149

@@ -1076,9 +1086,9 @@ export class PlatformWorker {
10761086
index: widx,
10771087
total: workspaces.length
10781088
})
1079-
// No if no integration, we will try connect one more time in a time period
10801089
this.clients.set(workspace, worker)
10811090
} else {
1091+
// No if no integration, we will try connect one more time in a time period
10821092
workerCtx.info(
10831093
'************************* Failed Register worker, timeout or integrations removed *************************',
10841094
{

services/github/pod-github/src/worker.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1753,8 +1753,14 @@ export class GithubWorker implements IntegrationManager {
17531753
ctx.info('Connecting to', { workspace })
17541754
let client: Client | undefined
17551755
let endpoint: string | undefined
1756+
let maitenanceState = false
17561757
try {
17571758
;({ client, endpoint } = await createPlatformClient(workspace.uuid, 30000, async (event: ClientConnectEvent) => {
1759+
if (event === ClientConnectEvent.Maintenance) {
1760+
await client?.close()
1761+
maitenanceState = true
1762+
throw new Error('Workspace in maintenance')
1763+
}
17581764
reconnect(workspace.uuid, event)
17591765
}))
17601766
ctx.info('connected to github', { workspace: workspace.uuid, endpoint })
@@ -1781,8 +1787,12 @@ export class GithubWorker implements IntegrationManager {
17811787
void worker.init()
17821788
return worker
17831789
} catch (err: any) {
1784-
ctx.error('timeout during to connect', { workspace, error: err })
17851790
await client?.close()
1791+
if (maitenanceState) {
1792+
ctx.info('workspace in maintenance, schedule recheck', { workspace: workspace.uuid, endpoint })
1793+
return
1794+
}
1795+
ctx.error('timeout during to connect', { workspace, error: err })
17861796
return undefined
17871797
}
17881798
}

0 commit comments

Comments
 (0)