Skip to content
This repository has been archived by the owner on Jul 18, 2022. It is now read-only.

Commit

Permalink
Add autokill feature
Browse files Browse the repository at this point in the history
  • Loading branch information
kdheepak committed Apr 21, 2020
1 parent b7379b5 commit 7645c6a
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/helics.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,23 @@ else:
IONBF {.importc: "_IONBF", nodecl.}: cint


proc monitor(p: Process, log_file: string) =
proc monitor(p: Process, log_file: string): int =

var l = log_file.newFileStream(fmWrite)
var o = p.outputStream()
var f: File
discard open(f, p.outputHandle(), fmRead)
discard c_setvbuf(f, nil, IONBF, 0)
var line = ""
var buffer: array[10, char]

while p.peekExitCode() == -1:
o.flush()
discard o.readLine(line)
l.writeLine(line)
l.flush()

return p.peekExitCode()

when defined(windows):
const ENV_COMMAND = "set"
else:
Expand Down Expand Up @@ -136,19 +137,21 @@ proc run(path: string, silent = false): int =
var f = open(path_to_config, fmRead)
var runner = parseJson(f.readAll())

print(&"""Running federation: {runner["name"]}""", silent = silent)
print(&"""Running federation: `{runner["name"].getStr}`""", silent = silent)

var env = {:}.newStringTable
for line in execProcess(ENV_COMMAND).splitLines():
var s = line.split("=")
env[s[0]] = join(s[1..s.high])

var processes = newSeq[Process]()
var process_names = newSeq[string]()
var threads = newSeq[FlowVar[int]]()

for f in runner["federates"]:

let name = f["name"].getStr
print(&"""Running federate {name} as a background process""", silent = silent)
print(&"""Running federate `{name}` as a background process""", silent = silent)

var directory: string
if f{"directory"} != nil:
Expand All @@ -166,12 +169,25 @@ proc run(path: string, silent = false): int =
let cmd = f["exec"].getStr
let p = startProcess(cmd, env = process_env, workingDir = directory, options = {poInteractive, poStdErrToStdOut, poEvalCommand})

spawn monitor(p, joinPath(dirname, &"""{f["name"].getStr}.log"""))
threads.add(spawn monitor(p, joinPath(dirname, &"""{name}.log""")))
processes.add(p)

sync()

print("Success!")
process_names.add(name)

var error_occured = false
while not all(threads, r => r.isReady):
for (i, t) in threads.pairs:
if t.isReady and ^t != 0:
print(&"""Something went wrong with federate `{process_names[i]}`. Please check the log files.""", sError)
error_occured = true
if error_occured:
for p in processes:
try:
kill(p)
except:
discard
break

if not error_occured: print("Success!")


when isMainModule:
Expand Down

0 comments on commit 7645c6a

Please sign in to comment.