PromucFlow_constructor/deploy/docker/utils/bin/check_replica_set.js
Shrikant Sharat Kandula e2343f5917
chore: Update MongoDB client in appsmithctl (#25959)
Should fix the following:

1. https://github.com/appsmithorg/appsmith/security/dependabot/234
2. https://github.com/appsmithorg/appsmith/security/dependabot/232
3. https://github.com/appsmithorg/appsmith/security/dependabot/249

Also removed the `estimate_billing.js` command, and the `luxon` and
`minimist` dependencies that are only used in that command.
2023-08-03 16:36:53 +05:30

56 lines
1.5 KiB
JavaScript

const { MongoClient, MongoServerError} = require("mongodb");
const { preprocessMongoDBURI } = require("./utils");
async function exec() {
const client = new MongoClient(preprocessMongoDBURI(process.env.APPSMITH_MONGODB_URI), {
useNewUrlParser: true,
useUnifiedTopology: true,
});
let isReplicaSetEnabled = false;
try {
isReplicaSetEnabled = await checkReplicaSet(client);
} catch (err) {
console.error("Error trying to check replicaset", err);
} finally {
await client.close();
}
process.exit(isReplicaSetEnabled ? 0 : 1);
}
async function checkReplicaSet(client) {
await client.connect();
return await new Promise((resolve) => {
try {
const changeStream = client
.db()
.collection("user")
.watch()
.on("change", (change) => console.log(change))
.on("error", (err) => {
if (err instanceof MongoServerError && err.toString() == "MongoServerError: The $changeStream stage is only supported on replica sets") {
console.log("Replica set not enabled");
} else {
console.error("Error even from changeStream", err);
}
resolve(false);
});
// setTimeout so the error event can kick-in first
setTimeout(() => {
resolve(true);
changeStream.close();
}, 1000);
} catch (err) {
console.error("Error thrown when checking replicaset", err);
resolve(false);
}
});
}
module.exports = {
exec,
};