SIGINT (interrupt) signal receiving refactored and fixed working on some conditions

This commit is contained in:
Musab Gültekin 2021-04-17 14:11:17 +03:00
parent 6a23efd175
commit c527d0b885

View File

@ -162,6 +162,12 @@ func (g *Geziyor) Start() {
}()
}
// Wait for SIGINT (interrupt) signal.
shutdownChan := make(chan os.Signal, 1)
shutdownDoneChan := make(chan struct{})
signal.Notify(shutdownChan, os.Interrupt)
go g.interruptSignalWaiter(shutdownChan, shutdownDoneChan)
// Start Requests
if g.Opt.StartRequestsFunc != nil {
g.Opt.StartRequestsFunc(g)
@ -171,23 +177,6 @@ func (g *Geziyor) Start() {
}
}
// 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:
internal.Logger.Println("Received SIGINT, shutting down gracefully. Send again to force")
g.shutdown = true
signal.Stop(shutdownChan)
case <-shutdownDoneChan:
return
}
}
}()
g.wgRequests.Wait()
close(g.Exports)
g.wgExporters.Wait()
@ -319,3 +308,17 @@ func (g *Geziyor) recoverMe() {
g.metrics.PanicCounter.Add(1)
}
}
// interruptSignalWaiter waits data from provided channels and stops scraper if shutdownChan channel receives SIGINT
func (g *Geziyor) interruptSignalWaiter(shutdownChan chan os.Signal, shutdownDoneChan chan struct{}) {
for {
select {
case <-shutdownChan:
internal.Logger.Println("Received SIGINT, shutting down gracefully. Send again to force")
g.shutdown = true
signal.Stop(shutdownChan)
case <-shutdownDoneChan:
return
}
}
}