Adjustments
This commit is contained in:
parent
5b964970a0
commit
3d64a99213
@ -41,7 +41,7 @@ class MetadataAndBaseInfoToCoverTask(@Autowired override var coordinator: Coordi
|
|||||||
val meta = events.findLast { it.data is MetadataPerformed }?.data as MetadataPerformed? ?: return null
|
val meta = events.findLast { it.data is MetadataPerformed }?.data as MetadataPerformed? ?: return null
|
||||||
val fileOut = events.findLast { it.data is VideoInfoPerformed }?.data as VideoInfoPerformed? ?: return null
|
val fileOut = events.findLast { it.data is VideoInfoPerformed }?.data as VideoInfoPerformed? ?: return null
|
||||||
|
|
||||||
val coverUrl = meta?.data?.cover
|
val coverUrl = meta.data?.cover
|
||||||
return if (coverUrl.isNullOrBlank()) {
|
return if (coverUrl.isNullOrBlank()) {
|
||||||
log.warn { "No cover available for ${baseInfo.title}" }
|
log.warn { "No cover available for ${baseInfo.title}" }
|
||||||
null
|
null
|
||||||
@ -49,7 +49,7 @@ class MetadataAndBaseInfoToCoverTask(@Autowired override var coordinator: Coordi
|
|||||||
CoverInfoPerformed(
|
CoverInfoPerformed(
|
||||||
status = Status.COMPLETED,
|
status = Status.COMPLETED,
|
||||||
url = coverUrl,
|
url = coverUrl,
|
||||||
outFileBaseName = baseInfo.title,
|
outFileBaseName = meta.data?.title ?: baseInfo.title,
|
||||||
outDir = fileOut.outDirectory,
|
outDir = fileOut.outDirectory,
|
||||||
derivedFromEventId = event.eventId
|
derivedFromEventId = event.eventId
|
||||||
)
|
)
|
||||||
|
|||||||
@ -42,26 +42,36 @@ class FileWatcherQueue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun removeFromQueue(file: File, onFileRemoved: (PendingFile) -> Unit) {
|
fun removeFromQueue(file: File, onFileRemoved: (PendingFile) -> Unit) {
|
||||||
val removedFile = fileChannel.findAndRemove { it.file == file }
|
if (file.isFile) {
|
||||||
removedFile?.let {
|
val removedFile = fileChannel.findAndRemove { it.file == file }
|
||||||
onFileRemoved(it)
|
removedFile.let {
|
||||||
|
it.forEach { file -> onFileRemoved(file) }
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
val removeFiles = fileChannel.findAndRemove { it.file.parentFile == file }
|
||||||
|
removeFiles.let {
|
||||||
|
it.forEach { file -> onFileRemoved(file) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Extension function to find and remove an element from the channel
|
// Extension function to find and remove an element from the channel
|
||||||
fun <T> Channel<T>.findAndRemove(predicate: (T) -> Boolean): T? {
|
fun <T> Channel<T>.findAndRemove(predicate: (T) -> Boolean): List<T> {
|
||||||
|
val forRemoved = mutableListOf<T>()
|
||||||
val items = mutableListOf<T>()
|
val items = mutableListOf<T>()
|
||||||
while (true) {
|
while (true) {
|
||||||
val item = tryReceive().getOrNull() ?: break
|
val item = tryReceive().getOrNull() ?: break
|
||||||
if (predicate(item)) {
|
if (predicate(item)) {
|
||||||
return item
|
forRemoved.add(item)
|
||||||
}
|
}
|
||||||
items.add(item)
|
items.add(item)
|
||||||
}
|
}
|
||||||
for (item in items) {
|
for (item in items) {
|
||||||
trySend(item).isSuccess
|
trySend(item).isSuccess
|
||||||
}
|
}
|
||||||
return null
|
return forRemoved
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -8,12 +8,13 @@ import kotlinx.coroutines.launch
|
|||||||
import mu.KotlinLogging
|
import mu.KotlinLogging
|
||||||
import no.iktdev.exfl.coroutines.Coroutines
|
import no.iktdev.exfl.coroutines.Coroutines
|
||||||
import no.iktdev.mediaprocessing.coordinator.Coordinator
|
import no.iktdev.mediaprocessing.coordinator.Coordinator
|
||||||
|
import no.iktdev.mediaprocessing.coordinator.log
|
||||||
import no.iktdev.mediaprocessing.shared.common.SharedConfig
|
import no.iktdev.mediaprocessing.shared.common.SharedConfig
|
||||||
import no.iktdev.mediaprocessing.shared.common.extended.isSupportedVideoFile
|
import no.iktdev.mediaprocessing.shared.common.extended.isSupportedVideoFile
|
||||||
import no.iktdev.mediaprocessing.shared.contract.ProcessType
|
import no.iktdev.mediaprocessing.shared.contract.ProcessType
|
||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
|
||||||
interface FileWatcherEvents {
|
interface FileWatcherEvents {
|
||||||
@ -50,26 +51,44 @@ class InputDirectoryWatcher(@Autowired var coordinator: Coordinator): FileWatche
|
|||||||
} else {
|
} else {
|
||||||
logger.info { "IO Event: ${it.kind}: ${it.file.name}" }
|
logger.info { "IO Event: ${it.kind}: ${it.file.name}" }
|
||||||
}
|
}
|
||||||
when (it.kind) {
|
try {
|
||||||
Deleted -> queue.removeFromQueue(it.file, this@InputDirectoryWatcher::onFileRemoved)
|
when (it.kind) {
|
||||||
Initialized -> { /* Do nothing */ }
|
Deleted -> removeFile(it.file)
|
||||||
else -> {
|
Initialized -> { /* Do nothing */ }
|
||||||
if (it.file.isFile && it.file.isSupportedVideoFile()) {
|
else -> {
|
||||||
queue.addToQueue(it.file, this@InputDirectoryWatcher::onFilePending, this@InputDirectoryWatcher::onFileAvailable)
|
val added = addFile(it.file)
|
||||||
} else if (it.file.isDirectory) {
|
if (!added) {
|
||||||
val supportedFiles = it.file.walkTopDown().filter { f -> f.isFile && f.isSupportedVideoFile() }
|
logger.info { "Ignoring event kind: ${it.kind.name} for file ${it.file.name} as it is not a supported video file" }
|
||||||
supportedFiles.forEach { sf ->
|
|
||||||
queue.addToQueue(sf, this@InputDirectoryWatcher::onFilePending, this@InputDirectoryWatcher::onFileAvailable)
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
logger.info { "Ignoring event kind: ${it.kind.name} for file ${it.file.name} as it is not a supported video file" }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun addFile(file: File): Boolean {
|
||||||
|
return if (file.isFile && file.isSupportedVideoFile()) {
|
||||||
|
log.info { "Adding ${file.name} to queue" }
|
||||||
|
queue.addToQueue(file, this@InputDirectoryWatcher::onFilePending, this@InputDirectoryWatcher::onFileAvailable)
|
||||||
|
true
|
||||||
|
} else if (file.isDirectory) {
|
||||||
|
log.info { "Searching for files in ${file.name}" }
|
||||||
|
val supportedFiles = file.walkTopDown().filter { f -> f.isFile && f.isSupportedVideoFile() }
|
||||||
|
supportedFiles.forEach { sf ->
|
||||||
|
log.info { "Adding ${sf.name} to queue from folder" }
|
||||||
|
queue.addToQueue(sf, this@InputDirectoryWatcher::onFilePending, this@InputDirectoryWatcher::onFileAvailable)
|
||||||
|
}
|
||||||
|
true
|
||||||
|
} else false
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun removeFile(file: File) {
|
||||||
|
queue.removeFromQueue(file, this@InputDirectoryWatcher::onFileRemoved)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onFileAvailable(file: PendingFile) {
|
override fun onFileAvailable(file: PendingFile) {
|
||||||
logger.info { "File available ${file.file.name}" }
|
logger.info { "File available ${file.file.name}" }
|
||||||
|
|
||||||
|
|||||||
@ -106,7 +106,7 @@ class Coordinator(): CoordinatorBase<PersistentProcessDataMessage, PersistentEve
|
|||||||
) + processKafkaEvents
|
) + processKafkaEvents
|
||||||
|
|
||||||
|
|
||||||
@Scheduled(fixedDelay = (5_000))
|
@Scheduled(fixedDelay = (5*6_0000))
|
||||||
fun checkForWork() {
|
fun checkForWork() {
|
||||||
log.info { "Checking if there is any work to do.." }
|
log.info { "Checking if there is any work to do.." }
|
||||||
readAllAvailableInQueue()
|
readAllAvailableInQueue()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user