Skip to content

Commit

Permalink
Enable database WAL
Browse files Browse the repository at this point in the history
  • Loading branch information
bhj committed May 29, 2021
1 parent 7a266f7 commit 5f7171a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

- **(app)** **(breaking)** Browsers without the [ResizeObserver API](https://caniuse.com/#feat=mdn-api_resizeobserver) are no longer supported
- **(server)** Uses one less process/helper
- **(server)** Media scanner performance is greatly improved when adding media
- **(server)** Media scanner process is restricted to read-only database access
- **(server)** Fixed potential error when a non-admin user tries to remove one of their queued songs
- **(server)** Fixed potential SQLITE_BUSY errors while scanning media
Expand Down
13 changes: 12 additions & 1 deletion server/lib/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ let _db

class Database {
static async close () {
// @todo
// depending on how the node process was started, it can receive
// multiple SIGINTs or SIGTERMs in the same tick, so we clear the
// reference first to avoid calling close() multiple times
if (_db) {
const db = _db
_db = null

log.info('Closing database file %s', db.config.filename)
await db.close()
}
}

static async open ({ readonly = true, env = process.env } = {}) {
Expand All @@ -25,6 +34,8 @@ class Database {
await db.migrate({
migrationsPath: path.join(__dirname, 'schemas'),
})

await db.run('PRAGMA journal_mode = WAL;')
}

_db = db
Expand Down
15 changes: 14 additions & 1 deletion server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ const {

Object.keys(env).forEach(key => log.verbose(`${key} = ${env[key]}`))

// close db before exiting (can't do async in the 'exit' handler)
process.on('SIGTERM', shutdown)
process.on('SIGINT', shutdown)

// make sure child processes don't hang around
process.on('exit', function () {
if (refs.scanner) refs.scanner.kill()
})

// debug: log stack trace for unhandled promise rejections
process.on('unhandledRejection', (reason, p) => {
log.error('Unhandled Rejection at: Promise', p, 'reason:', reason)
log.error('Unhandled Rejection:', p, 'reason:', reason)
})

// detect electron
Expand Down Expand Up @@ -80,3 +84,12 @@ function stopScanner () {
IPC.send({ type: SCANNER_CMD_STOP })
}
}

function shutdown (signal) {
log.info('Received signal %s', signal)

Database.close().then(() => {
log.info('Goodbye for now...')
process.exit(0)
})
}

0 comments on commit 5f7171a

Please sign in to comment.