Add: db error recovery check, Fix: bookmarks modal UI overflow #115

This commit is contained in:
advplyr 2021-11-03 19:12:17 -05:00
parent f7a780032f
commit f212ea91f9
5 changed files with 71 additions and 7 deletions

View file

@ -1,5 +1,6 @@
const Path = require('path')
const njodb = require("njodb")
const fs = require('fs-extra')
const jwt = require('jsonwebtoken')
const Logger = require('./Logger')
const Audiobook = require('./objects/Audiobook')
@ -187,6 +188,11 @@ class Db {
return true
}).catch((error) => {
Logger.error(`[DB] Update entity ${entityName} Failed: ${error}`)
if (error && error.code === 'ENOENT') {
this.attemptDataRecovery(entityName)
}
return false
})
}
@ -204,6 +210,63 @@ class Db {
})
}
async attemptDataRecovery(entityName) {
var dbDirName = this.getEntityArrayKey(entityName)
var dbdir = Path.join(this.ConfigPath, dbDirName)
console.log('Attempting data recovery for:', dbdir)
var exists = await fs.pathExists(dbdir)
if (!exists) {
console.error('Db dir does not exist', dbdir)
return
}
try {
var dbdatadir = Path.join(dbdir, 'data')
var dbtmpdir = Path.join(dbdir, 'tmp')
var datafiles = await fs.readdir(dbdatadir)
var tempfiles = await fs.readdir(dbtmpdir)
var orphanOld = datafiles.find(df => df.endsWith('.old'))
if (orphanOld) {
// Get data file num
var dbnum = orphanOld.split('.')[1]
console.log('Found orphan json.old', orphanOld, `Num: ${dbnum}`)
var dbDataFilename = `data.${dbnum}.json`
// make sure data.#.json does not already exist
if (datafiles.includes(dbDataFilename)) {
console.warn(`${dbDataFilename} already exists, not recovering`)
return
}
// find temp file that was supposed to be renamed
var matchingTmp = tempfiles.find(tmp => tmp.startsWith(`data.${dbnum}`))
if (matchingTmp) {
console.log('found matching tmp file', matchingTmp)
var tmpfileFullPath = Path.join(dbtmpdir, matchingTmp)
var renameToPath = Path.join(dbdatadir, dbDataFilename)
console.log(`Renamining "${tmpfileFullPath}" => "${renameToPath}"`)
await fs.rename(tmpfileFullPath, renameToPath)
console.log('Data recovery successful -- unlinking old')
await fs.unlink(orphanOld)
console.log('Removed .old file')
var lockdirpath = Path.join(dbdatadir, `data.${dbnum}.lock`)
await fs.rmdir(lockdirpath)
console.log('Removed lock dir')
}
}
} catch (error) {
console.error('Data recovery failed', error)
}
}
recreateAudiobookDb() {
return this.audiobooksDb.drop().then((results) => {
Logger.info(`[DB] Dropped audiobook db`, results)