You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.1 KiB
69 lines
1.1 KiB
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 17:00: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.Second)
|
|
}
|
|
}
|
|
|
|
func (n *Notifier) NotifyFunc(ctx context.Context, f func()) {
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-n.c:
|
|
f()
|
|
}
|
|
time.Sleep(time.Second)
|
|
}
|
|
}
|