Skip to content

Commit 645d784

Browse files
authored
Merge pull request #77 from EMResearch/reservations-api-busy-waiting
busy waiting for mongoDB
2 parents 91eded8 + b96eaac commit 645d784

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

jdk_11_gradle/em/embedded/rest/reservations-api/src/main/java/em/embedded/reservationsapi/EmbeddedEvoMasterController.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,38 @@ public String startSut() {
9696
"--app.jwt.secret=abcdef012345678901234567890123456789abcdef012345678901234567890123456789"
9797
});
9898

99+
try {
100+
Thread.sleep(3_000);
101+
} catch (InterruptedException e) {
102+
// do nothing
103+
}
104+
105+
while (!isMongoClientReady()) {
106+
try {
107+
Thread.sleep(1_000);
108+
} catch (InterruptedException e) {
109+
// do nothing
110+
}
111+
}
112+
99113
return "http://localhost:" + getSutPort();
100114
}
101115

116+
/**
117+
* Checks if the mongo database is ready to receive commands using a ping command
118+
* @return
119+
*/
120+
private boolean isMongoClientReady() {
121+
try {
122+
MongoDatabase db = mongoClient.getDatabase(MONGODB_DATABASE_NAME);
123+
Document pingResult = db.runCommand(new Document("ping", 1));
124+
return pingResult.getDouble("ok") == 1.0;
125+
} catch (Exception ex) {
126+
// Connection error
127+
return false;
128+
}
129+
}
130+
102131
protected int getSutPort() {
103132
return (Integer) ((Map) ctx.getEnvironment()
104133
.getPropertySources().get("server.ports").getSource())

jdk_11_gradle/em/external/rest/reservations-api/src/main/java/em/external/reservationsapi/ExternalEvoMasterController.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,34 @@ public void preStart() {
153153

154154
@Override
155155
public void postStart() {
156+
try {
157+
Thread.sleep(3_000);
158+
} catch (InterruptedException e) {
159+
// do nothing
160+
}
161+
162+
while (!isMongoClientReady()) {
163+
try {
164+
Thread.sleep(1_000);
165+
} catch (InterruptedException e) {
166+
// do nothing
167+
}
168+
}
169+
}
170+
171+
/**
172+
* Checks if the mongo database is ready to receive commands using a ping command
173+
* @return
174+
*/
175+
private boolean isMongoClientReady() {
176+
try {
177+
MongoDatabase db = mongoClient.getDatabase(MONGODB_DATABASE_NAME);
178+
Document pingResult = db.runCommand(new Document("ping", 1));
179+
return pingResult.getDouble("ok") == 1.0;
180+
} catch (Exception ex) {
181+
// Connection error
182+
return false;
183+
}
156184
}
157185

158186
@Override
@@ -270,4 +298,6 @@ public List<DbSpecification> getDbSpecifications() {
270298
public Object getMongoConnection() {
271299
return mongoClient;
272300
}
301+
302+
273303
}

0 commit comments

Comments
 (0)