Update proper lockfile settings #1326

This commit is contained in:
advplyr 2023-04-16 15:21:04 -05:00
parent 2ae3ea346f
commit f5009f76f4
2 changed files with 38 additions and 22 deletions

View File

@ -27,17 +27,16 @@ class Db {
this.SeriesPath = Path.join(global.ConfigPath, 'series') this.SeriesPath = Path.join(global.ConfigPath, 'series')
this.FeedsPath = Path.join(global.ConfigPath, 'feeds') this.FeedsPath = Path.join(global.ConfigPath, 'feeds')
const staleTime = 1000 * 60 * 2 this.libraryItemsDb = new njodb.Database(this.LibraryItemsPath, this.getNjodbOptions())
this.libraryItemsDb = new njodb.Database(this.LibraryItemsPath, { lockoptions: { stale: staleTime } }) this.usersDb = new njodb.Database(this.UsersPath, this.getNjodbOptions())
this.usersDb = new njodb.Database(this.UsersPath, { lockoptions: { stale: staleTime } }) this.sessionsDb = new njodb.Database(this.SessionsPath, this.getNjodbOptions())
this.sessionsDb = new njodb.Database(this.SessionsPath, { lockoptions: { stale: staleTime } }) this.librariesDb = new njodb.Database(this.LibrariesPath, this.getNjodbOptions())
this.librariesDb = new njodb.Database(this.LibrariesPath, { datastores: 2, lockoptions: { stale: staleTime } }) this.settingsDb = new njodb.Database(this.SettingsPath, this.getNjodbOptions())
this.settingsDb = new njodb.Database(this.SettingsPath, { datastores: 2, lockoptions: { stale: staleTime } }) this.collectionsDb = new njodb.Database(this.CollectionsPath, this.getNjodbOptions())
this.collectionsDb = new njodb.Database(this.CollectionsPath, { datastores: 2, lockoptions: { stale: staleTime } }) this.playlistsDb = new njodb.Database(this.PlaylistsPath, this.getNjodbOptions())
this.playlistsDb = new njodb.Database(this.PlaylistsPath, { datastores: 2, lockoptions: { stale: staleTime } }) this.authorsDb = new njodb.Database(this.AuthorsPath, this.getNjodbOptions())
this.authorsDb = new njodb.Database(this.AuthorsPath, { lockoptions: { stale: staleTime } }) this.seriesDb = new njodb.Database(this.SeriesPath, this.getNjodbOptions())
this.seriesDb = new njodb.Database(this.SeriesPath, { datastores: 2, lockoptions: { stale: staleTime } }) this.feedsDb = new njodb.Database(this.FeedsPath, this.getNjodbOptions())
this.feedsDb = new njodb.Database(this.FeedsPath, { datastores: 2, lockoptions: { stale: staleTime } })
this.libraryItems = [] this.libraryItems = []
this.users = [] this.users = []
@ -59,6 +58,21 @@ class Db {
return this.users.some(u => u.id === 'root') return this.users.some(u => u.id === 'root')
} }
getNjodbOptions() {
return {
lockoptions: {
stale: 1000 * 20, // 20 seconds
update: 2500,
retries: {
retries: 20,
minTimeout: 250,
maxTimeout: 5000,
factor: 1
}
}
}
}
getEntityDb(entityName) { getEntityDb(entityName) {
if (entityName === 'user') return this.usersDb if (entityName === 'user') return this.usersDb
else if (entityName === 'session') return this.sessionsDb else if (entityName === 'session') return this.sessionsDb
@ -88,17 +102,16 @@ class Db {
} }
reinit() { reinit() {
const staleTime = 1000 * 60 * 2 this.libraryItemsDb = new njodb.Database(this.LibraryItemsPath, this.getNjodbOptions())
this.libraryItemsDb = new njodb.Database(this.LibraryItemsPath, { lockoptions: { stale: staleTime } }) this.usersDb = new njodb.Database(this.UsersPath, this.getNjodbOptions())
this.usersDb = new njodb.Database(this.UsersPath, { lockoptions: { stale: staleTime } }) this.sessionsDb = new njodb.Database(this.SessionsPath, this.getNjodbOptions())
this.sessionsDb = new njodb.Database(this.SessionsPath, { lockoptions: { stale: staleTime } }) this.librariesDb = new njodb.Database(this.LibrariesPath, this.getNjodbOptions())
this.librariesDb = new njodb.Database(this.LibrariesPath, { datastores: 2, lockoptions: { stale: staleTime } }) this.settingsDb = new njodb.Database(this.SettingsPath, this.getNjodbOptions())
this.settingsDb = new njodb.Database(this.SettingsPath, { datastores: 2, lockoptions: { stale: staleTime } }) this.collectionsDb = new njodb.Database(this.CollectionsPath, this.getNjodbOptions())
this.collectionsDb = new njodb.Database(this.CollectionsPath, { datastores: 2, lockoptions: { stale: staleTime } }) this.playlistsDb = new njodb.Database(this.PlaylistsPath, this.getNjodbOptions())
this.playlistsDb = new njodb.Database(this.PlaylistsPath, { datastores: 2, lockoptions: { stale: staleTime } }) this.authorsDb = new njodb.Database(this.AuthorsPath, this.getNjodbOptions())
this.authorsDb = new njodb.Database(this.AuthorsPath, { lockoptions: { stale: staleTime } }) this.seriesDb = new njodb.Database(this.SeriesPath, this.getNjodbOptions())
this.seriesDb = new njodb.Database(this.SeriesPath, { datastores: 2, lockoptions: { stale: staleTime } }) this.feedsDb = new njodb.Database(this.FeedsPath, this.getNjodbOptions())
this.feedsDb = new njodb.Database(this.FeedsPath, { datastores: 2, lockoptions: { stale: staleTime } })
return this.init() return this.init()
} }

View File

@ -118,6 +118,7 @@ function updateLock(file, options) {
// the lockfile was deleted or we are over the threshold // the lockfile was deleted or we are over the threshold
if (err) { if (err) {
if (err.code === 'ENOENT' || isOverThreshold) { if (err.code === 'ENOENT' || isOverThreshold) {
console.error(`lockfile "${file}" compromised. stat code=${err.code}, isOverThreshold=${isOverThreshold}`)
return setLockAsCompromised(file, lock, Object.assign(err, { code: 'ECOMPROMISED' })); return setLockAsCompromised(file, lock, Object.assign(err, { code: 'ECOMPROMISED' }));
} }
@ -129,6 +130,7 @@ function updateLock(file, options) {
const isMtimeOurs = lock.mtime.getTime() === stat.mtime.getTime(); const isMtimeOurs = lock.mtime.getTime() === stat.mtime.getTime();
if (!isMtimeOurs) { if (!isMtimeOurs) {
console.error(`lockfile "${file}" compromised. mtime is not ours`)
return setLockAsCompromised( return setLockAsCompromised(
file, file,
lock, lock,
@ -152,6 +154,7 @@ function updateLock(file, options) {
// the lockfile was deleted or we are over the threshold // the lockfile was deleted or we are over the threshold
if (err) { if (err) {
if (err.code === 'ENOENT' || isOverThreshold) { if (err.code === 'ENOENT' || isOverThreshold) {
console.error(`lockfile "${file}" compromised. utimes code=${err.code}, isOverThreshold=${isOverThreshold}`)
return setLockAsCompromised(file, lock, Object.assign(err, { code: 'ECOMPROMISED' })); return setLockAsCompromised(file, lock, Object.assign(err, { code: 'ECOMPROMISED' }));
} }