diff --git a/cmd.go b/cmd.go index 13417d0..6c1ad56 100644 --- a/cmd.go +++ b/cmd.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "os/signal" + "syscall" "time" "github.com/alexcesaro/log" @@ -94,24 +95,22 @@ func main() { return } - isActive := true + resultChan := make(chan Result) // Construct interrupt handler sig := make(chan os.Signal, 1) - signal.Notify(sig, os.Interrupt) + signal.Notify(sig, os.Interrupt, syscall.SIGTERM) go func() { <-sig // Wait for ^C signal logger.Warningf("Interrupt signal detected, shutting down gracefully by waiting for active workers to finish.") crawler.Shutdown() - // FIXME: This isn't working? <-sig // Hurry up? - logger.Warningf("Super-interrupt. Abandoning in-progress workers.") - isActive = false + logger.Warningf("Urgent interrupt. Abandoning in-progress workers.") + close(resultChan) // FIXME: Could this cause stuff to asplode? }() // Launch crawler - resultChan := make(chan Result) go crawler.Run(resultChan, options.Concurrency) logger.Infof("Crawler started with %d concurrency limit.", options.Concurrency) @@ -139,11 +138,6 @@ func main() { logger.Infof("StopAfter count reached, shutting down gracefully.") crawler.Shutdown() } - - if !isActive { - // No time to wait, finish writing and quit. - break - } } w.Write([]byte("]")) // No error checking here because it's too late to care. diff --git a/crawler.go b/crawler.go index 1380081..4215a77 100644 --- a/crawler.go +++ b/crawler.go @@ -31,6 +31,7 @@ func NewCrawler(client *Client, seeds []string, peerAge time.Duration) *Crawler client: client, seenFilter: map[string]bool{}, peerAge: peerAge, + shutdown: make(chan struct{}, 1), } filter := func(address string) *string { return c.filter(address) @@ -187,6 +188,7 @@ func (c *Crawler) Run(resultChan chan<- Result, numWorkers int) { <-workerChan case <-c.shutdown: + logger.Infof("Shutting down.") isActive = false } }