@@ -49,27 +49,36 @@ export class MongoDBContainer extends GenericContainer {
4949
5050 private withWaitForRsHealthCheck ( ) : this {
5151 return this . withHealthCheck ( {
52- test : [
53- "CMD-SHELL" ,
54- this . buildMongoEvalCommand (
55- `'try { rs.initiate(); } catch (e){} while (db.runCommand({isMaster: 1}).ismaster==false) { sleep(100); }'`
56- ) ,
57- ] ,
58- interval : 250 ,
52+ test : [ "CMD-SHELL" , this . buildMongoEvalCommand ( this . getRsInitCmd ( ) ) ] ,
53+ interval : 5000 ,
5954 timeout : 60000 ,
6055 retries : 1000 ,
6156 } ) ;
6257 }
6358
6459 private buildMongoEvalCommand ( command : string ) {
65- const useMongosh = satisfies ( this . imageName . tag , ">=5.0.0" ) ;
6660 const args = [ ] ;
67- if ( useMongosh ) args . push ( "mongosh" ) ;
61+ if ( this . isV5OrLater ( ) ) args . push ( "mongosh" ) ;
6862 else args . push ( "mongo" , "admin" ) ;
6963 if ( this . username && this . password ) args . push ( "-u" , this . username , "-p" , this . password ) ;
7064 args . push ( "--quiet" , "--eval" , command ) ;
7165 return args . join ( " " ) ;
7266 }
67+
68+ private getRsInitCmd ( ) {
69+ if ( this . isV5OrLater ( ) )
70+ return `'try { rs.status(); } catch (e) { rs.initiate(); } while (db.runCommand({isMaster: 1}).ismaster==false) { sleep(100); }'` ;
71+ else
72+ return `'try { rs.initiate(); } catch (e) {} while (db.runCommand({isMaster: 1}).ismaster==false) { sleep(100); }'` ;
73+ }
74+
75+ private isV5OrLater ( ) {
76+ try {
77+ return satisfies ( this . imageName . tag , ">=5.0.0" ) ;
78+ } catch {
79+ return false ;
80+ }
81+ }
7382}
7483
7584export class StartedMongoDBContainer extends AbstractStartedContainer {
0 commit comments