forked from mtusi2026/mtusi2026lab7
30 lines
641 B
Go
30 lines
641 B
Go
// Package lifecycle connects an explicitly supplied health monitor to composition cancellation.
|
|
package lifecycle
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
)
|
|
|
|
type Ports struct {
|
|
Watch func(context.Context) error
|
|
Stop func()
|
|
Report func()
|
|
}
|
|
type Runner struct{ ports Ports }
|
|
|
|
var ErrConfiguration = errors.New("invalid lifecycle configuration")
|
|
|
|
func New(p Ports) (*Runner, error) {
|
|
if p.Watch == nil || p.Stop == nil || p.Report == nil {
|
|
return nil, ErrConfiguration
|
|
}
|
|
return &Runner{p}, nil
|
|
}
|
|
func (r *Runner) Run(ctx context.Context) {
|
|
if e := r.ports.Watch(ctx); e != nil && ctx.Err() == nil {
|
|
r.ports.Report()
|
|
r.ports.Stop()
|
|
}
|
|
}
|