run

package module
v0.0.0-...-5c31757 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2025 License: Unlicense Imports: 10 Imported by: 0

README

Run

Run defines a simple and composable mechanism for writing concurrent Go programs.

Documentation

Index

Examples

Constants

View Source
const (
	ShutdownTimeout = 10 * time.Second
)

Variables

View Source
var (
	ErrExited  = errors.New("runner exited early")
	ErrTimeout = errors.New("one or more runners did not exit in time")
)
View Source
var Idle = Func(func(ctx context.Context) error {
	<-ctx.Done()
	return nil
})

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

func Go(ctx context.Context, r Runner, res chan<- error)

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

func Start(ctx context.Context, runner Runner, ready Runner) (err error, stop func() error)

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

type Func (func(context.Context) error)

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!

func (Func) Run

func (f Func) Run(ctx context.Context) error

Run implements Runner

type Group

type Group map[string]Runner

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

func (g Group) Run(ctx context.Context) error

func (Group) WithoutCancel

func (g Group) WithoutCancel() Runner

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

func Command

func Command(cmd string, args ...string) Process

func (Process) Run

func (p Process) Run(ctx context.Context) error

Run implements Runner starting the external process.

The process can be shut down by cancelling ctx. In this case the process and all child processes

will receive a SIGINT.

If this program or p do not terminate gracefully then a SIGKILL will be sent to the process group.

type Runner

type Runner interface {
	Run(context.Context) error
}

func Once

func Once(r Runner) Runner

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

func Ready(runner Runner, ready Runner, readych chan<- error) Runner

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

func (Sequence) Run

func (s Sequence) Run(ctx context.Context) error

Run implements Runner

Directories

Path Synopsis
exp
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.

Jump to

Keyboard shortcuts

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