Files
monibuca/pkg/unit.go
2024-07-05 17:21:27 +08:00

35 lines
678 B
Go

package pkg
import (
"context"
"log/slog"
"time"
)
const TraceLevel = slog.Level(-8)
type Unit struct {
ID int
StartTime time.Time
*slog.Logger `json:"-" yaml:"-"`
context.Context `json:"-" yaml:"-"`
context.CancelCauseFunc `json:"-" yaml:"-"`
}
func (unit *Unit) Trace(msg string, fields ...any) {
unit.Log(unit.Context, TraceLevel, msg, fields...)
}
func (unit *Unit) IsStopped() bool {
return unit.StopReason() != nil
}
func (unit *Unit) StopReason() error {
return context.Cause(unit.Context)
}
func (unit *Unit) Stop(err error) {
unit.Info("stop", "reason", err.Error())
unit.CancelCauseFunc(err)
}