easy

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: Apache-2.0, Apache-2.0 Imports: 6 Imported by: 1

README

cirello.io/oversight/easy

GoDoc SLA

This repository is covered by this SLA.

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

go get [-u -f] cirello.io/oversight/easy

http://godoc.org/cirello.io/oversight/easy

Quickstart

package main

import oversight "cirello.io/oversight/easy"

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

Documentation

Overview

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

package main

import oversight "cirello.io/oversight/easy"

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

This package is covered by this SLA: https://cirello.io/sla

Example
package main

import (
	"context"
	"fmt"
	"io/ioutil"
	"log"
	"sync"
	"time"

	oversight "cirello.io/oversight/easy"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	var wg sync.WaitGroup
	ctx = oversight.WithContext(ctx, oversight.WithLogger(log.New(ioutil.Discard, "", 0)))
	wg.Add(1)
	serviceName, err := oversight.Add(ctx, func(ctx context.Context) error {
		select {
		case <-ctx.Done():
			return nil
		default:
			defer wg.Done()
			fmt.Println("executed successfully")
			cancel()
			return nil
		}
	}, oversight.RestartWith(oversight.Temporary()))
	if err != nil {
		log.Fatal(err)
	}
	wg.Wait()
	if err := oversight.Delete(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 = oversight.Permanent

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

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

Functions

func Add

func Add(ctx context.Context, f oversight.ChildProcess, opts ...Option) (string, error)

Add inserts a supervised function to the attached tree, it launches automatically. If the context is not correctly prepared, it returns an ErrNoTreeAttached error. The restart policy is Permanent.

func Delete

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

Delete stops and removes the given service from the attached tree. If the context is not correctly prepared, it returns an ErrNoTreeAttached error

func WithContext

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

WithContext takes a context and prepare it to be used by easy oversight tree package. Internally, it creates an oversight tree in OneForAll mode.

Types

type Logger

type Logger = oversight.Logger

Logger defines the interface for any logging facility to be compatible with oversight trees.

type Option

Option reconfigures the attachment of a process to the context tree.

func RestartWith

func RestartWith(restart oversight.Restart) Option

RestartWith changes the restart policy for the process.

type TreeOption

type TreeOption = oversight.TreeOption

TreeOption are applied to change the behavior of a Tree.

func WithLogger

func WithLogger(logger Logger) TreeOption

WithLogger attaches a log function to the oversight tree.

Jump to

Keyboard shortcuts

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