Documentation
¶
Overview ¶
Package srvc provides simple but powerful Run functionality on top of Module abstraction. Ready made modules can be found under: github.com/go-srvc/mods.
Index ¶
Constants ¶
const ErrModulePanic = ErrStr("module recovered from panic")
Variables ¶
var JoinErrors = errors.Join
JoinErrors allows overwriting the default errors.JoinErrors function used by srvc package. This can be useful for custom multi error formatting.
Functions ¶
func Run ¶
Run will run all given modules using following control flow:
- Exec Init() for each module in order. If any Init() returns error the Init loop is stopped and Stop() will be called for already initialized modules in reverse order.
- Exec Run() for each module in own goroutine so order isn't guaranteed.
- Wait for any Run() function to return nil or an error and move to Stop loop.
- Exec Stop() for modules in reverse order.
- Wait for all Run() goroutines to return.
- Return all errors or nil
Possible panics inside modules are captured to allow graceful shutdown of other modules. Captured panics are converted into errors and ErrPanic is returned.
func RunAndExit ¶
func RunAndExit(modules ...Module)
RunAndExit is convenience wrapper for Run that calls os.Exit with code 1 in case of an error. The common use case is to srvc.RunAndExit from main function and let the srvc handle the rest.
package main
import "github.com/go-srvc/srvc"
func main() {
srvc.RunAndExit(
// Add your modules here
)
}
Types ¶
type ErrGroup ¶
type ErrGroup struct {
// contains filtered or unexported fields
}
ErrGroup is a goroutine group that waits for all goroutines to finish and collects errors.
type Module ¶
type Module interface {
// ID should return identifier for logging purposes.
ID() string
// Init allows synchronous initialization of module.
Init() error
// Run should start the module and block until stop is called or error occurs.
Run() error
// Stop allows synchronous cleanup of module should make Run() return eventually.
// If Init was called then Stop is guaranteed to be called as part of cleanup.
Stop() error
}