Skip to content

Commit

Permalink
Fix setupJobs in the ConversionQueue and I realized that by performin…
Browse files Browse the repository at this point in the history
…g asyncAfter on a thread other than main should actually work
  • Loading branch information
Ponyboy47 committed Jun 10, 2017
1 parent 0b5596f commit f8174a9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
14 changes: 12 additions & 2 deletions Sources/monitr/ConversionQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,19 @@ class ConversionQueue: JSONConvertible {
}

private static func setupJobs(_ jobs: [BaseConvertibleMedia]) -> [ConvertibleMedia] {
return jobs
var js: [ConvertibleMedia] = []
for j in jobs {
let ext = j.path.extension ?? ""
do {
if Video.isSupported(ext: ext) {
js.append(try Video(j.path))
} else if Audio.isSupported(ext: ext) {
js.append(try Audio(j.path))
}
} catch {}
}
return js
}

public func encoded() -> JSON {
var js: [BaseConvertibleMedia] = []
for j in jobs {
Expand Down
19 changes: 18 additions & 1 deletion Sources/monitr/Monitr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import Foundation
import PathKit
import Async
import Cron
#if os(Linux)
import Dispatch
#endif

enum MonitrError: Error {
enum MissingDependency: Error {
Expand All @@ -37,6 +40,8 @@ final class Monitr: DirectoryMonitorDelegate {

/// The queue of conversion jobs
var conversionQueue: ConversionQueue?
private var cronStart: CronJob?
private var cronEnd: CronJob?

/// Whether or not media is currently being migrated to Plex. Automatically
/// runs a new again if new media has been added since the run routine began
Expand All @@ -60,14 +65,26 @@ final class Monitr: DirectoryMonitorDelegate {
self.statistics = try Statistic(statFile)
}

if self.config.convert {
if config.convert {
try checkConversionDependencies()
}

let conversionQueueFile = config.configFile.parent + ConversionQueue.filename
if conversionQueueFile.exists && conversionQueueFile.isFile {
self.conversionQueue = try ConversionQueue(conversionQueueFile)
}

if config.convert && !config.convertImmediately {
log.info("Setting up the conversion queue cron jobs")
self.cronStart = CronJob(pattern: config.convertCronStart, queue: .global(qos: .background)) {
self.conversionQueue?.start()
}
self.cronEnd = CronJob(pattern: config.convertCronEnd, queue: .global(qos: .background)) {
self.conversionQueue?.stop = true
}
let next = MediaDuration(double: cronStart!.pattern.next(Date())!.date!.timeIntervalSinceNow)
log.info("Set up conversion cron job! It will begin in \(next.description)")
}
}

private func checkConversionDependencies() throws {
Expand Down
29 changes: 1 addition & 28 deletions Sources/monitr/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,34 +221,6 @@ if saveConfig {
do {
monitr = try Monitr(config)
log.verbose("Sucessfully created the Monitr object from the config")

let keepalive = AsyncGroup()

func schedule(pattern: DatePattern, group: AsyncGroup = keepalive, job: @escaping @convention(block) () -> ()) {
guard let next = pattern.next()?.date else {
print("No next execution date could be determined")
return
}

let interval = next.timeIntervalSinceNow
group.enter()
sleep(UInt32(interval))
job()
group.leave()
schedule(pattern: pattern, group: group, job: job)
}

if config.convert && !config.convertImmediately {
log.info("Setting up the conversion queue cron jobs")
schedule(pattern: config.convertCronStart, job: {
monitr.conversionQueue?.start()
})
schedule(pattern: config.convertCronEnd, job: {
monitr.conversionQueue?.stop = true
})
let next = MediaDuration(double: config.convertCronStart.next(Date())!.date!.timeIntervalSinceNow)
log.info("Set up conversion cron job! It will begin in \(next.description)")
}

// Run once and then start monitoring regularly
log.info("Running Monitr once for startup!")
Expand All @@ -269,6 +241,7 @@ do {
}

// This keeps the program alive until ctrl-c is pressed or a signal is sent to the process
let keepalive = AsyncGroup()
keepalive.enter()
keepalive.wait()
} catch {
Expand Down

0 comments on commit f8174a9

Please sign in to comment.