mirror of
https://github.com/zrepl/zrepl.git
synced 2024-11-26 02:14:44 +01:00
60 lines
1.0 KiB
Go
60 lines
1.0 KiB
Go
package job
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/zrepl/zrepl/logger"
|
|
)
|
|
|
|
type Logger = logger.Logger
|
|
|
|
type contextKey int
|
|
|
|
const (
|
|
contextKeyLog contextKey = iota
|
|
contextKeyWakeup
|
|
)
|
|
|
|
func GetLogger(ctx context.Context) Logger {
|
|
if l, ok := ctx.Value(contextKeyLog).(Logger); ok {
|
|
return l
|
|
}
|
|
return logger.NewNullLogger()
|
|
}
|
|
|
|
func WithLogger(ctx context.Context, l Logger) context.Context {
|
|
return context.WithValue(ctx, contextKeyLog, l)
|
|
}
|
|
|
|
type WakeupFunc func() error
|
|
|
|
var AlreadyWokenUp = errors.New("already woken up")
|
|
|
|
func WithWakeup(ctx context.Context) (context.Context, WakeupFunc) {
|
|
wc := make(chan struct{})
|
|
wuf := func() error {
|
|
select {
|
|
case wc <- struct{}{}:
|
|
return nil
|
|
default:
|
|
return AlreadyWokenUp
|
|
}
|
|
}
|
|
return context.WithValue(ctx, contextKeyWakeup, wc), wuf
|
|
}
|
|
|
|
type Job interface {
|
|
Name() string
|
|
Run(ctx context.Context)
|
|
Status() interface{}
|
|
}
|
|
|
|
func WaitWakeup(ctx context.Context) <-chan struct{} {
|
|
wc, ok := ctx.Value(contextKeyWakeup).(chan struct{})
|
|
if !ok {
|
|
wc = make(chan struct{})
|
|
}
|
|
return wc
|
|
}
|
|
|