From 4b9cdf912b14de489a2d182ef2122aecb9243cec Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Fri, 8 Nov 2019 17:43:32 -0500 Subject: [PATCH] make sure everything is saved before the end of the world --- web.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/web.go b/web.go index c7f6a82..aa91e1e 100644 --- a/web.go +++ b/web.go @@ -26,6 +26,7 @@ import ( "net/http" "net/url" "os" + "os/signal" "regexp" "sort" "strconv" @@ -924,6 +925,11 @@ func tracker() { tracks = make(map[string][]string) } 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() { db := opendatabase() login.Init(db) @@ -2025,6 +2057,7 @@ func serve() { if err != nil { log.Fatal(err) } + go enditall() go redeliverator() go tracker()