Documentation ¶
Overview ¶
Package bg provides utilities for managing background processes.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Group ¶
type Group struct { // If set, OnShutdown will be called before the Group starts stopping its running services. If the shutdown is // caused by an error, that error will be passed as OnShutdown's argument. OnShutdown func(err error) // contains filtered or unexported fields }
Group manages a group of services by running them in the background, and stopping them when Stop is called. If any service unexpectedly stops on its own, all other services will also be stopped.
func (*Group) Check ¶ added in v1.2.0
Check calls callback with true if all servers are running as expected, or false if the group is shutting down, or has shut down.
It is safe to call Add from within callback. Calls Stop and StopOnSignal will deadlock, as will calls to Wait if the group is still alive.
func (*Group) Stop ¶
func (g *Group) Stop()
Stop calls Service.Stop on all running services, starting with the most recently added service.
func (*Group) StopOnSignal ¶
StopOnSignal listens in a new goroutine for the given signals, and calls Stop if and when they are received. If Stop is called manually, the goroutine stops listening and terminates.
Typically, you'll be using an os.Interrupt to catch CTRL+C:
g.StopOnSignal(os.Interrupt)
type Service ¶
type Service interface { Task // Stop should signal the service to shut down gracefully. Stop() }
Service is any Task that can be signalled to stop.
type Starter ¶
type Starter interface { // Start starts the task in a new goroutine. It must not be called more than once. Start() // Wait blocks until the task finishes, and returns its error. Wait can be called before or after Start is called. // It can be called multiple times. If called after the task has already finished, it will return the task's error // immediately. Wait() error }
Starter is a task that can be started using Start, and then later joined using Wait.
func NewStarter ¶
type Task ¶
type Task interface { // Run starts the task, and returns when it finishes. Run() error }
Task is a process that returns an error if it fails.
type UnexpectedStop ¶
func (*UnexpectedStop) Error ¶
func (u *UnexpectedStop) Error() string
func (*UnexpectedStop) Unwrap ¶
func (u *UnexpectedStop) Unwrap() error