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
|
|
|
|
|
2021-02-28 15:41:06 +00:00
|
|
|
type inviteDaemon 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
|
|
|
}
|
|
|
|
|
2021-02-28 15:41:06 +00:00
|
|
|
func newInviteDaemon(interval time.Duration, app *appContext) *inviteDaemon {
|
|
|
|
return &inviteDaemon{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 15:41:06 +00:00
|
|
|
func (rt *inviteDaemon) 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 15:41:06 +00:00
|
|
|
func (rt *inviteDaemon) shutdown() {
|
2020-08-01 14:22:30 +00:00
|
|
|
rt.Stopped = true
|
|
|
|
rt.ShutdownChannel <- "Down"
|
|
|
|
<-rt.ShutdownChannel
|
|
|
|
close(rt.ShutdownChannel)
|
|
|
|
}
|