From c527d0b885b718c25485064398085c32c98b10e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Musab=20G=C3=BCltekin?= Date: Sat, 17 Apr 2021 14:11:17 +0300 Subject: [PATCH] SIGINT (interrupt) signal receiving refactored and fixed working on some conditions --- geziyor.go | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/geziyor.go b/geziyor.go index a2652de..1fd4ddf 100644 --- a/geziyor.go +++ b/geziyor.go @@ -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 + } + } +}