Skip to content

Commit fe16acd

Browse files
committed
chore: simplify query connection logic
Now it doesn't need to be async because the connection itself attemps to ping, it just needs to check the current connection status and map the exact values
1 parent da872b4 commit fe16acd

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

src/tools/atlas/connect/connectCluster.ts

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,40 @@ export class ConnectClusterTool extends AtlasToolBase {
2323
clusterName: z.string().describe("Atlas cluster name"),
2424
};
2525

26-
private async queryConnection(
26+
private queryConnection(
2727
projectId: string,
2828
clusterName: string
29-
): Promise<"connected" | "disconnected" | "connecting" | "connected-to-other-cluster" | "unknown"> {
29+
): "connected" | "disconnected" | "connecting" | "connected-to-other-cluster" | "unknown" {
3030
if (!this.session.connectedAtlasCluster) {
3131
if (this.session.isConnectedToMongoDB) {
3232
return "connected-to-other-cluster";
3333
}
3434
return "disconnected";
3535
}
3636

37-
if (
38-
this.session.connectedAtlasCluster.projectId !== projectId ||
39-
this.session.connectedAtlasCluster.clusterName !== clusterName
40-
) {
41-
return "connected-to-other-cluster";
42-
}
43-
44-
if (this.session.connectionManager.currentConnectionState.tag !== "connected") {
45-
return "connecting";
46-
}
37+
const currentConectionState = this.session.connectionManager.currentConnectionState;
4738

48-
try {
49-
await this.session.serviceProvider.runCommand("admin", {
50-
ping: 1,
51-
});
52-
53-
return "connected";
54-
} catch (err: unknown) {
55-
const error = err instanceof Error ? err : new Error(String(err));
56-
logger.debug(
57-
LogId.atlasConnectFailure,
58-
"atlas-connect-cluster",
59-
`error querying cluster: ${error.message}`
60-
);
61-
return "unknown";
39+
switch (currentConectionState.tag) {
40+
case "connected":
41+
if (
42+
this.session.connectedAtlasCluster.projectId !== projectId ||
43+
this.session.connectedAtlasCluster.clusterName !== clusterName
44+
) {
45+
return "connected-to-other-cluster";
46+
}
47+
break;
48+
case "connecting":
49+
case "disconnected": // we might still be calling Atlas APIs and not attempted yet to connect to MongoDB, but we are still "connecting"
50+
return "connecting";
51+
case "errored":
52+
logger.debug(
53+
LogId.atlasConnectFailure,
54+
"atlas-connect-cluster",
55+
`error querying cluster: ${currentConectionState.errorReason}`
56+
);
57+
return "unknown";
6258
}
59+
return "unknown";
6360
}
6461

6562
private async prepareClusterConnection(
@@ -205,7 +202,7 @@ export class ConnectClusterTool extends AtlasToolBase {
205202
protected async execute({ projectId, clusterName }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
206203
await ensureCurrentIpInAccessList(this.session.apiClient, projectId);
207204
for (let i = 0; i < 60; i++) {
208-
const state = await this.queryConnection(projectId, clusterName);
205+
const state = this.queryConnection(projectId, clusterName);
209206
switch (state) {
210207
case "connected": {
211208
return {

0 commit comments

Comments
 (0)