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.
57 lines
913 B
57 lines
913 B
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)
|
|
}
|
|
}
|