parent
9aa181b474
commit
78fb5e122c
@ -0,0 +1,56 @@
|
||||
package themis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
var loc *time.Location
|
||||
|
||||
func init() {
|
||||
loc, _ = time.LoadLocation("America/Toronto")
|
||||
}
|
||||
|
||||
type Notifier struct {
|
||||
c chan struct{}
|
||||
}
|
||||
|
||||
func NewNotifier(c chan struct{}) *Notifier {
|
||||
return &Notifier{
|
||||
c: c,
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Notifier) Start(ctx context.Context) {
|
||||
m := NextMonday()
|
||||
sat := m.AddDate(0, 0, -2)
|
||||
if sat.Before(time.Now()) {
|
||||
sat = sat.AddDate(0, 0, 7)
|
||||
}
|
||||
|
||||
t, err := time.ParseInLocation(time.DateTime, fmt.Sprintf("%s 16:35:00", sat.Format(time.DateOnly)), loc)
|
||||
if err != nil {
|
||||
panic("failed to parse next monday notif time. this is likely a bug.")
|
||||
}
|
||||
|
||||
first := time.NewTimer(time.Until(t))
|
||||
<-first.C
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
n.c <- struct{}{}
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(time.Hour * 24 * 7)
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
n.c <- struct{}{}
|
||||
}
|
||||
time.Sleep(time.Hour)
|
||||
}
|
||||
}
|
Loading…
Reference in new issue