task

package
Version: v0.0.0-...-d3fb95e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2018 License: Apache-2.0 Imports: 8 Imported by: 0

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.

func (*Ended) String

func (p *Ended) String() string

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)
		}
	}

func (*Event) String

func (e *Event) String() string

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?

func (*Mux) Add

func (mux *Mux) Add(c chan *Event) <-chan *Event

Add a channel to this mux, piping all events from that channel out.

func (*Mux) Out

func (mux *Mux) Out() <-chan *Event

Out returns a channel that outputs all the events of channels added with Add.

type Output

type Output struct {
	Chunk string
}

Output is the type of payload indicating the process output some amount of data.

func (*Output) String

func (p *Output) String() string

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.

func (*Supervisor) Zero

func (s *Supervisor) Zero()

Zero resets all counters to zero

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.

func Spawn

func Spawn(cmd *exec.Cmd, splitter bufio.SplitFunc) (*Task, error)

Spawn a Cmd into a PTY. Returns a Task, so that you can communicate with the process

func (*Task) Kill

func (task *Task) Kill() error

Kill kills the task. Returns nil if the task is not running.

func (*Task) Respawn

func (task *Task) Respawn() (*Task, error)

Respawn spawns a new task with a duplicate of this task's command.

func (*Task) String

func (task *Task) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL