supervises

package
v0.0.0-...-f7543ad Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2025 License: ISC Imports: 13 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrExitFailure    = errors.New("failed")
	ErrInvalidCommand = errors.New("invalid command")
	ErrSignal         = errors.New("terminated by signal")
)

Functions

This section is empty.

Types

type Cmd

type Cmd struct {
	Path        string
	Args        []string
	Env         []string
	Dir         string
	Stdin       io.Reader
	Stdout      io.Writer
	Stderr      io.Writer
	ExtraFiles  []*os.File
	SysProcAttr *syscall.SysProcAttr
}

func (*Cmd) String

func (c *Cmd) String() string

type ExitError

type ExitError struct {
	Argv     string
	Err      error
	Cmd      *exec.Cmd
	ExitCode int
}

func (*ExitError) Error

func (e *ExitError) Error() string

func (*ExitError) String

func (e *ExitError) String() string

type Opt

type Opt struct {
	// contains filtered or unexported fields
}

func New

func New(ctx context.Context, opt ...Option) *Opt

New returns configuration for supervisors.

func (*Opt) Cmd

func (o *Opt) Cmd(args ...string) ([]*Cmd, error)

func (*Opt) Context

func (o *Opt) Context() context.Context

func (*Opt) Reset

func (o *Opt) Reset()

func (*Opt) Supervise

func (o *Opt) Supervise(args ...*Cmd) error
Example
package main

import (
	"context"
	"errors"
	"fmt"
	"os"
	"time"

	"codeberg.org/msantos/supervises/pkg/supervises"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
	defer cancel()

	s := supervises.New(ctx)
	cmds, err := s.Cmd("@echo test123; exec sleep 10", "cat")
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}

	err = s.Supervise(cmds...)

	var ee *supervises.ExitError

	if !errors.As(err, &ee) {
		fmt.Fprintln(os.Stderr, err.Error())
		return
	}

}
Output:

test123

type Option

type Option func(*Opt)

func WithCancelFunc

func WithCancelFunc(f func(*exec.Cmd, syscall.Signal) error) Option

WithCancelFunc sets the function to reap cancelled subprocesses.

func WithCancelSignal

func WithCancelSignal(sig syscall.Signal) Option

WithCancelSignal sets the signal sent to subprocesses on exit.

func WithNotifySignals

func WithNotifySignals(sigs ...os.Signal) Option

WithNotifySignals sets trapped signals by the supervisor.

func WithRetry

func WithRetry(retry func(*Cmd, *ExitError) *ExitError) Option

WithRetry sets the retry behaviour.

Jump to

Keyboard shortcuts

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