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

CB-5933 Connection state is not synchronized with other resources triggering connection state #3119

Merged
merged 18 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ enum CBServerEventId {
cb_datasource_folder_updated,
cb_datasource_folder_deleted,

cb_datasource_disconnected,
cb_datasource_connected,

cb_rm_resource_created,
cb_rm_resource_updated,
cb_rm_resource_deleted,
Expand Down Expand Up @@ -53,6 +56,7 @@ enum CBEventTopic {
cb_object_permissions,
cb_subject_permissions,
cb_database_output_log,
cb_datasource_connection,
cb_delete_temp_folder
}

Expand Down Expand Up @@ -178,6 +182,23 @@ type WSOutputLogInfo {
# Add more fields as needed
}

# Datasource disconnect event
type WSDataSourceDisconnectEvent implements CBServerEvent {
id: CBServerEventId!
topicId: CBEventTopic
connectionId: String!
projectId: String!
timestamp: Int!
}
# Datasource connect event
type WSDataSourceConnectEvent implements CBServerEvent {
id: CBServerEventId!
topicId: CBEventTopic
connectionId: String!
projectId: String!
timestamp: Int!
}

extend type Query {
emptyEvent: Boolean
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.dbeaver.model.app.DBPPlatform;
import org.jkiss.dbeaver.model.app.DBPProject;
import org.jkiss.dbeaver.model.websocket.event.datasource.WSDataSourceDisconnectEvent;
import org.jkiss.dbeaver.model.websocket.event.datasource.WSDataSourceEvent;
import org.jkiss.dbeaver.model.websocket.event.datasource.WSDataSourceProperty;
import org.jkiss.dbeaver.runtime.jobs.DataSourceMonitorJob;
Expand Down Expand Up @@ -57,14 +58,21 @@ protected void doJob() {
protected void showNotification(@NotNull DBPDataSource dataSource) {
final DBPProject project = dataSource.getContainer().getProject();
if (project.getWorkspaceSession() instanceof WebSession webSession) {
// TODO: Add new event for disconnect datasource
webSession.addSessionEvent(WSDataSourceEvent.update(
webSession.getSessionId(),
webSession.getUserId(),
project.getId(),
List.of(dataSource.getContainer().getId()),
WSDataSourceProperty.CONFIGURATION
));
webSession.addSessionEvent(
new WSDataSourceDisconnectEvent(
project.getId(),
dataSource.getContainer().getId(),
webSession.getSessionId(),
webSession.getUserId()
)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.jkiss.dbeaver.model.secret.DBSSecretController;
import org.jkiss.dbeaver.model.secret.DBSSecretValue;
import org.jkiss.dbeaver.model.websocket.WSConstants;
import org.jkiss.dbeaver.model.websocket.event.datasource.WSDataSourceConnectEvent;
import org.jkiss.dbeaver.model.websocket.event.datasource.WSDataSourceProperty;
import org.jkiss.dbeaver.registry.DataSourceDescriptor;
import org.jkiss.dbeaver.registry.DataSourceProviderRegistry;
Expand Down Expand Up @@ -366,7 +367,17 @@ public WebConnectionInfo initConnection(

boolean oldSavePassword = dataSourceContainer.isSavePassword();
try {
dataSourceContainer.connect(webSession.getProgressMonitor(), true, false);
boolean connect = dataSourceContainer.connect(webSession.getProgressMonitor(), true, false);
if (connect) {
webSession.addSessionEvent(
new WSDataSourceConnectEvent(
projectId,
connectionId,
webSession.getSessionId(),
webSession.getUserId()
)
);
}
} catch (Exception e) {
throw new DBWebException("Error connecting to database", e);
} finally {
Expand Down
45 changes: 45 additions & 0 deletions webapp/packages/core-connections/src/ConnectionInfoResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { schemaValidationError } from '@cloudbeaver/core-utils';

import { CONNECTION_INFO_PARAM_SCHEMA, type IConnectionInfoParams } from './CONNECTION_INFO_PARAM_SCHEMA.js';
import { ConnectionInfoEventHandler, type IConnectionInfoEvent } from './ConnectionInfoEventHandler.js';
import { ConnectionStateEventHandler, type IWsDataSourceConnectEvent, type IWsDataSourceDisconnectEvent } from './ConnectionStateEventHandler.js';
import type { DatabaseConnection } from './DatabaseConnection.js';
import { DBDriverResource } from './DBDriverResource.js';
import { parseConnectionKey } from './parseConnectionKey.js';
Expand Down Expand Up @@ -97,6 +98,7 @@ export class ConnectionInfoResource extends CachedMapResource<IConnectionInfoPar
sessionDataResource: SessionDataResource,
appAuthService: AppAuthService,
connectionInfoEventHandler: ConnectionInfoEventHandler,
connectionStateEventHandler: ConnectionStateEventHandler,
userInfoResource: UserInfoResource,
) {
super();
Expand Down Expand Up @@ -163,6 +165,49 @@ export class ConnectionInfoResource extends CachedMapResource<IConnectionInfoPar
this,
);

connectionStateEventHandler.onEvent<IWsDataSourceDisconnectEvent>(
ServerEventId.CbDatasourceDisconnected,
async data => {
const key: IConnectionInfoParams = {
projectId: data.projectId,
connectionId: data.connectionId,
};
const metadata = this.metadata.get(key);

if (metadata?.connecting) {
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use isConnecting (you then can make only one if)


if (this.isConnected(key)) {
this.markOutdated(key);
}
},
undefined,
this,
);

connectionStateEventHandler.onEvent<IWsDataSourceConnectEvent>(
ServerEventId.CbDatasourceConnected,
async data => {
const key: IConnectionInfoParams = {
projectId: data.projectId,
connectionId: data.connectionId,
};

const metadata = this.metadata.get(key);

if (metadata?.connecting) {
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here


if (!this.isConnected(key)) {
this.markOutdated(key);
}
},
undefined,
this,
);

connectionInfoEventHandler.onEvent<ResourceKeyList<IConnectionInfoParams>>(
ServerEventId.CbDatasourceUpdated,
key => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import { injectable } from '@cloudbeaver/core-di';
import { type ISessionEvent, type SessionEventId, SessionEventSource, SessionEventTopic, TopicEventHandler } from '@cloudbeaver/core-root';
import type { WsDataSourceConnectEvent, WsDataSourceDisconnectEvent } from '@cloudbeaver/core-sdk';

export type IWsDataSourceDisconnectEvent = WsDataSourceDisconnectEvent;
export type IWsDataSourceConnectEvent = WsDataSourceConnectEvent;

type ConnectionStateEvent = IWsDataSourceConnectEvent | IWsDataSourceDisconnectEvent;

@injectable()
export class ConnectionStateEventHandler extends TopicEventHandler<ConnectionStateEvent, ISessionEvent, SessionEventId, SessionEventTopic> {
constructor(sessionEventSource: SessionEventSource) {
super(SessionEventTopic.CbDatasourceConnection, sessionEventSource);
}

map(event: any): ConnectionStateEvent {
return event;
}
}
1 change: 1 addition & 0 deletions webapp/packages/core-connections/src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ export const manifest: PluginManifest = {
() => import('./ConnectionFolderEventHandler.js').then(m => m.ConnectionFolderEventHandler),
() => import('./ConnectionsSettingsService.js').then(m => m.ConnectionsSettingsService),
() => import('./ConnectionPublicSecretsResource.js').then(m => m.ConnectionPublicSecretsResource),
() => import('./ConnectionStateEventHandler.js').then(m => m.ConnectionStateEventHandler),
],
};
Loading