Documentation ¶
Overview ¶
Package mgr provides simple managing of flow control and logging.
Index ¶
- Variables
- func RunModules(ctx context.Context, modules ...Module) error
- type EventCallback
- type EventCallbackFunc
- type EventMgr
- type EventSubscription
- type Group
- type Manager
- func (m *Manager) Cancel()
- func (m *Manager) Ctx() context.Context
- func (m *Manager) Debug(msg string, args ...any)
- func (m *Manager) Do(name string, fn func(w *WorkerCtx) error) error
- func (m *Manager) Done() <-chan struct{}
- func (m *Manager) Error(msg string, args ...any)
- func (m *Manager) Go(name string, fn func(w *WorkerCtx) error)
- func (m *Manager) Info(msg string, args ...any)
- func (m *Manager) IsDone() bool
- func (m *Manager) Log(level slog.Level, msg string, args ...any)
- func (m *Manager) LogAttrs(level slog.Level, msg string, attrs ...slog.Attr)
- func (m *Manager) LogEnabled(level slog.Level) bool
- func (m *Manager) Name() string
- func (m *Manager) WaitForWorkers(max time.Duration) (done bool)
- func (m *Manager) Warn(msg string, args ...any)
- type Module
- type WorkerCtx
- func (w *WorkerCtx) AddToCtx(ctx context.Context) context.Context
- func (w *WorkerCtx) Cancel()
- func (w *WorkerCtx) Ctx() context.Context
- func (w *WorkerCtx) Debug(msg string, args ...any)
- func (w *WorkerCtx) Done() <-chan struct{}
- func (w *WorkerCtx) Error(msg string, args ...any)
- func (w *WorkerCtx) Info(msg string, args ...any)
- func (w *WorkerCtx) IsDone() bool
- func (w *WorkerCtx) Log(level slog.Level, msg string, args ...any)
- func (w *WorkerCtx) LogAttrs(level slog.Level, msg string, attrs ...slog.Attr)
- func (w *WorkerCtx) LogEnabled(level slog.Level) bool
- func (w *WorkerCtx) Logger() *slog.Logger
- func (w *WorkerCtx) Warn(msg string, args ...any)
Constants ¶
This section is empty.
Variables ¶
var WorkerCtxContextKey = workerContextKey{}
WorkerCtxContextKey is the key used to add the WorkerCtx to a context.
Functions ¶
Types ¶
type EventCallback ¶ added in v0.4.0
type EventCallback[T any] struct { // contains filtered or unexported fields }
EventCallback is a registered callback to an event.
type EventCallbackFunc ¶ added in v0.4.0
EventCallbackFunc defines the event callback function.
type EventMgr ¶ added in v0.4.0
type EventMgr[T any] struct { // contains filtered or unexported fields }
EventMgr is a simple event manager.
func NewEventMgr ¶ added in v0.4.0
NewEventMgr returns a new event manager. It is easiest used as a public field on a struct, so that others can simply Subscribe() oder AddCallback().
func (*EventMgr[T]) AddCallback ¶ added in v0.4.0
func (em *EventMgr[T]) AddCallback(callbackName string, callback EventCallbackFunc[T])
AddCallback adds a callback to executed on events. The received events are shared among all subscribers and callbacks. Be sure to apply proper concurrency safeguards, if applicable.
func (*EventMgr[T]) Submit ¶ added in v0.4.0
func (em *EventMgr[T]) Submit(event T)
Submit submits a new event.
func (*EventMgr[T]) Subscribe ¶ added in v0.4.0
func (em *EventMgr[T]) Subscribe(subscriberName string, chanSize int) *EventSubscription[T]
Subscribe subscribes to events. The received events are shared among all subscribers and callbacks. Be sure to apply proper concurrency safeguards, if applicable.
type EventSubscription ¶ added in v0.4.0
type EventSubscription[T any] struct { // contains filtered or unexported fields }
EventSubscription is a subscription to an event.
func (*EventSubscription[T]) Cancel ¶ added in v0.4.0
func (es *EventSubscription[T]) Cancel()
Cancel cancels the subscription. The events channel is not closed, but will not receive new events.
func (*EventSubscription[T]) Done ¶ added in v0.4.0
func (es *EventSubscription[T]) Done() bool
Done returns whether the event subscription has been canceled.
func (*EventSubscription[T]) Events ¶ added in v0.4.0
func (es *EventSubscription[T]) Events() <-chan T
Events returns a read channel for the events. The received events are shared among all subscribers and callbacks. Be sure to apply proper concurrency safeguards, if applicable.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group describes a group of modules.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager manages workers.
func NewWithContext ¶
NewWithContext returns a new manager that uses the given context.
func (*Manager) Do ¶ added in v0.1.0
Do directly executes the given function (as a "worker"). The worker context has - A separate context which is canceled when the functions returns. - Access to named structure logging. - Given function is re-run after failure (with backoff). - Panic catching. - Flow control helpers.
func (*Manager) Done ¶
func (m *Manager) Done() <-chan struct{}
Done returns the context Done channel.
func (*Manager) Go ¶ added in v0.1.0
Go starts the given function in a goroutine (as a "worker"). The worker context has - A separate context which is canceled when the functions returns. - Access to named structure logging. - Given function is re-run after failure (with backoff). - Panic catching. - Flow control helpers.
func (*Manager) Log ¶
Log emits a log record with the current time and the given level and message. The manager context is automatically supplied.
func (*Manager) LogAttrs ¶
LogAttrs is a more efficient version of Log() that accepts only Attrs. The manager context is automatically supplied.
func (*Manager) LogEnabled ¶
LogEnabled reports whether the logger emits log records at the given level. The manager context is automatically supplied.
func (*Manager) WaitForWorkers ¶
WaitForWorkers waits for all workers of this manager to be done. The default maximum waiting time is one minute.
type WorkerCtx ¶
type WorkerCtx struct {
// contains filtered or unexported fields
}
WorkerCtx provides workers with the necessary environment for flow control and logging.
func WorkerFromCtx ¶
WorkerFromCtx returns the WorkerCtx from the given context.
func (*WorkerCtx) Cancel ¶
func (w *WorkerCtx) Cancel()
Cancel cancels the worker context. Is automatically called after the worker stops/returns, regardless of error.
func (*WorkerCtx) Ctx ¶
Ctx returns the worker context. Is automatically canceled after the worker stops/returns, regardless of error.
func (*WorkerCtx) Done ¶
func (w *WorkerCtx) Done() <-chan struct{}
Done returns the context Done channel.
func (*WorkerCtx) Log ¶
Log emits a log record with the current time and the given level and message. The worker context is automatically supplied.
func (*WorkerCtx) LogAttrs ¶
LogAttrs is a more efficient version of Log() that accepts only Attrs. The worker context is automatically supplied.
func (*WorkerCtx) LogEnabled ¶
LogEnabled reports whether the logger emits log records at the given level. The worker context is automatically supplied.