Documentation
¶
Index ¶
Examples ¶
Constants ¶
const (
ShutdownTimeout = 10 * time.Second
)
Variables ¶
var ( ErrExited = errors.New("runner exited early") ErrTimeout = errors.New("one or more runners did not exit in time") )
Idle is a Runner that does nothing waiting for ctx to be cancelled.
Idle can be useful as the last member of a Sequence if you don't want the Sequence to return causing sibling runners to be cancelled, such as within a Group.
Functions ¶
func Go ¶
Execute r in a Goroutine pushing the return value into res.
Recovers panics from r returning them as an error instead. If r panics with a error the returned error will wrap that error.
func Start ¶
Start runs runner in a detatched state returning when runner is ready to be interacted with determined by ready returning nil.
If r does not become ready either because r returns either a nil or non-nil error, ready returns a non-nil error, or ctx.Err() returns a non-nil error then err will be non-nil with the cause.
stop must be called in order to terminate r gracefully. If r returns an error after being signalled to shut down through the context passed to its Run method being cancelled then stop will record an error on its testing.T.
pass a nil testing.T to avoid this behaviour.
Types ¶
type Func ¶
Func is a Runner for a Go function literal.
Example ¶
package main
import (
"context"
"fmt"
"github.com/matgreaves/run"
)
func main() {
f := run.Func(func(ctx context.Context) error {
fmt.Println("Hello, World!")
return nil
})
_ = f.Run(context.Background())
}
Output: Hello, World!
type Group ¶
Group executes a group of Runner in parallel returning the reason the first member exits.
If a runner exits with error == nil then ErrExited will be returned.
Runners have until ShutdownTimeout to exit or the group will exit in a ErrTimeout wrapping the original cause for the shutdown.
Group will catch panics within members and propagate them as errors instead gracefully terminating other members.
func (Group) WithoutCancel ¶
WithoutCancel returns a group that doesn't cancel other runners if a runner exits with a nil error.
type Process ¶
type Process struct {
Name string
// path to executable
Path string
// working directory
Dir string
// command-line args
Args []string
// environment variables
Env map[string]string
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
// inherit environment variables from os.Env.
// Env takes priority over os variables.
InheritOSEnv bool
// A list of environment variables to exclude when InheritOSEnv is true.
DoNotInherit []string
}
Process runs an extermal
type Runner ¶
func Once ¶
Once returns a Runner that only executes r the first time [Run] is called.
Successive calls will return the same result.
Example ¶
package main
import (
"context"
"fmt"
"github.com/matgreaves/run"
)
func main() {
r := run.Once(run.Func(func(_ context.Context) error {
fmt.Println("Hello, World!")
return nil
}))
r.Run(context.Background())
r.Run(context.Background())
}
Output: Hello, World!
func Ready ¶
Ready takes a runner that starts a long-lived process that needs to be checked by ready before it is ready to be interacted with.
If the context passed to Run is cancelled before the server ready then a non-nil error will be passed to readych.
ready is responsible for returning `nil` when it detects that server is ready to receive traffic.
type Sequence ¶
type Sequence []Runner
Sequence executes a group of Runner sequentially.
Example ¶
package main
import (
"context"
"fmt"
"github.com/matgreaves/run"
)
func main() {
r := run.Sequence{
run.Func(func(ctx context.Context) error {
fmt.Print("one")
return nil
}),
run.Func(func(ctx context.Context) error {
fmt.Print(" two")
return nil
}),
}
_ = r.Run(context.Background())
}
Output: one two
Directories
¶
| Path | Synopsis |
|---|---|
|
onexit is a package for robustly running deferred logic even if the go program exits forcefully without waiting for deferred logic to run.
|
onexit is a package for robustly running deferred logic even if the go program exits forcefully without waiting for deferred logic to run. |