From ddecd3760f89ef9ef7765bf445024c402ab4ad5a Mon Sep 17 00:00:00 2001 From: ale Date: Tue, 19 Dec 2017 09:05:37 +0000 Subject: Exit gracefully on signals --- cmd/crawl/crawl.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'cmd') diff --git a/cmd/crawl/crawl.go b/cmd/crawl/crawl.go index abf2b42..e7e8582 100644 --- a/cmd/crawl/crawl.go +++ b/cmd/crawl/crawl.go @@ -11,11 +11,13 @@ import ( "log" "net/http" "os" + "os/signal" "runtime/pprof" "strconv" "strings" "sync" "sync/atomic" + "syscall" "time" "git.autistici.org/ale/crawl" @@ -224,9 +226,26 @@ func main() { if err != nil { log.Fatal(err) } + + // Set up signal handlers so we can terminate gently if possible. + var signaled atomic.Value + signaled.Store(false) + sigCh := make(chan os.Signal, 1) + go func() { + <-sigCh + log.Printf("exiting due to signal") + signaled.Store(true) + crawler.Stop() + }() + signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) + crawler.Run(*concurrency) crawler.Close() + + if signaled.Load().(bool) { + os.Exit(1) + } if !*keepDb { os.RemoveAll(*dbPath) } -- cgit v1.2.3-54-g00ecf