diff options
author | ale <ale@incal.net> | 2017-12-19 09:05:37 +0000 |
---|---|---|
committer | ale <ale@incal.net> | 2017-12-19 09:05:37 +0000 |
commit | ddecd3760f89ef9ef7765bf445024c402ab4ad5a (patch) | |
tree | 9af76df39803c62d5657fea195d7f1924a07eb7d /cmd | |
parent | 03f5e29656ffdbcca651e9839bcfad6661e4c4e0 (diff) | |
download | crawl-ddecd3760f89ef9ef7765bf445024c402ab4ad5a.tar.gz crawl-ddecd3760f89ef9ef7765bf445024c402ab4ad5a.zip |
Exit gracefully on signals
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/crawl/crawl.go | 19 |
1 files changed, 19 insertions, 0 deletions
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) } |