Files
bf982cc194 feat: Replace devbox & moon by Earthly (#647)
Also add dev environment.


Co-authored-by: Geoffrey Ragot <[email protected]>
2023-10-30 12:43:44 +01:00

79 lines
1.3 KiB
Go

package service
import (
"context"
)
type contextKey string
const (
lifecycleContextKey contextKey = "ready"
)
var closedChan = make(chan struct{})
func init() {
close(closedChan)
}
type lifecycle struct {
ready chan struct{}
stopped chan struct{}
}
func newLifecycle() *lifecycle {
return &lifecycle{
ready: make(chan struct{}),
stopped: make(chan struct{}),
}
}
func contextWithLifecycle(ctx context.Context, lc *lifecycle) context.Context {
return context.WithValue(ctx, lifecycleContextKey, lc)
}
func lifecycleFromContext(ctx context.Context) *lifecycle {
lc := ctx.Value(lifecycleContextKey)
if lc == nil {
return nil
}
return lc.(*lifecycle)
}
func ContextWithLifecycle(ctx context.Context) context.Context {
return context.WithValue(ctx, lifecycleContextKey, newLifecycle())
}
func markAsAppReady(ctx context.Context) {
lc := lifecycleFromContext(ctx)
if lc == nil {
return
}
close(lc.ready)
}
func markAsAppStopped(ctx context.Context) {
lc := lifecycleFromContext(ctx)
if lc == nil {
return
}
close(lc.stopped)
}
func Ready(ctx context.Context) chan struct{} {
lc := lifecycleFromContext(ctx)
if lc == nil {
return closedChan
}
return lc.ready
}
func Stopped(ctx context.Context) chan struct{} {
lc := lifecycleFromContext(ctx)
if lc == nil {
return closedChan
}
return lc.stopped
}