Add: Backup notification (#3225)

* Formatting updates

* Add: backup completion notification

* Fix: comment for backup

* Add: backup size units to notification

* Add: failed backup notification

* Add: calls to failed backup notification

* Update: notification OpenAPI spec

* Update notifications to first check if any are active for an event, update JS docs

---------

Co-authored-by: advplyr <advplyr@protonmail.com>
This commit is contained in:
Nicholas W 2024-08-18 14:32:05 -05:00 committed by GitHub
parent 5308fd8b46
commit 27b3a44147
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 144 additions and 25 deletions

View file

@ -16,9 +16,11 @@ const { getFileSize } = require('../utils/fileUtils')
const Backup = require('../objects/Backup')
class BackupManager {
constructor() {
constructor(notificationManager) {
this.ItemsMetadataPath = Path.join(global.MetadataPath, 'items')
this.AuthorsMetadataPath = Path.join(global.MetadataPath, 'authors')
/** @type {import('./NotificationManager')} */
this.notificationManager = notificationManager
this.scheduleTask = null
@ -294,6 +296,8 @@ class BackupManager {
// Create backup sqlite file
const sqliteBackupPath = await this.backupSqliteDb(newBackup).catch((error) => {
Logger.error(`[BackupManager] Failed to backup sqlite db`, error)
const errorMsg = error?.message || error || 'Unknown Error'
this.notificationManager.onBackupFailed(errorMsg)
return false
})
@ -304,6 +308,8 @@ class BackupManager {
// Zip sqlite file, /metadata/items, and /metadata/authors folders
const zipResult = await this.zipBackup(sqliteBackupPath, newBackup).catch((error) => {
Logger.error(`[BackupManager] Backup Failed ${error}`)
const errorMsg = error?.message || error || 'Unknown Error'
this.notificationManager.onBackupFailed(errorMsg)
return false
})
@ -324,13 +330,18 @@ class BackupManager {
}
// Check remove oldest backup
if (this.backups.length > this.backupsToKeep) {
const removeOldest = this.backups.length > this.backupsToKeep
if (removeOldest) {
this.backups.sort((a, b) => a.createdAt - b.createdAt)
const oldBackup = this.backups.shift()
Logger.debug(`[BackupManager] Removing old backup ${oldBackup.id}`)
this.removeBackup(oldBackup)
}
// Notification for backup successfully completed
this.notificationManager.onBackupCompleted(newBackup, this.backups.length, removeOldest)
return true
}
@ -348,7 +359,6 @@ class BackupManager {
/**
* @see https://github.com/TryGhost/node-sqlite3/pull/1116
* @param {Backup} backup
* @promise
*/
backupSqliteDb(backup) {
const db = new sqlite3.Database(Database.dbPath)