Skip to content

Commit

Permalink
Reduced some duplicate code related to conversion config structs and …
Browse files Browse the repository at this point in the history
…bumped version
  • Loading branch information
Ponyboy47 committed Jun 29, 2017
1 parent 1218cc5 commit a0a535a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 34 deletions.
12 changes: 4 additions & 8 deletions Sources/monitr/ConversionQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,13 @@ class ConversionQueue: JSONConvertible {
conversionGroup.utility {
self.statistics.measure(.convert) {
do {
var config: ConversionConfig?
if next is Video {
next = try next.convert(self.videoConversionConfig, self.log)
config = self.videoConversionConfig
} else if next is Audio {
next = try next.convert(self.audioConversionConfig, self.log)
} else {
// We shouldn't be able to convert anything else, and we
// shouldn't have even put anything else in the queue.
// Calling convert on a BaseMedia object should throw an
// Unimplemented Error
next = try next.convert(nil, self.log)
config = self.audioConversionConfig
}
next = try next.convert(config, self.log)
try self.finish(next)
} catch MediaError.notImplemented {
self.log.warning("Media that is neither Video nor Audio somehow ended up in the conversion queue! => \(next.path)")
Expand Down
42 changes: 16 additions & 26 deletions Sources/monitr/Monitr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ enum MonitrError: Error {
/// Checks the downloads directory for new content to add to Plex
final class Monitr: DirectoryMonitorDelegate {
/// The current version of monitr
static var version: String = "0.5.2"
static var version: String = "0.5.3"

/// The configuration to use for the monitor
private var config: Config
Expand Down Expand Up @@ -386,34 +386,24 @@ final class Monitr: DirectoryMonitorDelegate {
let convertGroup = AsyncGroup()
var simultaneousConversions: Int = 0
for var m in mediaToConvert {
var config: ConversionConfig?
if m is Video {
simultaneousConversions += 1
convertGroup.utility {
self.statistics.measure(.convert) {
do {
m = try m.convert(videoConfig, self.config.log)
} catch {
self.config.log.warning("Failed to convert video file: \(m.path)")
self.config.log.error(error)
failedMedia.append(m)
}
}
simultaneousConversions -= 1
}
config = videoConfig
} else if m is Audio {
simultaneousConversions += 1
convertGroup.utility {
self.statistics.measure(.convert) {
do {
m = try m.convert(audioConfig, self.config.log)
} catch {
self.config.log.warning("Failed to convert audio file: \(m.path)")
self.config.log.error(error)
failedMedia.append(m)
}
config = audioConfig
}
simultaneousConversions += 1
convertGroup.utility {
self.statistics.measure(.convert) {
do {
m = try m.convert(config, self.config.log)
} catch {
self.config.log.warning("Failed to convert file: \(m.path)")
self.config.log.error(error)
failedMedia.append(m)
}
simultaneousConversions -= 1
}
simultaneousConversions -= 1
}
self.config.log.verbose("Currently running \(simultaneousConversions) simultaneous conversion jobs.")

Expand All @@ -435,7 +425,7 @@ final class Monitr: DirectoryMonitorDelegate {
// increment when a thread starts and decrement when it
// finishes)
while simultaneousConversions >= self.config.convertThreads {
self.config.log.info("Maximum number conversion threads (\(self.config.convertThreads)) reached. Waiting 60 seconds and checking to see if any have finished.")
self.config.log.info("Maximum number of conversion threads (\(self.config.convertThreads) threads) reached. Waiting 60 seconds and checking to see if any have finished.")
convertGroup.wait(seconds: 60)
}
}
Expand Down

0 comments on commit a0a535a

Please sign in to comment.