2022-04-28 15:23:11 +03:00
|
|
|
package worker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-05-07 18:36:01 +03:00
|
|
|
"fmt"
|
|
|
|
"path"
|
2022-05-01 17:45:15 +03:00
|
|
|
"reflect"
|
2022-04-28 15:23:11 +03:00
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"codeberg.org/gruf/go-runners"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Worker represents a proccessor for MsgType objects, using a worker pool to allocate resources.
|
|
|
|
type Worker[MsgType any] struct {
|
|
|
|
workers runners.WorkerPool
|
|
|
|
process func(context.Context, MsgType) error
|
2022-05-01 17:45:15 +03:00
|
|
|
prefix string // contains type prefix for logging
|
2022-04-28 15:23:11 +03:00
|
|
|
}
|
|
|
|
|
2022-05-07 18:36:01 +03:00
|
|
|
// New returns a new Worker[MsgType] with given number of workers and queue ratio,
|
|
|
|
// where the queue ratio is multiplied by no. workers to get queue size. If args < 1
|
|
|
|
// then suitable defaults are determined from the runtime's GOMAXPROCS variable.
|
|
|
|
func New[MsgType any](workers int, queueRatio int) *Worker[MsgType] {
|
|
|
|
var zero MsgType
|
|
|
|
|
2022-04-28 15:23:11 +03:00
|
|
|
if workers < 1 {
|
2022-05-01 17:45:15 +03:00
|
|
|
// ensure sensible workers
|
2022-04-28 15:23:11 +03:00
|
|
|
workers = runtime.GOMAXPROCS(0)
|
|
|
|
}
|
2022-05-07 18:36:01 +03:00
|
|
|
if queueRatio < 1 {
|
|
|
|
// ensure sensible ratio
|
|
|
|
queueRatio = 100
|
2022-04-28 15:23:11 +03:00
|
|
|
}
|
2022-05-01 17:45:15 +03:00
|
|
|
|
2022-05-07 18:36:01 +03:00
|
|
|
// Calculate the short type string for the msg type
|
|
|
|
msgType := reflect.TypeOf(zero).String()
|
|
|
|
_, msgType = path.Split(msgType)
|
|
|
|
|
2022-05-01 17:45:15 +03:00
|
|
|
w := &Worker[MsgType]{
|
2022-05-07 18:36:01 +03:00
|
|
|
workers: runners.NewWorkerPool(workers, workers*queueRatio),
|
2022-04-28 15:23:11 +03:00
|
|
|
process: nil,
|
2022-05-07 18:36:01 +03:00
|
|
|
prefix: fmt.Sprintf("worker.Worker[%s]", msgType),
|
2022-04-28 15:23:11 +03:00
|
|
|
}
|
2022-05-01 17:45:15 +03:00
|
|
|
|
|
|
|
// Log new worker creation with type prefix
|
2022-05-07 18:36:01 +03:00
|
|
|
logrus.Infof("%s created with workers=%d queue=%d",
|
|
|
|
w.prefix,
|
|
|
|
workers,
|
|
|
|
workers*queueRatio,
|
|
|
|
)
|
2022-05-01 17:45:15 +03:00
|
|
|
|
|
|
|
return w
|
2022-04-28 15:23:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start will attempt to start the underlying worker pool, or return error.
|
|
|
|
func (w *Worker[MsgType]) Start() error {
|
2022-05-07 18:36:01 +03:00
|
|
|
logrus.Infof("%s starting", w.prefix)
|
2022-05-01 17:45:15 +03:00
|
|
|
|
|
|
|
// Check processor was set
|
2022-04-28 15:23:11 +03:00
|
|
|
if w.process == nil {
|
|
|
|
return errors.New("nil Worker.process function")
|
|
|
|
}
|
2022-05-01 17:45:15 +03:00
|
|
|
|
|
|
|
// Attempt to start pool
|
2022-04-28 15:23:11 +03:00
|
|
|
if !w.workers.Start() {
|
|
|
|
return errors.New("failed to start Worker pool")
|
|
|
|
}
|
2022-05-01 17:45:15 +03:00
|
|
|
|
2022-04-28 15:23:11 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop will attempt to stop the underlying worker pool, or return error.
|
|
|
|
func (w *Worker[MsgType]) Stop() error {
|
2022-05-07 18:36:01 +03:00
|
|
|
logrus.Infof("%s stopping", w.prefix)
|
2022-05-01 17:45:15 +03:00
|
|
|
|
|
|
|
// Attempt to stop pool
|
2022-04-28 15:23:11 +03:00
|
|
|
if !w.workers.Stop() {
|
|
|
|
return errors.New("failed to stop Worker pool")
|
|
|
|
}
|
2022-05-01 17:45:15 +03:00
|
|
|
|
2022-04-28 15:23:11 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetProcessor will set the Worker's processor function, which is called for each queued message.
|
|
|
|
func (w *Worker[MsgType]) SetProcessor(fn func(context.Context, MsgType) error) {
|
|
|
|
if w.process != nil {
|
2022-05-07 18:36:01 +03:00
|
|
|
logrus.Panicf("%s Worker.process is already set", w.prefix)
|
2022-04-28 15:23:11 +03:00
|
|
|
}
|
|
|
|
w.process = fn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Queue will queue provided message to be processed with there's a free worker.
|
|
|
|
func (w *Worker[MsgType]) Queue(msg MsgType) {
|
2022-05-07 18:36:01 +03:00
|
|
|
logrus.Tracef("%s queueing message (workers=%d queue=%d): %+v",
|
|
|
|
w.prefix, w.workers.Workers(), w.workers.Queue(), msg,
|
|
|
|
)
|
2022-04-28 15:23:11 +03:00
|
|
|
w.workers.Enqueue(func(ctx context.Context) {
|
|
|
|
if err := w.process(ctx, msg); err != nil {
|
2022-05-07 18:36:01 +03:00
|
|
|
logrus.Errorf("%s %v", w.prefix, err)
|
2022-04-28 15:23:11 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|