33 lines
596 B
Go
33 lines
596 B
Go
package podbackend
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
// Lifecycle reconciles durable terminal markers into backend cleanup.
|
|
type Lifecycle struct {
|
|
Backend Backend
|
|
Interval time.Duration
|
|
OnError func(error)
|
|
}
|
|
|
|
func (l Lifecycle) Run(ctx context.Context) error {
|
|
interval := l.Interval
|
|
if interval <= 0 {
|
|
interval = 2 * time.Second
|
|
}
|
|
for {
|
|
if _, err := l.Backend.CleanupTerminated(ctx); err != nil && ctx.Err() == nil && l.OnError != nil {
|
|
l.OnError(err)
|
|
}
|
|
timer := time.NewTimer(interval)
|
|
select {
|
|
case <-ctx.Done():
|
|
timer.Stop()
|
|
return nil
|
|
case <-timer.C:
|
|
}
|
|
}
|
|
}
|