service

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2021 License: LGPL-3.0 Imports: 4 Imported by: 0

README

Go-service provides a helper for long-running services that require
a mechanism for graceful shutdown. The service starts shutting down
either when a component stops and returns an error, or a chosen signal
is received. A service can call registered functions when shutting down.

The package documentation for go-service can be found at
https://pkg.go.dev/github.com/canonical/go-service.

Documentation

Overview

Package service provides helpers for long-running service applications.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Service

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

A Service is a service provided by a number of goroutines which will initiate a graceful shutdown when either one of those goroutines errors, or on the receipt of chosen signals.

func NewService

func NewService(ctx context.Context, sig ...os.Signal) (context.Context, *Service)

NewService creates a new service instance using the given context. If any signals are specified the service will start a shutdown upon receiving that signal.

func (*Service) Go

func (s *Service) Go(f func() error)

Go calls the given function in a new goroutine.

The first call to return a non-nil error cancels the service; its error will be returned by Wait.

func (*Service) OnShutdown

func (s *Service) OnShutdown(f func())

OnShutdown registers a function to be called when the service determines it is shutting down. The Wait function will wait for all functions provided to OnShutdown to complete before returning.

func (*Service) Wait

func (s *Service) Wait() error

Wait waits for all goroutines started by this service and all functions registered with OnShutdown to complete. The error returned will be the error that caused the service to be canceled, if any.

type SignalError

type SignalError struct {
	Signal os.Signal
}

A SignalError is the type of error returned when a Service has shutdown due to receiving a signal.

func (*SignalError) Error

func (e *SignalError) Error() string

Error implements the error interface.

Jump to

Keyboard shortcuts

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