mirror of https://github.com/hrfee/jfa-go
checkInvite no longer loops over all invites and checks for expiry, that functionality has moved to checkInvites. Couple more rogue print statements removed aswell.pull/20/head
parent
dba20bd3ea
commit
4e16f6fd48
@ -0,0 +1,50 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
// https://bbengfort.github.io/snippets/2016/06/26/background-work-goroutines-timer.html THANKS
|
||||||
|
|
||||||
|
type Repeater struct {
|
||||||
|
Stopped bool
|
||||||
|
ShutdownChannel chan string
|
||||||
|
Interval time.Duration
|
||||||
|
period time.Duration
|
||||||
|
ctx *appContext
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRepeater(interval time.Duration, ctx *appContext) *Repeater {
|
||||||
|
return &Repeater{
|
||||||
|
Stopped: false,
|
||||||
|
ShutdownChannel: make(chan string),
|
||||||
|
Interval: interval,
|
||||||
|
period: interval,
|
||||||
|
ctx: ctx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rt *Repeater) Run() {
|
||||||
|
rt.ctx.info.Println("Invite daemon started")
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-rt.ShutdownChannel:
|
||||||
|
rt.ShutdownChannel <- "Down"
|
||||||
|
return
|
||||||
|
case <-time.After(rt.period):
|
||||||
|
break
|
||||||
|
}
|
||||||
|
started := time.Now()
|
||||||
|
rt.ctx.storage.loadInvites()
|
||||||
|
rt.ctx.debug.Println("Daemon: Checking invites")
|
||||||
|
rt.ctx.checkInvites()
|
||||||
|
finished := time.Now()
|
||||||
|
duration := finished.Sub(started)
|
||||||
|
rt.period = rt.Interval - duration
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rt *Repeater) Shutdown() {
|
||||||
|
rt.Stopped = true
|
||||||
|
rt.ShutdownChannel <- "Down"
|
||||||
|
<-rt.ShutdownChannel
|
||||||
|
close(rt.ShutdownChannel)
|
||||||
|
}
|
Loading…
Reference in new issue