Documentation
¶
Overview ¶
Package task provides building blocks for spawning processes and communicating with them via channels.
Here's an example of communicating with a simple process and reading its output.
package main
import (
// needed for ScanLines split function
"bufio"
"fmt"
"github.com/justjake/encabulator/task"
"os/exec"
// needed to for sleep. Normally not required for working with tasks.
"time"
)
func main() {
// `tee` reads from STDIN, writes each line to the given file,
// then echos to STDOUT.
cmd := exec.Command("tee", "./log.txt")
// Spawn `tee` as a task, so we can communicate with it.
tee, err := task.Spawn(cmd, bufio.ScanLines)
if err != nil {
panic(err)
}
// write "hello world\n" to tee's STDIN.
tee.Input <- []byte("hello world\n")
// start a goroutine to kill tee after a second. This is needed as tee will
// run forever.
go func(t *task.Task) {
time.Sleep(time.Second)
t.Kill()
}(tee)
// Loop over tee's output channel, handling each event.
// This channel will close once the task's command exits.
for event := range tee.Output {
// use a type switch to handle the different sorts of events.
switch payload := event.Payload.(type) {
case *task.Output:
fmt.Println("-> %s", payload.Chunk)
case *task.Ended:
fmt.Println("Task ended.")
}
}
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Ended ¶
type Ended struct {
Error error
}
Ended is the type of payload indicating the process ended. If the process exited 0, Error will be nil. Otherwise it will be an exec.ExitError.
type Event ¶
type Event struct {
// TODO: should Producer be a Task?
Task *Task
Payload interface{}
}
Event is emitted from a Task. Use a type switch on event.Payload to determine exactly which event occurred, and inspect its properties.
func onEvent(event *task.Event) {
switch payload := event.Payload.(type) {
case *task.Output:
fmt.Printf("Output: %s", payload.Chunk)
case *task.Ended:
fmt.Printf("Task %v exited with: %v", event.Task, payload.Error)
}
}
type Mux ¶
type Mux struct {
// contains filtered or unexported fields
}
Mux mixes together one or more event channels. Note that a Mux will never close its output channel. Currently 100% lock-free!
TODO: use a WaitGroup to close the mux when all added channels close?
type Output ¶
type Output struct {
Chunk string
}
Output is the type of payload indicating the process output some amount of data.
type Supervisor ¶
type Supervisor struct {
// contains filtered or unexported fields
}
Supervisor supervises a task, ressurecting it in a new Task if it ever dies. A certain number of failures are allowed per time period
func MakeSupervisor ¶
func MakeSupervisor(maxFailures int, within time.Duration) *Supervisor
Create a new Supervisor
func (*Supervisor) HandleEvent ¶
func (s *Supervisor) HandleEvent(ev *Event) (*Task, error)
HandleEvent processes an Event. Returns a Task and an error. The task may be an ongoing task, or it could be a new task in the case of a task failure.
type Task ¶
type Task struct {
// Send a byte slice to this channel to write to the process's pty.
Input chan<- []byte
// Output will emit Event structs as events (like process output or process
// termination) occurr.
Output <-chan *Event
// contains filtered or unexported fields
}
Task is a running process inside a PTY. Use Spawn to create a new Task. Interact with a task by reading from its Events channel, and writing to its Input channel.