Skip to content

Commit

Permalink
Update Task object to handle translation keys with subs
Browse files Browse the repository at this point in the history
  • Loading branch information
advplyr committed Sep 20, 2024
1 parent bb481cc commit 8512d5e
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 54 deletions.
7 changes: 6 additions & 1 deletion server/Watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,12 @@ class FolderWatcher extends EventEmitter {
libraryId,
libraryName: libwatcher.name
}
this.pendingTask = TaskManager.createAndAddTask('watcher-scan', `Scanning file changes in "${libwatcher.name}"`, null, true, taskData)
const taskTitleString = {
text: `Scanning file changes in "${libwatcher.name}"`,
key: 'MessageTaskScanningFileChanges',
subs: [libwatcher.name]
}
this.pendingTask = TaskManager.createAndAddTask('watcher-scan', taskTitleString, null, true, taskData)
}
this.pendingFileUpdates.push({
path,
Expand Down
49 changes: 41 additions & 8 deletions server/managers/AbMergeManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ class AbMergeManager {
* @returns {Promise<void>}
*/
cancelEncode(task) {
task.setFailed('Task canceled by user')
const taskFailedString = {
text: 'Task canceled by user',
key: 'MessageTaskCanceledByUser'
}
task.setFailed(taskFailedString)
return this.removeTask(task, true)
}

Expand Down Expand Up @@ -76,8 +80,17 @@ class AbMergeManager {
duration: libraryItem.media.duration,
encodeOptions: options
}
const taskDescription = `Encoding audiobook "${libraryItem.media.metadata.title}" into a single m4b file.`
task.setData('encode-m4b', 'Encoding M4b', taskDescription, false, taskData)

const taskTitleString = {
text: 'Encoding M4b',
key: 'MessageTaskEncodingM4b'
}
const taskDescriptionString = {
text: `Encoding audiobook "${libraryItem.media.metadata.title}" into a single m4b file.`,
key: 'MessageTaskEncodingM4bDescription',
subs: [libraryItem.media.metadata.title]
}
task.setData('encode-m4b', taskTitleString, taskDescriptionString, false, taskData)
TaskManager.addTask(task)
Logger.info(`Start m4b encode for ${libraryItem.id} - TaskId: ${task.id}`)

Expand All @@ -98,15 +111,23 @@ class AbMergeManager {
// Make sure the target directory is writable
if (!(await isWritable(task.data.libraryItemDir))) {
Logger.error(`[AbMergeManager] Target directory is not writable: ${task.data.libraryItemDir}`)
task.setFailed('Target directory is not writable')
const taskFailedString = {
text: 'Target directory is not writable',
key: 'MessageTaskTargetDirectoryNotWritable'
}
task.setFailed(taskFailedString)
this.removeTask(task, true)
return
}

// Create ffmetadata file
if (!(await ffmpegHelpers.writeFFMetadataFile(task.data.ffmetadataObject, task.data.chapters, task.data.ffmetadataPath))) {
Logger.error(`[AudioMetadataManager] Failed to write ffmetadata file for audiobook "${task.data.libraryItemId}"`)
task.setFailed('Failed to write metadata file.')
const taskFailedString = {
text: 'Failed to write metadata file',
key: 'MessageTaskFailedToWriteMetadataFile'
}
task.setFailed(taskFailedString)
this.removeTask(task, true)
return
}
Expand Down Expand Up @@ -137,7 +158,11 @@ class AbMergeManager {
Logger.info(`[AbMergeManager] Task cancelled ${task.id}`)
} else {
Logger.error(`[AbMergeManager] mergeAudioFiles failed`, error)
task.setFailed('Failed to merge audio files')
const taskFailedString = {
text: 'Failed to merge audio files',
key: 'MessageTaskFailedToMergeAudioFiles'
}
task.setFailed(taskFailedString)
this.removeTask(task, true)
}
return
Expand All @@ -164,7 +189,11 @@ class AbMergeManager {
Logger.info(`[AbMergeManager] Task cancelled ${task.id}`)
} else {
Logger.error(`[AbMergeManager] Failed to write metadata to file "${task.data.tempFilepath}"`)
task.setFailed('Failed to write metadata to m4b file')
const taskFailedString = {
text: 'Failed to write metadata to m4b file',
key: 'MessageTaskFailedToWriteMetadataToM4bFile'
}
task.setFailed(taskFailedString)
this.removeTask(task, true)
}
return
Expand Down Expand Up @@ -196,7 +225,11 @@ class AbMergeManager {
await fs.remove(task.data.tempFilepath)
} catch (err) {
Logger.error(`[AbMergeManager] Failed to move m4b from ${task.data.tempFilepath} to ${task.data.targetFilepath}`, err)
task.setFailed('Failed to move m4b file')
const taskFailedString = {
text: 'Failed to move m4b file',
key: 'MessageTaskFailedToMoveM4bFile'
}
task.setFailed(taskFailedString)
this.removeTask(task, true)
return
}
Expand Down
25 changes: 17 additions & 8 deletions server/managers/AudioMetadataManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,17 @@ class AudioMetadataMangaer {
},
duration: libraryItem.media.duration
}
const taskDescription = `Embedding metadata in audiobook "${libraryItem.media.metadata.title}".`
task.setData('embed-metadata', 'Embedding Metadata', taskDescription, false, taskData)

const taskTitleString = {
text: 'Embedding Metadata',
key: 'MessageTaskEmbeddingMetadata'
}
const taskDescriptionString = {
text: `Embedding metadata in audiobook "${libraryItem.media.metadata.title}".`,
key: 'MessageTaskEmbeddingMetadataDescription',
subs: [libraryItem.media.metadata.title]
}
task.setData('embed-metadata', taskTitleString, taskDescriptionString, false, taskData)

if (this.tasksRunning.length >= this.MAX_CONCURRENT_TASKS) {
Logger.info(`[AudioMetadataManager] Queueing embed metadata for audiobook "${libraryItem.media.metadata.title}"`)
Expand All @@ -123,7 +132,7 @@ class AudioMetadataMangaer {
Logger.debug(`[AudioMetadataManager] Target directory ${task.data.libraryItemDir} writable: ${targetDirWritable}`)
if (!targetDirWritable) {
Logger.error(`[AudioMetadataManager] Target directory is not writable: ${task.data.libraryItemDir}`)
task.setFailed('Target directory is not writable')
task.setFailedText('Target directory is not writable')
this.handleTaskFinished(task)
return
}
Expand All @@ -134,7 +143,7 @@ class AudioMetadataMangaer {
await fs.access(af.path, fs.constants.W_OK)
} catch (err) {
Logger.error(`[AudioMetadataManager] Audio file is not writable: ${af.path}`)
task.setFailed(`Audio file "${Path.basename(af.path)}" is not writable`)
task.setFailedText(`Audio file "${Path.basename(af.path)}" is not writable`)
this.handleTaskFinished(task)
return
}
Expand All @@ -148,7 +157,7 @@ class AudioMetadataMangaer {
cacheDirCreated = true
} catch (err) {
Logger.error(`[AudioMetadataManager] Failed to create cache directory ${task.data.itemCachePath}`, err)
task.setFailed('Failed to create cache directory')
task.setFailedText('Failed to create cache directory')
this.handleTaskFinished(task)
return
}
Expand All @@ -159,7 +168,7 @@ class AudioMetadataMangaer {
const success = await ffmpegHelpers.writeFFMetadataFile(task.data.metadataObject, task.data.chapters, ffmetadataPath)
if (!success) {
Logger.error(`[AudioMetadataManager] Failed to write ffmetadata file for audiobook "${task.data.libraryItemId}"`)
task.setFailed('Failed to write metadata file.')
task.setFailedText('Failed to write metadata file.')
this.handleTaskFinished(task)
return
}
Expand All @@ -181,7 +190,7 @@ class AudioMetadataMangaer {
Logger.debug(`[AudioMetadataManager] Backed up audio file at "${backupFilePath}"`)
} catch (err) {
Logger.error(`[AudioMetadataManager] Failed to backup audio file "${af.path}"`, err)
task.setFailed(`Failed to backup audio file "${Path.basename(af.path)}"`)
task.setFailedText(`Failed to backup audio file "${Path.basename(af.path)}"`)
this.handleTaskFinished(task)
return
}
Expand All @@ -195,7 +204,7 @@ class AudioMetadataMangaer {
Logger.info(`[AudioMetadataManager] Successfully tagged audio file "${af.path}"`)
} catch (err) {
Logger.error(`[AudioMetadataManager] Failed to tag audio file "${af.path}"`, err)
task.setFailed(`Failed to tag audio file "${Path.basename(af.path)}"`)
task.setFailedText(`Failed to tag audio file "${Path.basename(af.path)}"`)
this.handleTaskFinished(task)
return
}
Expand Down
72 changes: 64 additions & 8 deletions server/managers/PodcastManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,20 @@ class PodcastManager {
return
}

const taskDescription = `Downloading episode "${podcastEpisodeDownload.podcastEpisode.title}".`
const taskData = {
libraryId: podcastEpisodeDownload.libraryId,
libraryItemId: podcastEpisodeDownload.libraryItemId
}
const task = TaskManager.createAndAddTask('download-podcast-episode', 'Downloading Episode', taskDescription, false, taskData)
const taskTitleString = {
text: 'Downloading episode',
key: 'MessageDownloadingEpisode'
}
const taskDescriptionString = {
text: `Downloading episode "${podcastEpisodeDownload.podcastEpisode.title}".`,
key: 'MessageTaskDownloadingEpisodeDescription',
subs: [podcastEpisodeDownload.podcastEpisode.title]
}
const task = TaskManager.createAndAddTask('download-podcast-episode', taskTitleString, taskDescriptionString, false, taskData)

SocketAuthority.emitter('episode_download_started', podcastEpisodeDownload.toJSONForClient())
this.currentDownload = podcastEpisodeDownload
Expand Down Expand Up @@ -119,14 +127,14 @@ class PodcastManager {
if (!success) {
await fs.remove(this.currentDownload.targetPath)
this.currentDownload.setFinished(false)
task.setFailed('Failed to download episode')
task.setFailedText('Failed to download episode')
} else {
Logger.info(`[PodcastManager] Successfully downloaded podcast episode "${this.currentDownload.podcastEpisode.title}"`)
this.currentDownload.setFinished(true)
task.setFinished()
}
} else {
task.setFailed('Failed to download episode')
task.setFailedText('Failed to download episode')
this.currentDownload.setFinished(false)
}

Expand Down Expand Up @@ -407,13 +415,35 @@ class PodcastManager {
* @param {import('../managers/CronManager')} cronManager
*/
async createPodcastsFromFeedUrls(rssFeedUrls, folder, autoDownloadEpisodes, cronManager) {
const task = TaskManager.createAndAddTask('opml-import', 'OPML import', `Creating podcasts from ${rssFeedUrls.length} RSS feeds`, true, null)
const taskTitleString = {
text: 'OPML import',
key: 'MessageTaskOpmlImport'
}
const taskDescriptionString = {
text: `Creating podcasts from ${rssFeedUrls.length} RSS feeds`,
key: 'MessageTaskOpmlImportDescription',
subs: [rssFeedUrls.length]
}
const task = TaskManager.createAndAddTask('opml-import', taskTitleString, taskDescriptionString, true, null)
let numPodcastsAdded = 0
Logger.info(`[PodcastManager] createPodcastsFromFeedUrls: Importing ${rssFeedUrls.length} RSS feeds to folder "${folder.path}"`)
for (const feedUrl of rssFeedUrls) {
const feed = await getPodcastFeed(feedUrl).catch(() => null)
if (!feed?.episodes) {
TaskManager.createAndEmitFailedTask('opml-import-feed', 'OPML import feed', `Importing RSS feed "${feedUrl}"`, 'Failed to get podcast feed')
const taskTitleStringFeed = {
text: 'OPML import feed',
key: 'MessageTaskOpmlImportFeed'
}
const taskDescriptionStringFeed = {
text: `Importing RSS feed "${feedUrl}"`,
key: 'MessageTaskOpmlImportFeedDescription',
subs: [feedUrl]
}
const taskErrorString = {
text: 'Failed to get podcast feed',
key: 'MessageTaskOpmlImportFeedFailed'
}
TaskManager.createAndEmitFailedTask('opml-import-feed', taskTitleStringFeed, taskDescriptionStringFeed, taskErrorString)
Logger.error(`[PodcastManager] createPodcastsFromFeedUrls: Failed to get podcast feed for "${feedUrl}"`)
continue
}
Expand All @@ -429,7 +459,20 @@ class PodcastManager {
})) > 0
if (existingLibraryItem) {
Logger.error(`[PodcastManager] createPodcastsFromFeedUrls: Podcast already exists at path "${podcastPath}"`)
TaskManager.createAndEmitFailedTask('opml-import-feed', 'OPML import feed', `Creating podcast "${feed.metadata.title}"`, 'Podcast already exists at path')
const taskTitleStringFeed = {
text: 'OPML import feed',
key: 'MessageTaskOpmlImportFeed'
}
const taskDescriptionStringPodcast = {
text: `Creating podcast "${feed.metadata.title}"`,
key: 'MessageTaskOpmlImportFeedPodcastDescription',
subs: [feed.metadata.title]
}
const taskErrorString = {
text: 'Podcast already exists at path',
key: 'MessageTaskOpmlImportFeedPodcastExists'
}
TaskManager.createAndEmitFailedTask('opml-import-feed', taskTitleStringFeed, taskDescriptionStringPodcast, taskErrorString)
continue
}

Expand All @@ -442,7 +485,20 @@ class PodcastManager {
})
if (!successCreatingPath) {
Logger.error(`[PodcastManager] createPodcastsFromFeedUrls: Failed to create podcast folder at "${podcastPath}"`)
TaskManager.createAndEmitFailedTask('opml-import-feed', 'OPML import feed', `Creating podcast "${feed.metadata.title}"`, 'Failed to create podcast folder')
const taskTitleStringFeed = {
text: 'OPML import feed',
key: 'MessageTaskOpmlImportFeed'
}
const taskDescriptionStringPodcast = {
text: `Creating podcast "${feed.metadata.title}"`,
key: 'MessageTaskOpmlImportFeedPodcastDescription',
subs: [feed.metadata.title]
}
const taskErrorString = {
text: 'Failed to create podcast folder',
key: 'MessageTaskOpmlImportFeedPodcastFailed'
}
TaskManager.createAndEmitFailedTask('opml-import-feed', taskTitleStringFeed, taskDescriptionStringPodcast, taskErrorString)
continue
}

Expand Down
27 changes: 17 additions & 10 deletions server/managers/TaskManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
const SocketAuthority = require('../SocketAuthority')
const Task = require('../objects/Task')

/**
* @typedef TaskString
* @property {string} text
* @property {string} key
* @property {string[]} [subs]
*/

class TaskManager {
constructor() {
/** @type {Task[]} */
Expand Down Expand Up @@ -33,14 +40,14 @@ class TaskManager {
* Create new task and add
*
* @param {string} action
* @param {string} title
* @param {string} description
* @param {TaskString} titleString
* @param {TaskString|null} descriptionString
* @param {boolean} showSuccess
* @param {Object} [data]
*/
createAndAddTask(action, title, description, showSuccess, data = {}) {
createAndAddTask(action, titleString, descriptionString, showSuccess, data = {}) {
const task = new Task()
task.setData(action, title, description, showSuccess, data)
task.setData(action, titleString, descriptionString, showSuccess, data)
this.addTask(task)
return task
}
Expand All @@ -49,14 +56,14 @@ class TaskManager {
* Create new failed task and add
*
* @param {string} action
* @param {string} title
* @param {string} description
* @param {string} errorMessage
* @param {TaskString} titleString
* @param {TaskString|null} descriptionString
* @param {TaskString} errorMessageString
*/
createAndEmitFailedTask(action, title, description, errorMessage) {
createAndEmitFailedTask(action, titleString, descriptionString, errorMessageString) {
const task = new Task()
task.setData(action, title, description, false)
task.setFailed(errorMessage)
task.setData(action, titleString, descriptionString, false)
task.setFailedText(errorMessageString)
SocketAuthority.emitter('task_started', task.toJSON())
return task
}
Expand Down
Loading

0 comments on commit 8512d5e

Please sign in to comment.