2020-08-01 14:22:30 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "time"
|
|
|
|
|
|
|
|
// https://bbengfort.github.io/snippets/2016/06/26/background-work-goroutines-timer.html THANKS
|
|
|
|
|
2020-11-22 16:36:43 +00:00
|
|
|
type repeater struct {
|
2020-08-01 14:22:30 +00:00
|
|
|
Stopped bool
|
|
|
|
ShutdownChannel chan string
|
|
|
|
Interval time.Duration
|
|
|
|
period time.Duration
|
2020-08-16 12:36:54 +00:00
|
|
|
app *appContext
|
2020-08-01 14:22:30 +00:00
|
|
|
}
|
|
|
|
|
2020-11-22 16:36:43 +00:00
|
|
|
func newRepeater(interval time.Duration, app *appContext) *repeater {
|
|
|
|
return &repeater{
|
2020-08-01 14:22:30 +00:00
|
|
|
Stopped: false,
|
|
|
|
ShutdownChannel: make(chan string),
|
|
|
|
Interval: interval,
|
|
|
|
period: interval,
|
2020-08-16 12:36:54 +00:00
|
|
|
app: app,
|
2020-08-01 14:22:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-22 16:36:43 +00:00
|
|
|
func (rt *repeater) run() {
|
2020-08-16 12:36:54 +00:00
|
|
|
rt.app.info.Println("Invite daemon started")
|
2020-08-01 14:22:30 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-rt.ShutdownChannel:
|
|
|
|
rt.ShutdownChannel <- "Down"
|
|
|
|
return
|
|
|
|
case <-time.After(rt.period):
|
|
|
|
break
|
|
|
|
}
|
|
|
|
started := time.Now()
|
2020-08-16 12:36:54 +00:00
|
|
|
rt.app.storage.loadInvites()
|
|
|
|
rt.app.debug.Println("Daemon: Checking invites")
|
|
|
|
rt.app.checkInvites()
|
2020-08-01 14:22:30 +00:00
|
|
|
finished := time.Now()
|
|
|
|
duration := finished.Sub(started)
|
|
|
|
rt.period = rt.Interval - duration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-22 16:36:43 +00:00
|
|
|
func (rt *repeater) shutdown() {
|
2020-08-01 14:22:30 +00:00
|
|
|
rt.Stopped = true
|
|
|
|
rt.ShutdownChannel <- "Down"
|
|
|
|
<-rt.ShutdownChannel
|
|
|
|
close(rt.ShutdownChannel)
|
|
|
|
}
|