make sure everything is saved before the end of the world

This commit is contained in:
Ted Unangst 2019-11-08 17:43:32 -05:00
parent d59a13efd3
commit 4b9cdf912b
1 changed files with 33 additions and 0 deletions

33
web.go
View File

@ -26,6 +26,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"os/signal"
"regexp" "regexp"
"sort" "sort"
"strconv" "strconv"
@ -924,6 +925,11 @@ func tracker() {
tracks = make(map[string][]string) tracks = make(map[string][]string)
} }
sleeper.Reset(timeout) sleeper.Reset(timeout)
case <-endoftheworld:
if len(tracks) > 0 {
savetracks(tracks)
}
readyalready <- true
} }
} }
} }
@ -2017,6 +2023,32 @@ func apihandler(w http.ResponseWriter, r *http.Request) {
} }
} }
var endoftheworld = make(chan bool)
var readyalready = make(chan bool)
func enditall() {
sig := make(chan os.Signal)
signal.Notify(sig, os.Interrupt)
<-sig
count := 0
log.Printf("stopping...")
sendloop:
for {
select {
case endoftheworld <- true:
count++
default:
break sendloop
}
}
log.Printf("waiting...")
for i := 0; i < count; i++ {
<-readyalready
}
log.Printf("apocalypse")
os.Exit(0)
}
func serve() { func serve() {
db := opendatabase() db := opendatabase()
login.Init(db) login.Init(db)
@ -2025,6 +2057,7 @@ func serve() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
go enditall()
go redeliverator() go redeliverator()
go tracker() go tracker()