Add SIGTERM monitoring (#101)

This commit is contained in:
Joona Hoikkala 2019-11-16 16:51:29 +02:00 committed by GitHub
parent 3949e49b3b
commit ec480844a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,7 +4,10 @@ import (
"fmt"
"log"
"math/rand"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
@ -66,6 +69,8 @@ func (j *Job) Start() {
j.Output.Banner()
}
j.Running = true
// Monitor for SIGTERM and do cleanup properly (writing the output files etc)
j.interruptMonitor()
var wg sync.WaitGroup
wg.Add(1)
go j.runProgress(&wg)
@ -105,6 +110,17 @@ func (j *Job) Start() {
return
}
func (j *Job) interruptMonitor() {
sigChan := make(chan os.Signal, 2)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
for _ = range sigChan {
j.Error = "Caught keyboard interrupt (Ctrl-C)\n"
j.Stop()
}
}()
}
func (j *Job) runProgress(wg *sync.WaitGroup) {
defer wg.Done()
j.startTime = time.Now()