Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: wait for mongo replica set #205

Merged
merged 1 commit into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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