mirror of https://github.com/hrfee/jfa-go
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.
51 lines
1.1 KiB
51 lines
1.1 KiB
4 years ago
|
package main
|
||
|
|
||
|
import "time"
|
||
|
|
||
|
// https://bbengfort.github.io/snippets/2016/06/26/background-work-goroutines-timer.html THANKS
|
||
|
|
||
4 years ago
|
type inviteDaemon struct {
|
||
4 years ago
|
Stopped bool
|
||
|
ShutdownChannel chan string
|
||
|
Interval time.Duration
|
||
|
period time.Duration
|
||
4 years ago
|
app *appContext
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
func newInviteDaemon(interval time.Duration, app *appContext) *inviteDaemon {
|
||
|
return &inviteDaemon{
|
||
4 years ago
|
Stopped: false,
|
||
|
ShutdownChannel: make(chan string),
|
||
|
Interval: interval,
|
||
|
period: interval,
|
||
4 years ago
|
app: app,
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
func (rt *inviteDaemon) run() {
|
||
4 years ago
|
rt.app.info.Println("Invite daemon started")
|
||
4 years ago
|
for {
|
||
|
select {
|
||
|
case <-rt.ShutdownChannel:
|
||
|
rt.ShutdownChannel <- "Down"
|
||
|
return
|
||
|
case <-time.After(rt.period):
|
||
|
break
|
||
|
}
|
||
|
started := time.Now()
|
||
4 years ago
|
rt.app.storage.loadInvites()
|
||
|
rt.app.debug.Println("Daemon: Checking invites")
|
||
|
rt.app.checkInvites()
|
||
4 years ago
|
finished := time.Now()
|
||
|
duration := finished.Sub(started)
|
||
|
rt.period = rt.Interval - duration
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
func (rt *inviteDaemon) Shutdown() {
|
||
4 years ago
|
rt.Stopped = true
|
||
|
rt.ShutdownChannel <- "Down"
|
||
|
<-rt.ShutdownChannel
|
||
|
close(rt.ShutdownChannel)
|
||
|
}
|