mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-08-31 23:20:05 +02:00
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:
parent
5308fd8b46
commit
27b3a44147
7 changed files with 144 additions and 25 deletions
|
@ -1,5 +1,5 @@
|
|||
const axios = require('axios')
|
||||
const Logger = require("../Logger")
|
||||
const Logger = require('../Logger')
|
||||
const SocketAuthority = require('../SocketAuthority')
|
||||
const Database = require('../Database')
|
||||
const { notificationData } = require('../utils/notifications')
|
||||
|
@ -17,6 +17,11 @@ class NotificationManager {
|
|||
async onPodcastEpisodeDownloaded(libraryItem, episode) {
|
||||
if (!Database.notificationSettings.isUseable) return
|
||||
|
||||
if (!Database.notificationSettings.getHasActiveNotificationsForEvent('onPodcastEpisodeDownloaded')) {
|
||||
Logger.debug(`[NotificationManager] onPodcastEpisodeDownloaded: No active notifications`)
|
||||
return
|
||||
}
|
||||
|
||||
Logger.debug(`[NotificationManager] onPodcastEpisodeDownloaded: Episode "${episode.title}" for podcast ${libraryItem.media.metadata.title}`)
|
||||
const library = await Database.libraryModel.getOldById(libraryItem.libraryId)
|
||||
const eventData = {
|
||||
|
@ -36,10 +41,60 @@ class NotificationManager {
|
|||
this.triggerNotification('onPodcastEpisodeDownloaded', eventData)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {import('../objects/Backup')} backup
|
||||
* @param {number} totalBackupCount
|
||||
* @param {boolean} removedOldest - If oldest backup was removed
|
||||
*/
|
||||
async onBackupCompleted(backup, totalBackupCount, removedOldest) {
|
||||
if (!Database.notificationSettings.isUseable) return
|
||||
|
||||
if (!Database.notificationSettings.getHasActiveNotificationsForEvent('onBackupCompleted')) {
|
||||
Logger.debug(`[NotificationManager] onBackupCompleted: No active notifications`)
|
||||
return
|
||||
}
|
||||
|
||||
Logger.debug(`[NotificationManager] onBackupCompleted: Backup completed`)
|
||||
const eventData = {
|
||||
completionTime: backup.createdAt,
|
||||
backupPath: backup.fullPath,
|
||||
backupSize: backup.fileSize,
|
||||
backupCount: totalBackupCount || 'Invalid',
|
||||
removedOldest: removedOldest || 'false'
|
||||
}
|
||||
this.triggerNotification('onBackupCompleted', eventData)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} errorMsg
|
||||
*/
|
||||
async onBackupFailed(errorMsg) {
|
||||
if (!Database.notificationSettings.isUseable) return
|
||||
|
||||
if (!Database.notificationSettings.getHasActiveNotificationsForEvent('onBackupFailed')) {
|
||||
Logger.debug(`[NotificationManager] onBackupFailed: No active notifications`)
|
||||
return
|
||||
}
|
||||
|
||||
Logger.debug(`[NotificationManager] onBackupFailed: Backup failed (${errorMsg})`)
|
||||
const eventData = {
|
||||
errorMsg: errorMsg || 'Backup failed'
|
||||
}
|
||||
this.triggerNotification('onBackupFailed', eventData)
|
||||
}
|
||||
|
||||
onTest() {
|
||||
this.triggerNotification('onTest')
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} eventName
|
||||
* @param {any} eventData
|
||||
* @param {boolean} [intentionallyFail=false] - If true, will intentionally fail the notification
|
||||
*/
|
||||
async triggerNotification(eventName, eventData, intentionallyFail = false) {
|
||||
if (!Database.notificationSettings.isUseable) return
|
||||
|
||||
|
@ -52,7 +107,8 @@ class NotificationManager {
|
|||
const success = intentionallyFail ? false : await this.sendNotification(notification, eventData)
|
||||
|
||||
notification.updateNotificationFired(success)
|
||||
if (!success) { // Failed notification
|
||||
if (!success) {
|
||||
// Failed notification
|
||||
if (notification.numConsecutiveFailedAttempts >= Database.notificationSettings.maxFailedAttempts) {
|
||||
Logger.error(`[NotificationManager] triggerNotification: ${notification.eventName}/${notification.id} reached max failed attempts`)
|
||||
notification.enabled = false
|
||||
|
@ -68,7 +124,12 @@ class NotificationManager {
|
|||
this.notificationFinished()
|
||||
}
|
||||
|
||||
// Return TRUE if notification should be triggered now
|
||||
/**
|
||||
*
|
||||
* @param {string} eventName
|
||||
* @param {any} eventData
|
||||
* @returns {boolean} - TRUE if notification should be triggered now
|
||||
*/
|
||||
checkTriggerNotification(eventName, eventData) {
|
||||
if (this.sendingNotification) {
|
||||
if (this.notificationQueue.length >= Database.notificationSettings.maxNotificationQueue) {
|
||||
|
@ -87,7 +148,8 @@ class NotificationManager {
|
|||
// Delay between events then run next notification in queue
|
||||
setTimeout(() => {
|
||||
this.sendingNotification = false
|
||||
if (this.notificationQueue.length) { // Send next notification in queue
|
||||
if (this.notificationQueue.length) {
|
||||
// Send next notification in queue
|
||||
const nextNotificationEvent = this.notificationQueue.shift()
|
||||
this.triggerNotification(nextNotificationEvent.eventName, nextNotificationEvent.eventData)
|
||||
}
|
||||
|
@ -95,7 +157,7 @@ class NotificationManager {
|
|||
}
|
||||
|
||||
sendTestNotification(notification) {
|
||||
const eventData = notificationData.events.find(e => e.name === notification.eventName)
|
||||
const eventData = notificationData.events.find((e) => e.name === notification.eventName)
|
||||
if (!eventData) {
|
||||
Logger.error(`[NotificationManager] sendTestNotification: Event not found ${notification.eventName}`)
|
||||
return false
|
||||
|
@ -106,13 +168,16 @@ class NotificationManager {
|
|||
|
||||
sendNotification(notification, eventData) {
|
||||
const payload = notification.getApprisePayload(eventData)
|
||||
return axios.post(Database.notificationSettings.appriseApiUrl, payload, { timeout: 6000 }).then((response) => {
|
||||
Logger.debug(`[NotificationManager] sendNotification: ${notification.eventName}/${notification.id} response=`, response.data)
|
||||
return true
|
||||
}).catch((error) => {
|
||||
Logger.error(`[NotificationManager] sendNotification: ${notification.eventName}/${notification.id} error=`, error)
|
||||
return false
|
||||
})
|
||||
return axios
|
||||
.post(Database.notificationSettings.appriseApiUrl, payload, { timeout: 6000 })
|
||||
.then((response) => {
|
||||
Logger.debug(`[NotificationManager] sendNotification: ${notification.eventName}/${notification.id} response=`, response.data)
|
||||
return true
|
||||
})
|
||||
.catch((error) => {
|
||||
Logger.error(`[NotificationManager] sendNotification: ${notification.eventName}/${notification.id} error=`, error)
|
||||
return false
|
||||
})
|
||||
}
|
||||
}
|
||||
module.exports = NotificationManager
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue