@@ -108,7 +108,7 @@ export class ConnectClusterTool extends AtlasToolBase {
108
108
return cn . toString ( ) ;
109
109
}
110
110
111
- private async connectToCluster ( connectionString : string ) : Promise < void > {
111
+ private async connectToCluster ( connectionString : string , tryCount : number ) : Promise < void > {
112
112
let lastError : Error | undefined = undefined ;
113
113
114
114
logger . debug (
@@ -117,11 +117,15 @@ export class ConnectClusterTool extends AtlasToolBase {
117
117
`attempting to connect to cluster: ${ this . session . connectedAtlasCluster ?. clusterName } `
118
118
) ;
119
119
120
- for ( let i = 0 ; i < 600 ; i ++ ) {
121
- // try for 5 minutes
120
+ for ( let i = 0 ; i < tryCount ; i ++ ) {
122
121
try {
123
- await this . session . connectToMongoDB ( connectionString , this . config . connectOptions ) ;
124
122
lastError = undefined ;
123
+
124
+ if ( ! this . session . connectedAtlasCluster ) {
125
+ throw new Error ( "Cluster connection aborted" ) ;
126
+ }
127
+
128
+ await this . session . connectToMongoDB ( connectionString , this . config . connectOptions ) ;
125
129
break ;
126
130
} catch ( err : unknown ) {
127
131
const error = err instanceof Error ? err : new Error ( String ( err ) ) ;
@@ -171,6 +175,19 @@ export class ConnectClusterTool extends AtlasToolBase {
171
175
}
172
176
173
177
protected async execute ( { projectId, clusterName } : ToolArgs < typeof this . argsShape > ) : Promise < CallToolResult > {
178
+ const connectingResult = {
179
+ content : [
180
+ {
181
+ type : "text" as const ,
182
+ text : `Attempting to connect to cluster "${ clusterName } "...` ,
183
+ } ,
184
+ {
185
+ type : "text" as const ,
186
+ text : `Warning: Provisioning a user and connecting to the cluster may take more time, please check again in a few seconds.` ,
187
+ } ,
188
+ ] ,
189
+ } ;
190
+
174
191
try {
175
192
const state = await this . queryConnection ( projectId , clusterName ) ;
176
193
switch ( state ) {
@@ -184,14 +201,7 @@ export class ConnectClusterTool extends AtlasToolBase {
184
201
] ,
185
202
} ;
186
203
case "connecting" :
187
- return {
188
- content : [
189
- {
190
- type : "text" ,
191
- text : "Cluster is connecting..." ,
192
- } ,
193
- ] ,
194
- } ;
204
+ return connectingResult ;
195
205
case "connected-to-other-cluster" :
196
206
case "disconnected" :
197
207
default :
@@ -210,30 +220,40 @@ export class ConnectClusterTool extends AtlasToolBase {
210
220
211
221
await this . session . disconnect ( ) ;
212
222
const connectionString = await this . prepareClusterConnection ( projectId , clusterName ) ;
213
- process . nextTick ( async ( ) => {
214
- try {
215
- await this . connectToCluster ( connectionString ) ;
216
- } catch ( err : unknown ) {
217
- const error = err instanceof Error ? err : new Error ( String ( err ) ) ;
218
- logger . debug (
219
- LogId . atlasConnectFailure ,
220
- "atlas-connect-cluster" ,
221
- `error connecting to cluster: ${ error . message } `
222
- ) ;
223
- }
224
- } ) ;
225
223
226
- return {
227
- content : [
228
- {
229
- type : "text" ,
230
- text : `Attempting to connect to cluster "${ clusterName } "...` ,
231
- } ,
232
- {
233
- type : "text" ,
234
- text : `Warning: Check again in a few seconds.` ,
235
- } ,
236
- ] ,
237
- } ;
224
+ try {
225
+ await this . connectToCluster ( connectionString , 60 ) ;
226
+
227
+ return {
228
+ content : [
229
+ {
230
+ type : "text" ,
231
+ text : `Connected to cluster "${ clusterName } ".` ,
232
+ } ,
233
+ ] ,
234
+ } ;
235
+ } catch ( err : unknown ) {
236
+ const error = err instanceof Error ? err : new Error ( String ( err ) ) ;
237
+ logger . debug (
238
+ LogId . atlasConnectFailure ,
239
+ "atlas-connect-cluster" ,
240
+ `error connecting to cluster: ${ error . message } `
241
+ ) ;
242
+
243
+ process . nextTick ( async ( ) => {
244
+ try {
245
+ await this . connectToCluster ( connectionString , 600 ) ;
246
+ } catch ( err : unknown ) {
247
+ const error = err instanceof Error ? err : new Error ( String ( err ) ) ;
248
+ logger . debug (
249
+ LogId . atlasConnectFailure ,
250
+ "atlas-connect-cluster" ,
251
+ `error connecting to cluster: ${ error . message } `
252
+ ) ;
253
+ }
254
+ } ) ;
255
+
256
+ return connectingResult ;
257
+ }
238
258
}
239
259
}
0 commit comments