cirello.io: cirello.io/supervisor/easy Index | Examples | Files

package easy

import "cirello.io/supervisor/easy"

Package easy is an easier interface to use cirello.io/supervisor. Its lifecycle is managed through context.Context. Stop a given supervisor by cancelling its context.

package main

import supervisor "cirello.io/supervisor/easy"

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	// use cancel() to stop the supervisor
	ctx = supervisor.WithContext(ctx)
	supervisor.Add(ctx, func(ctx context.Context) {
		// ...
	})
}

Code:

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

var wg sync.WaitGroup
ctx = supervisor.WithContext(ctx)
wg.Add(1)
serviceName, err := supervisor.Add(ctx, func(ctx context.Context) {
    select {
    case <-ctx.Done():
        return
    default:
        defer wg.Done()
        fmt.Println("executed successfully")
        cancel()
    }
})
if err != nil {
    log.Fatal(err)
}

wg.Wait()

if err := supervisor.Remove(ctx, serviceName); err != nil {
    log.Fatal(err)
}

Output:

executed successfully

Index

Examples

Package Files

easy.go

Variables

var (
    // Permanent services are always restarted.
    Permanent = supervisor.Permanent

    // Transient services are restarted only when panic.
    Transient = supervisor.Transient

    // Temporary services are never restarted.
    Temporary = supervisor.Temporary
)
var (
    // ErrNoSupervisorAttached means that the given context has not been
    // wrapped with WithContext, and thus this package cannot detect
    // which supervisore you are referring to.
    ErrNoSupervisorAttached = errors.New("no supervisor attached to context")
)

func Add Uses

func Add(ctx context.Context, f func(context.Context), opts ...supervisor.ServiceOption) (string, error)

Add inserts supervised function to the attached supervisor, it launches automatically. If the context is not correctly prepared, it returns an ErrNoSupervisorAttached error. By default, the restart policy is Permanent.

func Remove Uses

func Remove(ctx context.Context, name string) error

Remove stops and removes the given service from the attached supervisor. If the context is not correctly prepared, it returns an ErrNoSupervisorAttached error

func WithContext Uses

func WithContext(ctx context.Context, opts ...SupervisorOption) context.Context

WithContext takes a context and prepare it to be used by easy supervisor package. Internally, it creates a supervisor in group mode. In this mode, every time a service dies, the whole supervisor is restarted.

type SupervisorOption Uses

type SupervisorOption func(*supervisor.Supervisor)

SupervisorOption reconfigures the supervisor attached to the context.

func WithLogger Uses

func WithLogger(logger func(a ...interface{})) SupervisorOption

WithLogger attaches a log function to the supervisor

Package easy imports 6 packages (graph). Updated 2019-01-14. Refresh now. Tools for package owners.