Skip to content

Commit

Permalink
task execution without and prefixed logging
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveitaly committed Jan 1, 2023
1 parent 8d7a8d5 commit fc2de7f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
56 changes: 54 additions & 2 deletions Sources/hyper-focus/sleepwatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class SleepWatcher {
log("first wake of the day")

if let initialWake = configuration.initial_wake {
executeTask(initialWake)
// executeTask(initialWake)
executeTaskWithName(initialWake, "initial_wake")
}
}

Expand All @@ -39,7 +40,8 @@ class SleepWatcher {
return
}

executeTask(wakeScript)
// executeTask(wakeScript)
executeTaskWithName(wakeScript, "wake")
}

func executeTask(_ taskPath: String) {
Expand Down Expand Up @@ -77,4 +79,54 @@ class SleepWatcher {

log("script executed successfully")
}

func executeTaskWithName(_ rawCommandPath: String, _ taskName: String) {
let expandedCommandPath = NSString(string: rawCommandPath).expandingTildeInPath

if !FileManager.default.fileExists(atPath: expandedCommandPath) {
error("script does not exist: \(expandedCommandPath)")
return
}

log("running script \(expandedCommandPath)")

let process: Process = Process()
let pipe = Pipe()
let timeout = 120.0

// no user input
process.standardInput = FileHandle.nullDevice
process.standardOutput = pipe
process.standardError = pipe
process.arguments = ["-c", expandedCommandPath]
process.launchPath = "/bin/bash"

let timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.global())
timer.schedule(deadline: .now() + timeout)
timer.setEventHandler {
process.terminate()
}
timer.resume()

process.launch()
process.waitUntilExit()
timer.cancel()

let status = process.terminationStatus

if status != 0 {
error("script \(expandedCommandPath) exited with error code \(status)")
} else {
log("script executed successfully")
}

let data = pipe.fileHandleForReading.readDataToEndOfFile()
let resultString = String(data: data, encoding: .utf8)
let prefixedString = prefixLines(resultString!, taskName)
log("script output:\n\(prefixedString)")
}

func prefixLines(_ str: String, _ prefix: String) -> String {
return str.split(separator: "\n").map { "[task-runner] [\(prefix)] \($0)" }.joined(separator: "\n")
}
}
2 changes: 0 additions & 2 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
## Next

- [ ] execute wake and initial wake scripts
- [ ] This is working, but stderr seems to get stuck. Need to understand what is going on here and implement timeouts
- [ ] Would be cool to have a regex match option, not sure how this would fit into the config schema
- [ ] custom redirect page for browsers, could be a fun way to redirect to quotes or something
- [ ] reload configuration endpoint
Expand Down

0 comments on commit fc2de7f

Please sign in to comment.