|
|
@ -6,6 +6,7 @@ import (
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var loc *time.Location
|
|
|
|
var loc *time.Location
|
|
|
@ -15,10 +16,10 @@ func init() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Notifier struct {
|
|
|
|
type Notifier struct {
|
|
|
|
c chan struct{}
|
|
|
|
c chan context.Context
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewNotifier(c chan struct{}) *Notifier {
|
|
|
|
func NewNotifier(c chan context.Context) *Notifier {
|
|
|
|
return &Notifier{
|
|
|
|
return &Notifier{
|
|
|
|
c: c,
|
|
|
|
c: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -45,8 +46,11 @@ func (n *Notifier) Start(ctx context.Context) {
|
|
|
|
log.Debug().Msg("context deadline exceeded, exiting notifier")
|
|
|
|
log.Debug().Msg("context deadline exceeded, exiting notifier")
|
|
|
|
return
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
log.Debug().Msg("notifier tick")
|
|
|
|
log.Debug().Str("parent", "ticker").Msg("notifier tick")
|
|
|
|
n.c <- struct{}{}
|
|
|
|
ctx, span := tracer.Start(ctx, "notifier_tick", trace.WithNewRoot())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
n.c <- ctx
|
|
|
|
|
|
|
|
span.End()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ticker := time.NewTicker(time.Hour * 24 * 7)
|
|
|
|
ticker := time.NewTicker(time.Hour * 24 * 7)
|
|
|
@ -56,28 +60,32 @@ func (n *Notifier) Start(ctx context.Context) {
|
|
|
|
log.Debug().Msg("context deadline exceeded, exiting notifier")
|
|
|
|
log.Debug().Msg("context deadline exceeded, exiting notifier")
|
|
|
|
return
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
case <-ticker.C:
|
|
|
|
log.Debug().Msg("notifier tick")
|
|
|
|
log.Debug().Str("parent", "ticker").Msg("notifier tick")
|
|
|
|
n.c <- struct{}{}
|
|
|
|
ctx, span := tracer.Start(ctx, "notifier_tick", trace.WithNewRoot())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
n.c <- ctx
|
|
|
|
|
|
|
|
span.End()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Trigger the notifier manually. Should be used for testing purposes only.
|
|
|
|
// Trigger the notifier manually. Should be used for testing purposes only.
|
|
|
|
func (n *Notifier) Send() {
|
|
|
|
func (n *Notifier) Send(ctx context.Context) {
|
|
|
|
n.c <- struct{}{}
|
|
|
|
log.Debug().Str("parent", "trigger").Ctx(ctx).Msg("notifier tick")
|
|
|
|
|
|
|
|
n.c <- ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO(wperron) is there a (nice) way to instrument this function?
|
|
|
|
// TODO(wperron) is there a (nice) way to instrument this function?
|
|
|
|
func (n *Notifier) NotifyFunc(ctx context.Context, f func()) {
|
|
|
|
func (n *Notifier) NotifyFunc(ctx context.Context, f func(context.Context)) {
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Debug().Msg("context deadline exceeded, exiting notify func")
|
|
|
|
log.Debug().Msg("context deadline exceeded, exiting notify func")
|
|
|
|
return
|
|
|
|
return
|
|
|
|
case <-n.c:
|
|
|
|
case innerCtx := <-n.c:
|
|
|
|
log.Debug().Msg("tick received, notifying function")
|
|
|
|
log.Debug().Ctx(innerCtx).Msg("tick received, notifying function")
|
|
|
|
f()
|
|
|
|
f(innerCtx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|