Proper shutdown.

This commit is contained in:
Andrey Petrov 2014-05-15 15:33:09 -07:00
parent e94164c692
commit 81e7762d1d
2 changed files with 7 additions and 11 deletions

16
cmd.go
View File

@ -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.

View File

@ -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
}
}