Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UBERF-6653: Rework server connectivity/upgrade ws creation #5418

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions dev/client-resources/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ import core, {
FindOptions,
FindResult,
getWorkspaceId,
MeasureDoneOperation,
MeasureMetricsContext,
Ref,
SearchOptions,
SearchQuery,
SearchResult,
ServerStorage,
Timestamp,
Tx,
TxHandler,
TxResult,
SearchQuery,
SearchOptions,
SearchResult,
MeasureDoneOperation
TxResult
} from '@hcengineering/core'
import { createInMemoryTxAdapter } from '@hcengineering/dev-storage'
import devmodel from '@hcengineering/devmodel'
Expand Down Expand Up @@ -109,6 +109,8 @@ class ServerStorageWrapper implements ClientConnection {
async measure (operationName: string): Promise<MeasureDoneOperation> {
return async () => ({ time: 0, serverTime: 0 })
}

async sendForceClose (): Promise<void> {}
}

async function createNullFullTextAdapter (): Promise<FullTextAdapter> {
Expand Down
3 changes: 2 additions & 1 deletion dev/prod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
"@hcengineering/document-resources": "^0.6.0",
"@hcengineering/guest": "^0.6.0",
"@hcengineering/guest-assets": "^0.6.0",
"@hcengineering/guest-resources": "^0.6.0"
"@hcengineering/guest-resources": "^0.6.0",
"@hcengineering/analytics": "^0.6.0"
}
}
17 changes: 16 additions & 1 deletion dev/prod/src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
//

import { Plugin, addLocation, addStringsLoader, platformId } from '@hcengineering/platform'
import platform, { Plugin, addLocation, addStringsLoader, platformId } from '@hcengineering/platform'

import { activityId } from '@hcengineering/activity'
import { attachmentId } from '@hcengineering/attachment'
Expand Down Expand Up @@ -86,6 +86,8 @@ import { preferenceId } from '@hcengineering/preference'
import { setDefaultLanguage } from '@hcengineering/theme'
import { uiId } from '@hcengineering/ui/src/plugin'

import { Analytics } from '@hcengineering/analytics'

interface Config {
ACCOUNTS_URL: string
UPLOAD_URL: string
Expand Down Expand Up @@ -143,6 +145,19 @@ function configureI18n(): void {
}

export async function configurePlatform() {
setMetadata(platform.metadata.LoadHelper, async (loader) => {
for (let i = 0; i < 3; i++) {
try {
return loader()
} catch (err: any) {
if (err.message.includes('Loading chunk') && i != 2) {
continue
}
Analytics.handleError(err)
}
}
})

configureI18n()

const config: Config = await (await fetch(devConfig? '/config-dev.json' : '/config.json')).json()
Expand Down
7 changes: 3 additions & 4 deletions dev/tool/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import {
replacePassword,
setAccountAdmin,
setRole,
upgradeWorkspace,
UpgradeWorker
UpgradeWorker,
upgradeWorkspace
} from '@hcengineering/account'
import { setMetadata } from '@hcengineering/platform'
import {
Expand Down Expand Up @@ -264,7 +264,7 @@ export function devTool (
.action(async (workspace, cmd) => {
const { mongodbUri, txes, version, migrateOperations } = prepareTools()
await withDatabase(mongodbUri, async (db) => {
const { client } = await createWorkspace(
await createWorkspace(
toolCtx,
version,
txes,
Expand All @@ -275,7 +275,6 @@ export function devTool (
cmd.workspaceName,
workspace
)
await client?.close()
})
})

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/__tests__/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ describe('client', () => {
getAccount: async () => null as unknown as Account,
measure: async () => {
return async () => ({ time: 0, serverTime: 0 })
}
},
sendForceClose: async () => {}
}
}
const spyCreate = jest.spyOn(TxProcessor, 'createDoc2Doc')
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/__tests__/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export async function connect (handler: (tx: Tx) => void): Promise<ClientConnect
clean: async (domain: Domain, docs: Ref<Doc>[]) => {},
loadModel: async (last: Timestamp) => txes,
getAccount: async () => null as unknown as Account,
measure: async () => async () => ({ time: 0, serverTime: 0 })
measure: async () => async () => ({ time: 0, serverTime: 0 }),
sendForceClose: async () => {}
}
}
2 changes: 2 additions & 0 deletions packages/core/src/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ export interface BackupClient {
loadDocs: (domain: Domain, docs: Ref<Doc>[]) => Promise<Doc[]>
upload: (domain: Domain, docs: Doc[]) => Promise<void>
clean: (domain: Domain, docs: Ref<Doc>[]) => Promise<void>

sendForceClose: () => Promise<void>
}
4 changes: 4 additions & 0 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ class ClientImpl implements AccountClient, BackupClient, MeasureClient {
async getAccount (): Promise<Account> {
return await this.conn.getAccount()
}

async sendForceClose (): Promise<void> {
await this.conn.sendForceClose()
}
}

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/platform/src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// limitations under the License.
*/

import { Metadata } from '.'
import { Metadata, PluginLoader, PluginModule, Resources } from '.'

/**
* Id in format 'plugin.resource-kind.id'
Expand Down Expand Up @@ -156,6 +156,7 @@ export default plugin(platformId, {
ProductIdMismatch: '' as StatusCode<{ productId: string }>
},
metadata: {
locale: '' as Metadata<string>
locale: '' as Metadata<string>,
LoadHelper: '' as Metadata<<T extends Resources>(loader: PluginLoader<T>) => Promise<PluginModule<T>>>
}
})
29 changes: 18 additions & 11 deletions packages/platform/src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { _parseId } from './ident'
import type { Plugin, Resource } from './platform'
import { PlatformError, Severity, Status } from './status'

import { getMetadata } from './metadata'
import platform from './platform'

/**
Expand Down Expand Up @@ -77,19 +78,25 @@ async function loadPlugin (id: Plugin): Promise<Resources> {
const status = new Status(Severity.INFO, platform.status.LoadingPlugin, {
plugin: id
})
pluginLoader = monitor(status, getLocation(id)()).then(async (plugin) => {
try {
// In case of ts-node, we have a bit different import structure, so let's check for it.
if (typeof plugin.default === 'object') {
// eslint-disable-next-line @typescript-eslint/return-await
return await (plugin as any).default.default()

const loadHelper = getMetadata(platform.metadata.LoadHelper)

const locationLoader = getLocation(id)
pluginLoader = monitor(status, loadHelper !== undefined ? loadHelper(locationLoader) : locationLoader()).then(
async (plugin) => {
try {
// In case of ts-node, we have a bit different import structure, so let's check for it.
if (typeof plugin.default === 'object') {
// eslint-disable-next-line @typescript-eslint/return-await
return await (plugin as any).default.default()
}
return await plugin.default()
} catch (err: any) {
console.error(err)
throw err
}
return await plugin.default()
} catch (err: any) {
console.error(err)
throw err
}
})
)
loading.set(id, pluginLoader)
}
return await pluginLoader
Expand Down
3 changes: 2 additions & 1 deletion packages/query/src/__tests__/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ FulltextStorage & {
searchFulltext: async (query: SearchQuery, options: SearchOptions): Promise<SearchResult> => {
return { docs: [] }
},
measure: async () => async () => ({ time: 0, serverTime: 0 })
measure: async () => async () => ({ time: 0, serverTime: 0 }),
sendForceClose: async () => {}
}
}
11 changes: 10 additions & 1 deletion packages/ui/src/popups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export interface CompAndProps {
refId?: string
}
dock?: boolean

// Internal
closing?: boolean
}

export interface PopupResult {
Expand Down Expand Up @@ -116,7 +119,13 @@ export function closePopup (category?: string): void {
} else {
for (let i = popups.length - 1; i >= 0; i--) {
if (popups[i].options.fixed !== true) {
popups[i].onClose?.(undefined)
const isClosing = popups[i].closing ?? false
popups[i].closing = true
if (!isClosing) {
// To prevent possible recursion, we need to check if we call some code from popup close, to do close.
popups[i].onClose?.(undefined)
}
popups[i].closing = false
popups.splice(i, 1)
break
}
Expand Down
14 changes: 4 additions & 10 deletions plugins/client-resources/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,12 @@ class Connection implements ClientConnection {
async close (): Promise<void> {
this.closed = true
clearInterval(this.interval)
const closeEvt = serialize(
{
method: 'close',
params: [],
id: -1
},
false
)
if (this.websocket !== null) {
if (this.websocket instanceof Promise) {
await this.websocket.then((ws) => {
ws.send(closeEvt)
ws.close(1000)
})
} else {
this.websocket.send(closeEvt)
this.websocket.close(1000)
}
this.websocket = null
Expand Down Expand Up @@ -547,6 +537,10 @@ class Connection implements ClientConnection {
searchFulltext (query: SearchQuery, options: SearchOptions): Promise<SearchResult> {
return this.sendRequest({ method: 'searchFulltext', params: [query, options] })
}

sendForceClose (): Promise<void> {
return this.sendRequest({ method: 'forceClose', params: [] })
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
} from '@hcengineering/core'
import document, { Teamspace } from '@hcengineering/document'
import { Asset } from '@hcengineering/platform'
import presentation, { Card, getClient } from '@hcengineering/presentation'
import presentation, { Card, getClient, reduceCalls } from '@hcengineering/presentation'
import {
Button,
EditBox,
Expand Down Expand Up @@ -72,7 +72,7 @@
let spaceType: WithLookup<SpaceType> | undefined

$: void loadSpaceType(typeId)
async function loadSpaceType (id: typeof typeId): Promise<void> {
const loadSpaceType = reduceCalls(async (id: typeof typeId): Promise<void> => {
spaceType =
id !== undefined
? await client
Expand All @@ -85,7 +85,7 @@
}

rolesAssignment = getRolesAssignment()
}
})

function getRolesAssignment (): RolesAssignment {
if (teamspace === undefined || spaceType?.targetClass === undefined || spaceType?.$lookup?.roles === undefined) {
Expand Down Expand Up @@ -243,7 +243,10 @@
label={isNew ? documentRes.string.NewTeamspace : documentRes.string.EditTeamspace}
okLabel={isNew ? presentation.string.Create : presentation.string.Save}
okAction={handleSave}
canSave={name.trim().length > 0 && !(members.length === 0 && isPrivate) && typeId !== undefined}
canSave={name.trim().length > 0 &&
!(members.length === 0 && isPrivate) &&
typeId !== undefined &&
spaceType?.targetClass !== undefined}
accentHeader
width={'medium'}
gap={'gapV-6'}
Expand Down
Loading