Skip to content

Instantly share code, notes, and snippets.

@nhusher
Last active April 10, 2017 14:08
Show Gist options
  • Save nhusher/658dce62e8ddad36ce6f08f54f20df1b to your computer and use it in GitHub Desktop.
Save nhusher/658dce62e8ddad36ce6f08f54f20df1b to your computer and use it in GitHub Desktop.
node_modules
version: '3'
services:
db:
image: mongo
# ports:
# - "27017:27017"
my-project:
image: my-project
build:
context: .
links:
- db
FROM node:6.9.4-alpine
WORKDIR /app
ADD package.json ./
RUN npm install --production
ADD server.js ./
CMD npm start
{
"name": "test-dc",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"mongodb": "^2.2.25"
}
}
const MongoClient = require('mongodb').MongoClient
const DATABASE_URL = process.env.DATABASE_URL || 'mongodb://db:27017/myproject'
function connect (url) {
return new Promise((resolve, reject) => {
MongoClient.connect(url, (err, db) => {
if (err) reject(err)
else resolve(db)
})
})
}
function wait (ms, val) {
return new Promise(resolve => {
setTimeout(() => {
resolve(val)
})
}, ms)
}
function retry (coroutine, tries, interval = 500) {
return Promise.resolve(coroutine())
.catch(err => {
if (tries) return wait(interval, () => retry(coroutine, tries - 1))
else throw err
})
}
retry(() => connect(DATABASE_URL), 5).then(db => {
console.log(`Connected to database [${DATABASE_URL}]`)
db.close()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment