Skip to content

Commit

Permalink
Merge pull request #31 from jiro4989/develop
Browse files Browse the repository at this point in the history
catで固まるのを一旦中断できるようにした #27
  • Loading branch information
jiro4989 authored Dec 12, 2019
2 parents 3717c88 + 40a0a79 commit e69973b
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions websh_server/src/websh_server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,23 @@ proc readStream(strm: var Stream): string =
lines.add(line)
result = lines.join("\n")

proc runCommand(command: string, args: openArray[string]): (string, string) =
proc runCommand(command: string, args: openArray[string], timeout: int = 3): (string, string) =
## ``command`` を実行し、標準出力と標準エラー出力を返す。
## timeout は秒を指定する。
var
p = startProcess(command, args = args, options = {poUsePath})
stdoutStr, stderrStr: string
defer: p.close()
let sleepInterval = 10 # ミリ秒
let timeoutMilSec = timeout * 1000
var elapsedTime: int
while p.running():
# プロセスが処理完了するまで待機
sleep 10
sleep sleepInterval
elapsedTime += sleepInterval
if timeoutMilSec < elapsedTime:
info &"Over timeout: {timeout} second"
return
block:
var strm = p.outputStream
stdoutStr = strm.readStream()
Expand Down Expand Up @@ -82,7 +90,7 @@ router myrouter:
"-v", &"{imageDir}:/{img}",
# "-v", "./media:/media:ro",
imageName,
"bash", "-c", &"chmod +x {containerShellScriptPath} && sync && timeout -sKILL 3 {containerShellScriptPath} | stdbuf -o0 head -c 100K",
"bash", "-c", &"chmod +x {containerShellScriptPath} && sync && {containerShellScriptPath} | stdbuf -o0 head -c 100K",
]
let (stdoutStr, stderrStr) = runCommand("docker", args)

Expand Down

0 comments on commit e69973b

Please sign in to comment.