forge

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2019 License: MIT Imports: 4 Imported by: 0

README

forge

Build Status Coverage Status GoDoc Release Go Report Card

A toolkit for managing long-running tasks in Go.

Example

// prepare service
service := &forge.Service{}

// set reporter
var errs []error
service.Report(func(err error) {
    errs = append(errs, err)
})

// run task
i := 0
service.Run(1, func() error {
    i++

    if i == 5 {
        return forge.ErrDone
    }
    if i%2 == 0 {
        return errors.New("foo")
    }

    return nil
}, func() {
    fmt.Println("finalize")
})

// wait for exit
<-service.Done()

// print output
fmt.Println(i)
fmt.Println(errs)

// Output:
// finalize
// 5
// [foo foo]

Documentation

Overview

Example
// prepare service
service := &Service{}

// set reporter
var errs []error
service.Report(func(err error) {
	errs = append(errs, err)
})

// run task
i := 0
service.Run(1, func() error {
	i++

	if i == 5 {
		return ErrDone
	}
	if i%2 == 0 {
		return errors.New("foo")
	}

	return nil
}, func() {
	fmt.Println("finalize")
})

// wait for exit
<-service.Done()

// print output
fmt.Println(i)
fmt.Println(errs)
Output:

finalize
5
[foo foo]

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrDone = errors.New("done")

ErrDone is returned to indicate that the task is done.

View Source
var ErrKilled = errors.New("killed")

ErrKilled indicates that a Terminator has been killed.

View Source
var ErrStopped = errors.New("stopped")

ErrStopped indicates that a Terminator has been stopped.

Functions

func Close

func Close(stop, kill time.Duration, closers ...Closer) bool

Close will stop and kill the provided closers in the specified timeouts.

func Repeat

func Repeat(task func() error, reporter func(error))

Repeat will wrap the provided task and run it repeatedly until ErrDone is returned. The specified reporter is called for any other returned error.

func Run

func Run(n int, task func(), finalizer func())

Run will launch multiple goroutines that execute the specified task. If a finalizer is configured, it will be called once all tasks returned.

Types

type Closer

type Closer interface {
	Done() <-chan struct{}
	Stop()
	Kill()
}

Closer is a closable service or struct that embeds a service.

type Manager

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

A Manager manages multiple finite running tasks.

func (*Manager) Done

func (m *Manager) Done() <-chan struct{}

Done will return a channel that is closed once all until now started tasks have returned.

func (*Manager) Finished

func (m *Manager) Finished() bool

Finished will return whether all ran tasks have returned.

func (*Manager) Run

func (m *Manager) Run(n int, task func(), finalizer func())

Run will run the specified task.

type Reporter

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

A Reporter manages repeating tasks and their error reporting.

func (*Reporter) Repeat

func (r *Reporter) Repeat(fn func() error)

Repeat will repeat the provided function.

func (*Reporter) Report

func (r *Reporter) Report(fn func(error))

Report will set the reporter function.

type Service

type Service struct {
	Manager
	Terminator
	Reporter
}

Service manages multiple long-running tasks.

func (*Service) Run

func (s *Service) Run(n int, task func() error, finalizer func())

Run will run the specified task continuously until the tasks return or service has been stopped or killed.

type Terminator

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

Terminator provides a stopping and killing mechanism.

func (*Terminator) IsKilled

func (t *Terminator) IsKilled() bool

IsKilled returns whether Kill has been called.

func (*Terminator) IsStopping

func (t *Terminator) IsStopping() bool

IsStopping returns whether Stop has been called.

func (*Terminator) Kill

func (t *Terminator) Kill()

Kill will close the Stopping and Killed channel.

func (*Terminator) Killed

func (t *Terminator) Killed() <-chan struct{}

Killed returns the channel closed by Kill.

func (*Terminator) Notify added in v0.3.0

func (t *Terminator) Notify(fn func())

Notify will store the specified callback and call it once the terminator has been stopped.

func (*Terminator) Status

func (t *Terminator) Status() error

Status returns and error if Stop or Kill have been called.

func (*Terminator) Stop

func (t *Terminator) Stop()

Stop will close the Stopping channel.

func (*Terminator) Stopping

func (t *Terminator) Stopping() <-chan struct{}

Stopping returns the channel closed by Stop.

Jump to

Keyboard shortcuts

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