Refactor exit-after

This commit is contained in:
Leonid Bugaev
2016-07-08 19:27:20 +03:00
parent 6620d25626
commit dae6a8915d
2 changed files with 19 additions and 26 deletions
+1 -7
View File
@@ -32,13 +32,7 @@ func Start(stop chan int) {
for {
select {
case <-stop:
pluginMu.Lock()
for _, p := range Plugins.All {
if cp, ok := p.(io.Closer); ok {
cp.Close()
}
}
pluginMu.Unlock()
finalize()
return
case <-time.After(100 * time.Millisecond):
}
+18 -19
View File
@@ -78,27 +78,33 @@ func main() {
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
for _, p := range Plugins.All {
if cp, ok := p.(io.Closer); ok {
cp.Close()
}
}
finalize()
os.Exit(1)
}()
if Settings.exitAfter >= 1 {
if Settings.exitAfter > 0 {
log.Println("Running gor for a duration of", Settings.exitAfter)
stop := make(chan int)
stopAfter(stop, Settings.exitAfter)
closeCh := make(chan int)
Start(stop)
time.AfterFunc(Settings.exitAfter, func() {
log.Println("Stopping gor after", Settings.exitAfter)
close(closeCh)
})
Start(closeCh)
} else {
Start(nil)
}
}
func finalize() {
for _, p := range Plugins.All {
if cp, ok := p.(io.Closer); ok {
cp.Close()
}
}
}
func profileCPU(cpuprofile string) {
if cpuprofile != "" {
f, err := os.Create(cpuprofile)
@@ -126,11 +132,4 @@ func profileMEM(memprofile string) {
f.Close()
})
}
}
func stopAfter(stop chan int, exitAfter time.Duration) {
time.AfterFunc(exitAfter, func() {
log.Println("Stopping gor, duration of", exitAfter, "reached")
close(stop)
})
}
}