Skip to content

Instantly share code, notes, and snippets.

View pwhisenhunt's full-sized avatar

Phillip Whisenhunt pwhisenhunt

View GitHub Profile
@pwhisenhunt
pwhisenhunt / cleanly-end-process.js
Created April 28, 2020 23:01
blog-post-node-js-handle-connections-cleanly-end-process
process.on('SIGINT', () => {
console.log('SIGINT signal received.');
let mongoClientExited = false;
let redisClientExited = false;
let expressServerExited = false;
function attemptToExitProcess() {
if (redisClientExited && mongoClientExited && expressServerExited) {
console.log('All connections have cleanly closed. Exiting process...');
process.exit(0);
@pwhisenhunt
pwhisenhunt / express-wait-for-mongo-and-redis-connections.js
Last active May 25, 2020 17:34
blog-post-node-js-handle-connections-express-wait-for-mongo-and-redis
let mongooseConnected = false;
let redisConnected = false;
function tryToStartTheServer() {
if (mongooseConnected && redisConnected) {
console.log("mongoose and redis are connected. LET'S GO!");
app.emit('ready');
}
}
@pwhisenhunt
pwhisenhunt / redis-client-events.js
Last active May 25, 2020 17:32
blog-post-node-js-handle-connections-redis-events
const redisOptions = { host: env.REDIS_HOST, port: env.REDIS_PORT };
if (env.NODE_ENV === 'production') {
redisOptions['password'] = env.REDIS_PASSWORD;
}
const redisClient = redis.createClient(redisOptions);
const redisEvents = [
'ready',
'connect',
'end',
'error',
@pwhisenhunt
pwhisenhunt / mongoose-exponential-backoff-reconnect.js
Last active May 25, 2020 17:31
blog-post-node-js-handle-connections-mongoose-initial-connection-retry
let numberOfMongooseRetries = 1;
const THIRTY_SECONDS = 30000;
const MAX_DELAY_TO_RECONNECT = THIRTY_SECONDS;
async function connectToMongoose() {
console.log(`attempt ${numberOfMongooseRetries} to connect to mongo`);
try {
await mongoose.connect(env.MONGO_DB_CONNECTION_STRING, { useNewUrlParser: true })
}
catch (err) {
// Note: this is where you would send errors to your error tracker
@pwhisenhunt
pwhisenhunt / populate-env.js
Last active May 25, 2020 17:07
blog-post-node-js-handle-connections-get-config
function ensure(keys) {
const out = {};
keys.forEach(key => {
if (!process.env[key]) {
throw new Error(`Missing environment variable ${key}`);
}
out[key] = process.env[key];
});
return out;
};
@pwhisenhunt
pwhisenhunt / log-mongoose-events.js
Last active May 25, 2020 17:31
blog-post-node-js-handle-connections-mongoose-events
const mongooseEvents = [
'close',
'connected',
'disconnected',
'disconnecting',
'error',
'fullsetup',
'all',
'reconnected',
'reconnectFailed',
@pwhisenhunt
pwhisenhunt / simple-express-app.js
Last active April 28, 2020 22:19
blog-post-node-js-handle-connections-simple-express-app
const express = require("express")
const http = require("http")
const redis = require("redis")
const mongoose = require('mongoose');
const app = express();
server = http.createServer(app);

Keybase proof

I hereby claim:

  • I am pwhisenhunt on github.
  • I am pwhisenhunt (https://keybase.io/pwhisenhunt) on keybase.
  • I have a public key ASDPcgH_FgkRxCK_p1U3SEgz53PUlCKcXBWsO7nrQZN49wo

To claim this, I am signing this object: