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

Added db connection retry and docker configuration #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
npm-debug.log
Dockerfile
.dockerignore
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ dump.tar.gz
dump/
coverage
*.orig
.idea
.idea/*
*.iml
8 changes: 8 additions & 0 deletions 001_users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
db.createUser({
"user": "smack_chat",
"pwd": "smack123",
"roles": [{
"role": "readWrite",
"db": "smack_chat"
}]
})
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM node:10-alpine
WORKDIR /usr/app
COPY package.json ./
RUN npm install
COPY --chown=node:node . .
EXPOSE 3030
CMD [ "npm", "start" ]
23 changes: 23 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: '3.8'

services:
mongodb:
image: mongo
command: --serviceExecutor adaptive
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: admin123
MONGO_INITDB_DATABASE: smack_chat
volumes:
- ./001_users.js:/docker-entrypoint-initdb.d/001_users.js:ro
mac-chat-api:
build: .
depends_on:
- mongodb
ports:
- "3005:3005"
environment:
PORT: 3005
MONGODB_URI: "mongodb://smack_chat:smack123@mongodb:27017/smack_chat"
39 changes: 27 additions & 12 deletions src/db.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
import mongoose from 'mongoose';
import mongodb from 'mongodb';
import config from './config';

export default callback => {
let db;
let retryCount = 0;

let connectWithRetry = function() {
return mongoose.connect(config.mongoUrl, function (err, database) {
console.log("Trying to connect to mongodb...")
if (err) {
console.log("Waiting for 5 seconds to retry...")
if (retryCount < 4) {
setTimeout(connectWithRetry, 5000)
retryCount++
return
}

console.log(err);
console.log("Giving up after 5 attempts...")
proccess.exit(-1)
}

console.log(config.mongoUrl);
// Save database object from the callback for reuse.
db = database;
console.log("Database connection ready");
callback(db);
});
}
// Connect to the database before starting the application server.
mongoose.connect(config.mongoUrl, function (err, database) {
if (err) {
console.log(err);
process.exit(1);
}
console.log(config.mongoUrl);
// Save database object from the callback for reuse.
db = database;
console.log("Database connection ready");
callback(db);
});

connectWithRetry()
}