Run golder using sub-process
This commit is contained in:
@@ -9,49 +9,13 @@ import Foundation
|
||||
import KissMe
|
||||
|
||||
|
||||
enum RunMode: String {
|
||||
case simulator = "sim"
|
||||
case runner = "run"
|
||||
}
|
||||
|
||||
|
||||
struct Model: Codable {
|
||||
let indexSets: [IndexSet]
|
||||
|
||||
struct IndexSet: Codable {
|
||||
let name: String
|
||||
let memo: String
|
||||
let config: String?
|
||||
let runner: String
|
||||
let weight: Double
|
||||
|
||||
func build(date: Date) -> (URL, [String])? {
|
||||
guard let _ = name.kmiIndex else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let command = URL.currentDirectory().appending(path: runner)
|
||||
|
||||
let day = date.yyyyMMdd
|
||||
let time = date.HHmmss
|
||||
var args: [String] = [name, day, time]
|
||||
|
||||
if let config = config {
|
||||
args.append(config)
|
||||
}
|
||||
return (command, args)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class KissMatrix {
|
||||
|
||||
func run() {
|
||||
|
||||
func printUsage() {
|
||||
let appName = (CommandLine.arguments[0] as NSString).lastPathComponent
|
||||
print("\(appName) [sim|run] model.json [yyyyMMdd] [HHmmss]")
|
||||
printError("\(appName) [sim|run] model.json [yyyyMMdd] [HHmmss]")
|
||||
}
|
||||
|
||||
guard CommandLine.argc >= 3 else {
|
||||
@@ -71,12 +35,12 @@ class KissMatrix {
|
||||
return
|
||||
}
|
||||
guard let (year, month, day) = CommandLine.arguments[3].yyyyMMdd else {
|
||||
print("Invalid [yyyyMMdd] argument")
|
||||
printError("Invalid [yyyyMMdd] argument")
|
||||
printUsage()
|
||||
return
|
||||
}
|
||||
guard let (hour, min, sec) = CommandLine.arguments[4].HHmmss else {
|
||||
print("Invalid [HHmmss] argument")
|
||||
printError("Invalid [HHmmss] argument")
|
||||
printUsage()
|
||||
return
|
||||
}
|
||||
@@ -92,23 +56,23 @@ class KissMatrix {
|
||||
|
||||
let modelJson = CommandLine.arguments[2]
|
||||
guard let model = loadModel(modelJson) else { return }
|
||||
print("Loaded \(model.indexSets.count) index set from \(modelJson)")
|
||||
printError("Loaded \(model.indexSets.count) index set from \(modelJson)")
|
||||
|
||||
let semaphore = DispatchSemaphore(value: 0)
|
||||
Task {
|
||||
let results = try await withThrowingTaskGroup(of: KissIndexResult?.self, returning: [KissIndexResult].self) { taskGroup in
|
||||
for indexSet in model.indexSets {
|
||||
guard let (runUrl, args) = indexSet.build(date: runDate) else {
|
||||
print("Cannot get command from \(indexSet.name)")
|
||||
printError("Cannot get command from \(indexSet.name)")
|
||||
continue
|
||||
}
|
||||
|
||||
taskGroup.addTask {
|
||||
do {
|
||||
let result = try await self.runIndex(runUrl, args: args)
|
||||
let result = try await self.runIndex(runUrl, args: args, date: runDate)
|
||||
return result
|
||||
} catch {
|
||||
print(error)
|
||||
printError(error)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -122,18 +86,19 @@ class KissMatrix {
|
||||
}
|
||||
|
||||
mergeResult(results)
|
||||
|
||||
semaphore.signal()
|
||||
}
|
||||
semaphore.wait()
|
||||
}
|
||||
|
||||
|
||||
private func runIndex(_ runUrl: URL, args: [String]) async throws -> KissIndexResult {
|
||||
private func runIndex(_ runUrl: URL, args: [String], date: Date) async throws -> KissIndexResult {
|
||||
assert(args.count >= 3)
|
||||
return try await withUnsafeThrowingContinuation { continuation in
|
||||
let kmi = args[0]
|
||||
|
||||
let outputUrl = KissMatrix.indexLogFile(kmi)
|
||||
let outputUrl = KissMatrix.indexLogFile(kmi, date: date)
|
||||
FileManager.default.createFile(atPath: outputUrl.path, contents: nil)
|
||||
let output = FileHandle(forWritingAtPath: outputUrl.path)
|
||||
|
||||
@@ -143,17 +108,17 @@ class KissMatrix {
|
||||
task.arguments = args
|
||||
task.standardOutput = output
|
||||
task.standardError = FileHandle.standardError
|
||||
//task.qualityOfService = .userInitiated
|
||||
task.qualityOfService = .default
|
||||
|
||||
print("curPath: \(task.currentDirectoryPath)")
|
||||
print("runPath: \(runUrl)")
|
||||
print("args: \(args)")
|
||||
printError("curPath: \(task.currentDirectoryPath)")
|
||||
printError("runPath: \(runUrl.path)")
|
||||
printError("args: \(args)")
|
||||
|
||||
do {
|
||||
try task.run()
|
||||
task.waitUntilExit()
|
||||
} catch {
|
||||
print("run error \(error)")
|
||||
printError("run error \(error)")
|
||||
continuation.resume(throwing: error)
|
||||
}
|
||||
|
||||
@@ -172,7 +137,7 @@ class KissMatrix {
|
||||
let indexResult = try JSONDecoder().decode(KissIndexResult.self, from: data)
|
||||
continuation.resume(returning: indexResult)
|
||||
} catch {
|
||||
print("jsonError \(kmi)")
|
||||
printError("jsonError \(kmi)")
|
||||
continuation.resume(throwing: error)
|
||||
}
|
||||
}
|
||||
@@ -220,15 +185,14 @@ class KissMatrix {
|
||||
let model = try JSONDecoder().decode(Model.self, from: data)
|
||||
return model
|
||||
} catch {
|
||||
print(error)
|
||||
printError(error)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static func indexLogFile(_ kmi: String) -> URL {
|
||||
static func indexLogFile(_ kmi: String, date: Date) -> URL {
|
||||
let subPath = "log/index"
|
||||
let date = Date()
|
||||
let subFile = "\(subPath)/\(date.yyyyMMdd_HHmmssSSSS_forFile)-\(kmi).log"
|
||||
|
||||
let fileUrl = URL.currentDirectory().appending(path: subFile)
|
||||
@@ -242,3 +206,44 @@ class KissMatrix {
|
||||
try? FileManager.default.createDirectory(at: subPath, withIntermediateDirectories: true)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
enum RunMode: String {
|
||||
case simulator = "sim"
|
||||
case runner = "run"
|
||||
}
|
||||
|
||||
|
||||
struct Model: Decodable {
|
||||
let indexSets: [IndexSet]
|
||||
|
||||
struct IndexSet: Codable {
|
||||
/// Index Set 이름
|
||||
let name: String
|
||||
/// Index Set 에 대한 설명
|
||||
let memo: String
|
||||
/// config.json 설정
|
||||
let config: String?
|
||||
/// Index 수집을 수행하는 Runner app
|
||||
let runner: String
|
||||
/// 보정용 가중치
|
||||
let weight: Double
|
||||
|
||||
func build(date: Date) -> (URL, [String])? {
|
||||
guard let _ = name.kmiIndex else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let command = URL.currentDirectory().appending(path: runner)
|
||||
|
||||
let day = date.yyyyMMdd
|
||||
let time = date.HHmmss
|
||||
var args: [String] = [name, day, time]
|
||||
|
||||
if let config = config {
|
||||
args.append(config)
|
||||
}
|
||||
return (command, args)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user