2022-01-03 16:37:09 +00:00
|
|
|
package runners
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
2022-07-22 11:43:34 +01:00
|
|
|
|
|
|
|
"codeberg.org/gruf/go-atomics"
|
2022-01-03 16:37:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// FuncRunner provides a means of managing long-running functions e.g. main logic loops.
|
|
|
|
type FuncRunner struct {
|
|
|
|
// HandOff is the time after which a blocking function will be considered handed off
|
|
|
|
HandOff time.Duration
|
|
|
|
|
|
|
|
// ErrorHandler is the function that errors are passed to when encountered by the
|
|
|
|
// provided function. This can be used both for logging, and for error filtering
|
|
|
|
ErrorHandler func(err error) error
|
|
|
|
|
2022-07-22 11:43:34 +01:00
|
|
|
svc Service // underlying service to manage start/stop
|
|
|
|
err atomics.Error
|
2022-01-03 16:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Go will attempt to run 'fn' asynchronously. The provided context is used to propagate requested
|
|
|
|
// cancel if FuncRunner.Stop() is called. Any returned error will be passed to FuncRunner.ErrorHandler
|
|
|
|
// for filtering/logging/etc. Any blocking functions will be waited on for FuncRunner.HandOff amount of
|
|
|
|
// time before considering the function as handed off. Returned bool is success state, i.e. returns true
|
|
|
|
// if function is successfully handed off or returns within hand off time with nil error.
|
|
|
|
func (r *FuncRunner) Go(fn func(ctx context.Context) error) bool {
|
2022-07-22 11:43:34 +01:00
|
|
|
var has bool
|
|
|
|
|
2022-01-03 16:37:09 +00:00
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
var cancelled bool
|
|
|
|
|
2022-07-22 11:43:34 +01:00
|
|
|
has = r.svc.Run(func(ctx context.Context) {
|
2022-01-03 16:37:09 +00:00
|
|
|
// reset error
|
2022-07-22 11:43:34 +01:00
|
|
|
r.err.Store(nil)
|
2022-01-03 16:37:09 +00:00
|
|
|
|
|
|
|
// Run supplied func and set errror if returned
|
|
|
|
if err := Run(func() error { return fn(ctx) }); err != nil {
|
2022-07-22 11:43:34 +01:00
|
|
|
r.err.Store(err)
|
2022-01-03 16:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// signal done
|
|
|
|
close(done)
|
|
|
|
|
|
|
|
// Check if cancelled
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
cancelled = true
|
|
|
|
default:
|
|
|
|
cancelled = false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
switch has {
|
|
|
|
// returned after starting
|
|
|
|
case true:
|
2022-07-22 11:43:34 +01:00
|
|
|
// Load set error
|
|
|
|
err := r.err.Load()
|
2022-01-03 16:37:09 +00:00
|
|
|
|
|
|
|
// filter out errors due FuncRunner.Stop() being called
|
2022-07-22 11:43:34 +01:00
|
|
|
if cancelled && errors.Is(err, context.Canceled) {
|
2022-01-03 16:37:09 +00:00
|
|
|
// filter out errors from FuncRunner.Stop() being called
|
2022-07-22 11:43:34 +01:00
|
|
|
r.err.Store(nil)
|
|
|
|
} else if err != nil && r.ErrorHandler != nil {
|
2022-01-03 16:37:09 +00:00
|
|
|
// pass any non-nil error to set handler
|
2022-07-22 11:43:34 +01:00
|
|
|
r.err.Store(r.ErrorHandler(err))
|
2022-01-03 16:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// already running
|
|
|
|
case false:
|
|
|
|
close(done)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// get valid handoff to use
|
|
|
|
handoff := r.HandOff
|
|
|
|
if handoff < 1 {
|
|
|
|
handoff = time.Second * 5
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
// handed off (long-run successful)
|
|
|
|
case <-time.After(handoff):
|
|
|
|
return true
|
|
|
|
|
|
|
|
// 'fn' returned, check error
|
|
|
|
case <-done:
|
2022-07-22 11:43:34 +01:00
|
|
|
return has
|
2022-01-03 16:37:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop will cancel the context supplied to the running function.
|
|
|
|
func (r *FuncRunner) Stop() bool {
|
|
|
|
return r.svc.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Err returns the last-set error value.
|
|
|
|
func (r *FuncRunner) Err() error {
|
2022-07-22 11:43:34 +01:00
|
|
|
return r.err.Load()
|
2022-01-03 16:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run will execute the supplied 'fn' catching any panics. Returns either function-returned error or formatted panic.
|
|
|
|
func Run(fn func() error) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
if e, ok := r.(error); ok {
|
|
|
|
// wrap and preserve existing error
|
|
|
|
err = fmt.Errorf("caught panic: %w", e)
|
|
|
|
} else {
|
|
|
|
// simply create new error fromt iface
|
|
|
|
err = fmt.Errorf("caught panic: %v", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// run supplied func
|
|
|
|
err = fn()
|
|
|
|
return
|
|
|
|
}
|