Graceful shut down system implemented
This commit is contained in:
parent
42faa92ece
commit
0d6c2a6864
29
geziyor.go
29
geziyor.go
@ -9,6 +9,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http/cookiejar"
|
"net/http/cookiejar"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
@ -29,6 +30,7 @@ type Geziyor struct {
|
|||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
hostSems map[string]chan struct{}
|
hostSems map[string]chan struct{}
|
||||||
}
|
}
|
||||||
|
shutdown bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGeziyor creates new Geziyor with default values.
|
// NewGeziyor creates new Geziyor with default values.
|
||||||
@ -142,17 +144,35 @@ func (g *Geziyor) Start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Start Requests
|
// Start Requests
|
||||||
if g.Opt.StartRequestsFunc == nil {
|
if g.Opt.StartRequestsFunc != nil {
|
||||||
|
g.Opt.StartRequestsFunc(g)
|
||||||
|
} else {
|
||||||
for _, startURL := range g.Opt.StartURLs {
|
for _, startURL := range g.Opt.StartURLs {
|
||||||
g.Get(startURL, g.Opt.ParseFunc)
|
g.Get(startURL, g.Opt.ParseFunc)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
g.Opt.StartRequestsFunc(g)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wait for SIGINT (interrupt) signal.
|
||||||
|
shutdownChan := make(chan os.Signal, 1)
|
||||||
|
shutdownDoneChan := make(chan struct{})
|
||||||
|
signal.Notify(shutdownChan, os.Interrupt)
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-shutdownChan:
|
||||||
|
log.Println("Received SIGINT, shutting down gracefully. Send again to force")
|
||||||
|
g.shutdown = true
|
||||||
|
signal.Stop(shutdownChan)
|
||||||
|
case <-shutdownDoneChan:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
g.wgRequests.Wait()
|
g.wgRequests.Wait()
|
||||||
close(g.Exports)
|
close(g.Exports)
|
||||||
g.wgExporters.Wait()
|
g.wgExporters.Wait()
|
||||||
|
shutdownDoneChan <- struct{}{}
|
||||||
log.Println("Scraping Finished")
|
log.Println("Scraping Finished")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,6 +211,9 @@ func (g *Geziyor) Head(url string, callback func(g *Geziyor, r *client.Response)
|
|||||||
|
|
||||||
// Do sends an HTTP request
|
// Do sends an HTTP request
|
||||||
func (g *Geziyor) Do(req *client.Request, callback func(g *Geziyor, r *client.Response)) {
|
func (g *Geziyor) Do(req *client.Request, callback func(g *Geziyor, r *client.Response)) {
|
||||||
|
if g.shutdown {
|
||||||
|
return
|
||||||
|
}
|
||||||
if req.Synchronized {
|
if req.Synchronized {
|
||||||
g.do(req, callback)
|
g.do(req, callback)
|
||||||
} else {
|
} else {
|
||||||
|
@ -24,7 +24,7 @@ func TestSimple(t *testing.T) {
|
|||||||
}).Start()
|
}).Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSimpleCache(t *testing.T) {
|
func TestCache(t *testing.T) {
|
||||||
defer leaktest.Check(t)()
|
defer leaktest.Check(t)()
|
||||||
geziyor.NewGeziyor(&geziyor.Options{
|
geziyor.NewGeziyor(&geziyor.Options{
|
||||||
StartURLs: []string{"http://api.ipify.org"},
|
StartURLs: []string{"http://api.ipify.org"},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user