Created
September 1, 2020 13:20
-
-
Save florianbepunkt/c75b1bf062b7e42831fa34bebc72be6f to your computer and use it in GitHub Desktop.
MongoDB lambda mongocryptd race condition fix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this is the writable folder where our .pid file live | |
const tmpPath = path.resolve(process.env.LAMBDA_TASK_ROOT, "../../tmp"); | |
// if the file exists our ephemeral function container got reused, so we do not need to spawn the process again | |
if (!fs.existsSync(tmpPath + "/mongocryptd.pid")) { | |
await new Promise((resolve, reject) => { | |
// to prevent waiting for godot | |
const safeGuard = setTimeout(() => reject(), 3000); | |
const child = spawn( | |
"mongocryptd", | |
["--idleShutdownTimeoutSecs=240", `--pidfilepath=${tmpPath}/mongocryptd.pid`], | |
{ | |
// note that we need stdio in order to determine when the process is ready to accept connections | |
detached: true, | |
} | |
); | |
child.stdout.on("data", (data) => { | |
// I checked the mongocryptd logs | |
// id:23016 means the process is ready to accept incoming connections | |
// this looks hacky, but the bson package is unable to parse the mongocryptd logs | |
if (data.toString().includes(`"id":23016`)) { | |
console.debug("Mongocryptd is ready to accept connecitons. Proceed to connect to cluster."); | |
clearTimeout(safeGuard); | |
resolve(); | |
} | |
}); | |
child.stderr.on("data", (data) => { | |
// you could add debug logs here | |
reject(data); | |
}); | |
child.unref(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment