Process

lib/Process.gob

Running another program and collecting what it says: its standard output and standard error as bytes, kept apart, and how it ended.

let cmd = withEnvironment(command(["turkey", "check", "a.gob"]), "TURKEY_LIB", lib)
match run(cmd, Some(Time.seconds(60))) {
    Right(Output { status = Exited(0), .. }) -> ...
    Right(Output { status = Signaled(sig), stderr, .. }) -> ...
    Right(Output { status = TimedOut, stdout, .. }) -> ...
    Left(e) -> ...
}

A run is one call. run returns when the program has ended and both its streams are closed, with the program's standard input empty (/dev/null). Both streams are read at once, so a program that fills one while the other waits is not deadlocked, and the call waits without holding a worker. To run several programs at once, call run from several tasks (System.Task, System.Parallel): each waits for its own program and none for another's.

How it ended. Exited carries the program's exit code; Signaled the number of the signal that killed it, as a crash or a kill does, which is not an exit code and is told apart from one. A program that failed to start at all -- missing, not executable, a missing working directory -- is a Left, never a Status.

The timeout is one deadline for the whole run: the program, and the reading of both streams. When it falls first, every process in the program's process group is killed (the program starts in a group of its own, so a shell, a wrapper or a compiler driver it started goes with it), the program is reaped, and the status is TimedOut. What was read until then is kept in Output. A program that leaves a process behind that keeps its output open after the program itself has exited is the same case: the streams do not end, the deadline falls, and that process is killed. With no timeout, a stream held open that way is waited for as long as it is held.

The environment is the one this program started with, less any name withEnvironment replaces, plus the replacements. Only the process's own start is read: nothing here sees a change made since.

type Command = Command { argv : Array String, directory : Option String, environment : Array (String, String) }

A program to run: argv[0] is the program, looked up on PATH if it has no slash, and the rest are its arguments. directory is where it runs, this program's own when None. environment is (name, value) pairs that replace or add to the inherited environment.

type Status = Exited(Int) | Signaled(Int) | TimedOut

How the program ended: with an exit code, killed by the signal with that number, or still running when the timeout came and killed then.

type Output = Output { stdout : Array Byte, stderr : Array Byte, status : Process.Status }

What a run produced. After a TimedOut, stdout and stderr are what the program had written when the deadline fell.

fun command : fun(Array String) -> Command

Run argv here, with the inherited environment.

fun inDirectory : fun(Command, String) -> Command

cmd, run in directory.

fun withEnvironment : fun(Command, String, String) -> Command

cmd, with name set to value for the program. Later calls for the same name win.

fun run : fun(Command, Option Duration) -> Either IOError Output

Run cmd and wait for it to end, or for timeout to pass if there is one.

fun runUntil : fun(Command, Option Deadline) -> Either IOError Output

run with a deadline made earlier, so that one limit can cover this and other steps.

instance Show Process.Status