From 23f7497622016f3c1048f367bf0aa06821a9221c Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Fri, 26 May 2023 13:06:21 -0400 Subject: [PATCH] Routine manager now manages functions --- routines/routines.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) 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")