Skip to content

Commit

Permalink
Update:Replace library scan toast with task manager advplyr#1279
Browse files Browse the repository at this point in the history
  • Loading branch information
advplyr committed Oct 20, 2023
1 parent 6f65350 commit bef6549
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 19 deletions.
23 changes: 19 additions & 4 deletions client/components/cards/ItemTaskRunningCard.vue
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<template>
<div class="flex items-center px-1 overflow-hidden">
<div class="w-8 flex items-center justify-center">
<!-- <div class="text-lg"> -->
<span v-if="isFinished" :class="taskIconStatus" class="material-icons text-base">{{ actionIcon }}</span>
<widgets-loading-spinner v-else />
<!-- </div> -->
</div>
<div class="flex-grow px-2 taskRunningCardContent">
<p class="truncate text-sm">{{ title }}</p>

<p class="truncate text-xs text-gray-300">{{ description }}</p>

<p v-if="isFailed && failedMessage" class="text-xs truncate text-red-500">{{ failedMessage }}</p>
<p v-else-if="!isFinished && cancelingScan" class="text-xs truncate">Canceling...</p>
</div>
<ui-btn v-if="userIsAdminOrUp && !isFinished && action === 'library-scan' && !cancelingScan" color="primary" :padding-y="1" :padding-x="1" class="text-xs w-16 max-w-16 truncate mr-1" @click.stop="cancelScan">{{ this.$strings.ButtonCancel }}</ui-btn>
</div>
</template>

Expand All @@ -25,9 +25,14 @@ export default {
}
},
data() {
return {}
return {
cancelingScan: false
}
},
computed: {
userIsAdminOrUp() {
return this.$store.getters['user/getIsAdminOrUp']
},
title() {
return this.task.title || 'No Title'
},
Expand Down Expand Up @@ -78,7 +83,17 @@ export default {
return ''
}
},
methods: {},
methods: {
cancelScan() {
const libraryId = this.task?.data?.libraryId
if (!libraryId) {
console.error('No library id in library-scan task', this.task)
return
}
this.cancelingScan = true
this.$root.socket.emit('cancel_scan', libraryId)
}
},
mounted() {}
}
</script>
Expand Down
7 changes: 0 additions & 7 deletions client/layouts/default.vue
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,6 @@ export default {
init(payload) {
console.log('Init Payload', payload)
// Start scans currently running
if (payload.librariesScanning) {
payload.librariesScanning.forEach((libraryScan) => {
this.scanStart(libraryScan)
})
}
// Remove any current scans that are no longer running
var currentScans = [...this.$store.state.scanners.libraryScans]
currentScans.forEach((ls) => {
Expand Down
5 changes: 1 addition & 4 deletions server/managers/PodcastManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const LibraryFile = require('../objects/files/LibraryFile')
const PodcastEpisodeDownload = require('../objects/PodcastEpisodeDownload')
const PodcastEpisode = require('../objects/entities/PodcastEpisode')
const AudioFile = require('../objects/files/AudioFile')
const Task = require("../objects/Task")

class PodcastManager {
constructor(watcher, notificationManager) {
Expand Down Expand Up @@ -70,14 +69,12 @@ class PodcastManager {
return
}

const task = new Task()
const taskDescription = `Downloading episode "${podcastEpisodeDownload.podcastEpisode.title}".`
const taskData = {
libraryId: podcastEpisodeDownload.libraryId,
libraryItemId: podcastEpisodeDownload.libraryItemId,
}
task.setData('download-podcast-episode', 'Downloading Episode', taskDescription, false, taskData)
TaskManager.addTask(task)
const task = TaskManager.createAndAddTask('download-podcast-episode', 'Downloading Episode', taskDescription, false, taskData)

SocketAuthority.emitter('episode_download_started', podcastEpisodeDownload.toJSONForClient())
this.currentDownload = podcastEpisodeDownload
Expand Down
16 changes: 16 additions & 0 deletions server/managers/TaskManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,21 @@ class TaskManager {
SocketAuthority.emitter('task_finished', task.toJSON())
}
}

/**
* Create new task and add
*
* @param {string} action
* @param {string} title
* @param {string} description
* @param {boolean} showSuccess
* @param {Object} [data]
*/
createAndAddTask(action, title, description, showSuccess, data = {}) {
const task = new Task()
task.setData(action, title, description, showSuccess, data)
this.addTask(task)
return task
}
}
module.exports = new TaskManager()
11 changes: 10 additions & 1 deletion server/scanner/LibraryScan.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const date = require('../libs/dateAndTime')
const Logger = require('../Logger')
const Library = require('../objects/Library')
const { LogLevel } = require('../utils/constants')
const { secondsToTimestamp } = require('../utils/index')
const { secondsToTimestamp, elapsedPretty } = require('../utils/index')

class LibraryScan {
constructor() {
Expand Down Expand Up @@ -67,6 +67,15 @@ class LibraryScan {
get logFilename() {
return date.format(new Date(), 'YYYY-MM-DD') + '_' + this.id + '.txt'
}
get scanResultsString() {
if (this.error) return this.error
const strs = []
if (this.resultsAdded) strs.push(`${this.resultsAdded} added`)
if (this.resultsUpdated) strs.push(`${this.resultsUpdated} updated`)
if (this.resultsMissing) strs.push(`${this.resultsMissing} missing`)
if (!strs.length) return `Everything was up to date (${elapsedPretty(this.elapsed / 1000)})`
return strs.join(', ') + ` (${elapsedPretty(this.elapsed / 1000)})`
}

toJSON() {
return {
Expand Down
15 changes: 12 additions & 3 deletions server/scanner/LibraryScanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const fileUtils = require('../utils/fileUtils')
const scanUtils = require('../utils/scandir')
const { LogLevel, ScanResult } = require('../utils/constants')
const libraryFilters = require('../utils/queries/libraryFilters')
const TaskManager = require('../managers/TaskManager')
const LibraryItemScanner = require('./LibraryItemScanner')
const LibraryScan = require('./LibraryScan')
const LibraryItemScanData = require('./LibraryItemScanData')
Expand Down Expand Up @@ -68,7 +69,12 @@ class LibraryScanner {
libraryScan.verbose = true
this.librariesScanning.push(libraryScan.getScanEmitData)

SocketAuthority.emitter('scan_start', libraryScan.getScanEmitData)
const taskData = {
libraryId: library.id,
libraryName: library.name,
libraryMediaType: library.mediaType
}
const task = TaskManager.createAndAddTask('library-scan', `Scanning "${library.name}" library`, null, true, taskData)

Logger.info(`[LibraryScanner] Starting${forceRescan ? ' (forced)' : ''} library scan ${libraryScan.id} for ${libraryScan.libraryName}`)

Expand All @@ -85,9 +91,11 @@ class LibraryScanner {
this.librariesScanning = this.librariesScanning.filter(ls => ls.id !== library.id)

if (canceled && !libraryScan.totalResults) {
task.setFinished('Scan canceled')
TaskManager.taskFinished(task)

const emitData = libraryScan.getScanEmitData
emitData.results = null
SocketAuthority.emitter('scan_complete', emitData)
return
}

Expand All @@ -98,7 +106,8 @@ class LibraryScanner {
}
await Database.libraryModel.updateFromOld(library)

SocketAuthority.emitter('scan_complete', libraryScan.getScanEmitData)
task.setFinished(libraryScan.scanResultsString)
TaskManager.taskFinished(task)

if (libraryScan.totalResults) {
libraryScan.saveLog()
Expand Down
3 changes: 3 additions & 0 deletions server/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ module.exports.getId = (prepend = '') => {
}

function elapsedPretty(seconds) {
if (seconds > 0 && seconds < 1) {
return `${Math.floor(seconds * 1000)} ms`
}
if (seconds < 60) {
return `${Math.floor(seconds)} sec`
}
Expand Down

0 comments on commit bef6549

Please sign in to comment.