Skip to content

Commit

Permalink
fix: wait for DB to connect before serving
Browse files Browse the repository at this point in the history
  • Loading branch information
zeim839 committed May 1, 2024
1 parent aa79bb7 commit 831b36a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
9 changes: 5 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ const http = require('http').createServer(app)
const apiRoute = require('./routes/api')

const cache = new CacheModule()
cache.register(...callbacks)
cache.start(config.cache_interval)

app.set('view engine', 'ejs')
app.use(compression())
Expand Down Expand Up @@ -81,8 +79,11 @@ app.get('/*', (req, res) => {
res.render('404', { version: config.VERSION })
})

db.connect()
http.listen(config.port, () => console.log(`Server running on port ${config.port}`))
db.connect().then(async () => {
await cache.register(...callbacks)
cache.start(config.cache_interval)
http.listen(config.port, () => console.log(`Server running on port ${config.port}`))
})

const stop = () => {
http.close()
Expand Down
4 changes: 3 additions & 1 deletion utils/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ class Cache {
}

start (interval) {
if (typeof (interval) !== 'number') { return new Error('Expected interval to be of type number') }
if (typeof (interval) !== 'number') {
return new Error('Expected interval to be of type number')
}

// Run AT MOST once every 'interval' milliseconds
this.process = setInterval(() => {
Expand Down

0 comments on commit 831b36a

Please sign in to comment.