easy

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2018 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

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) {
		// ...
	})
}
Example
package main

import (
	"context"
	"fmt"
	"log"
	"sync"
	"time"

	supervisor "cirello.io/supervisor/easy"
)

func main() {
	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

Constants

This section is empty.

Variables

View Source
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
)
View Source
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")
)

Functions

func Add

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

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

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.

Types

type SupervisorOption

type SupervisorOption func(*supervisor.Supervisor)

SupervisorOption reconfigures the supervisor attached to the context.

func WithLogger

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

WithLogger attaches a log function to the supervisor

Jump to

Keyboard shortcuts

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