Skip to content

Commit

Permalink
Merge pull request #205 from reactioncommerce/feat-aldeed-wait-for-re…
Browse files Browse the repository at this point in the history
…plica-set

feat: wait for mongo replica set
  • Loading branch information
kieckhafer authored Feb 5, 2020
2 parents 6c1b8bb + 9ac4214 commit cca79a4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
31 changes: 27 additions & 4 deletions .reaction/waitForMongo.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Meteor hard fails right away if it can't connect to MongoDB.
* Since we have no way of changing that, we run this script
* first, which tries for 60 seconds to connect before failing.
* first, which tries for 120 seconds to connect before failing.
*
* Usage:
* node waitForMongo.js && some command that need mongo
Expand All @@ -10,7 +10,7 @@
const url = require("url");
const { MongoClient } = require("mongodb");

const WAIT_FOR_SECONDS = 60;
const WAIT_FOR_SECONDS = 120;

/**
* Print a message to the console (no trailing newline)
Expand Down Expand Up @@ -93,7 +93,20 @@ async function connect(mongoUrl) {
useNewUrlParser: true,
useUnifiedTopology: true
});
await client.close();
return client;
}

/**
* Check if replication is ready
*
* @param {Object} db connected mongo db instance
* @returns {Promise} indication of success/failure
*/
async function checkReplicaSetStatus(db) {
const status = await db.admin().replSetGetStatus();
if (status.ok !== 1) {
throw new Error("Replica set not yet initialized");
}
}

/**
Expand All @@ -105,12 +118,22 @@ async function main() {
if (!MONGO_URL) {
throw new Error("You must set MONGO_URL environment variable.");
}

defaultOut("Waiting for MongoDB...\n");
await checkWaitRetry({
const client = await checkWaitRetry({
timeoutMessage: "ERROR: MongoDB not reachable in time.",
check: connect.bind(null, MONGO_URL)
});
defaultOut("MongoDB ready.\n");

defaultOut("Waiting for MongoDB replica set...\n");
await checkWaitRetry({
timeoutMessage: "ERROR: MongoDB replica set not ready in time.",
check: checkReplicaSetStatus.bind(null, client.db())
});
defaultOut("MongoDB replica set ready.\n");

await client.close();
process.exit();
}

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ services:
build:
context: .
dockerfile: Dockerfile-dev
command: bash -c "export PATH=$PATH:/home/node/.meteor && time meteor npm install --no-audit && && node ./.reaction/waitForMongo.js && node --experimental-modules ./.reaction/scripts/run.mjs"
command: bash -c "export PATH=$PATH:/home/node/.meteor && npm install --no-audit && node ./.reaction/waitForMongo.js && node --experimental-modules ./.reaction/scripts/run.mjs"
volumes:
- .:/usr/local/src/app:cached
- reaction_meteor_local:/usr/local/src/app/.meteor/local
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "reaction",
"name": "reaction-admin",
"description": "Reaction is a modern reactive, real-time event driven ecommerce platform.",
"version": "3.0.0-beta.3",
"main": "main.js",
Expand Down

0 comments on commit cca79a4

Please sign in to comment.