Skip to content

Commit

Permalink
better handling of the db instance (only open once for listeners, don…
Browse files Browse the repository at this point in the history
…'t over-close)
  • Loading branch information
briangonzalez committed Nov 2, 2017
1 parent 5bdfa5d commit 5e5c1cf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ function listen() {
let last = packTimeConditionally(appleTimeNow() - 5)
let bail = false

const dbPromise = messagesDb.open()

async function check() {
const db = await dbPromise
const query = `
SELECT
guid,
Expand All @@ -137,7 +140,6 @@ function listen() {
last = packTimeConditionally(appleTimeNow())

try {
const db = await messagesDb.open()
const messages = await db.all(query)
messages.forEach(msg => {
if (emittedMsgs[msg.guid]) return
Expand Down
6 changes: 4 additions & 2 deletions lib/messages-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ async function open() {
return db
}

let isClosing;
function cleanUp() {
if (db && db.driver.open)
if (db && db.driver.open && !isClosing) {
isClosing = true
db.close()
}
}
process.on('exit', cleanUp)
process.on('SIGINT', cleanUp)
process.on('uncaughtException', cleanUp)

module.exports = {
Expand Down

0 comments on commit 5e5c1cf

Please sign in to comment.