Documentation ¶
Overview ¶
Package port provides a [Runnable] used to interact with external system processes (ExtProg). It uses the os.Exec.Cmd subsystem from the stdlib, but wraps it with Processes. This provides the following guarantees:
A Port must be started by another process. This process will be known as the PortOwner.
When the ExtProg exits, the PortOwner will receive an erl.ExitMsg if it is non-zero. If the process exits cleanly, it will have exitreason.Normal and will be ignored by the PortOwner unless it is trapping exits (this is behavior that applies to all processes). However an ExtProg returning non-zero exit code will cause the PortOwner to crash (if not trapping exits).
If the ReturnExitStatus option is set, the PortOwner will receive a [PortExited] msg, regardless of the ExtProg exit code.
By default, the stdout of the ExtProg will sent to the PortOwner, line by line, as a [PortMessage]. Alternative decoding options can be set when opening the port by provding a bufio.SplitFunc or using one of the existing Decode* Opts in this package. To send this output else where, use SetStdOut with a *os.File or buffer, or IgnoreStdOut to ignore output completely.
Data can be sent to the ExtProg via stdin via [PortCommand] messages. It is up to the sender to format the messages in a way the ExtProg can parse.
The above features allow for asynchronous management of both long-running and transient ExtProgs, integrated into a supervision tree.
Index ¶
- Variables
- func Cast(port erl.PID, msg []byte)
- func Close(self erl.PID, port erl.PID)
- func DecodeNumBytesSplitFun(num int) bufio.SplitFunc
- func Open(self erl.PID, cmd Cmd, opts ...Opt) erl.PID
- type Closed
- type Cmd
- type Command
- type ErrMessage
- type Exited
- type Message
- type Opt
- func CustomDecoder(splitFun bufio.SplitFunc) Opt
- func DecodeLines() Opt
- func DecodeNULs() Opt
- func DecodeNumBytes(num int) Opt
- func IgnoreStdOut() Opt
- func ReceiveStdErr(splitFunc bufio.SplitFunc) Opt
- func ReturnExitStatus() Opt
- func SetExitSignal(sig syscall.Signal) Opt
- func SetStdErr(stderr io.Writer) Opt
- func SetStdOut(stdout io.Writer) Opt
- type Opts
- type Port
Constants ¶
This section is empty.
Variables ¶
var DecodeLinesSplitFun = bufio.ScanLines
Functions ¶
func Cast ¶ added in v0.8.0
sends an asynchronous command to the port which will be sent to the external procs stdin. It's the callers responsibility to encode [msg] to a format the external program understands.
func DecodeNumBytesSplitFun ¶ added in v0.8.0
Types ¶
type ErrMessage ¶ added in v0.8.0
Received from a port error stream
type Exited ¶ added in v0.8.0
returned when the port process returns and ReturnExitStatus is true. if [err] is not nil, it may be an *os/exec.ExitError
type Opt ¶ added in v0.8.0
func CustomDecoder ¶ added in v0.8.0
func DecodeNULs ¶ added in v0.8.0
func DecodeNULs() Opt
Port messages will be parsed as NUL delimited strings
func DecodeNumBytes ¶ added in v0.8.0
Will read [num] bytes from the external command before returning a message to the port owner.
func IgnoreStdOut ¶ added in v0.8.0
func IgnoreStdOut() Opt
Causing stdout to be redirected to a null device. The PortOwner should recieve no [PortMessage]s while this is set.
func ReceiveStdErr ¶ added in v0.8.0
If set, the PortOwner will receive the data from the ExtProg error stream as [PortErrMessage]s. Not all programs will return error messages in the saem format as stdout, this option accepts a distinct bufio.Splitfunc to use for stderr.
func ReturnExitStatus ¶ added in v0.8.0
func ReturnExitStatus() Opt
This will cause the exit status to be returend to the PortOwner
func SetExitSignal ¶ added in v0.8.0
If set, this signal will be sent to the ExtProg during port shutdown in addition to closing stdin