diff --git a/routines/routines.go b/routines/routines.go index 0d0dfd6..4203263 100644 --- a/routines/routines.go +++ b/routines/routines.go @@ -6,12 +6,9 @@ import "log" import "time" import "sync" -// Routine represents an object that can be run. -type Routine interface { - // Run starts the routine and does not return until it is finished. An - // error is returned if the routine exited due to an error. - Run () error -} +// Routine is a long-running function that does not return until it is finished. +// An error is returned if the routine exited due to an error. +type Routine func () error // Manager is a system capable of managing multiple routines, and restarting // them if they fail. @@ -49,6 +46,12 @@ func (manager *Manager) Run () error { return errExit } +// Append adds one or more routines to the Routines slice. This has no effect if +// the manager is already running. +func (manager *Manager) Append (routines ...Routine) { + manager.Routines = append(manager.Routines, routines...) +} + func (manager *Manager) log (message ...any) { if manager.Logger == nil { log.Println(message...) @@ -64,7 +67,7 @@ func (manager *Manager) runRoutine (routine Routine, group *sync.WaitGroup, errE for { // TODO: recover from panics lastStart := time.Now() - err = routine.Run() + err = routine() if err == nil { manager.log("(i) routine exited")