Skip to content

Commit

Permalink
First Ctrl+C starts graceful shutdown. Second Ctrl+C abruptly ends th…
Browse files Browse the repository at this point in the history
…e program.
  • Loading branch information
RichardKnop committed Nov 14, 2017
1 parent b3e46db commit 85f83f4
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions v1/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (worker *Worker) Launch() error {
errorsChan := make(chan error)
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
quitting := false
var signalsReceived uint

go func() {
for {
Expand All @@ -63,16 +63,19 @@ func (worker *Worker) Launch() error {
select {
case s := <-sig:
log.WARNING.Printf("Signal received: %v", s)
if quitting {
return
signalsReceived++

if signalsReceived < 2 {
// After first Ctrl+C start quitting the worker gracefully
log.WARNING.Print("Waiting for running tasks to finish before shutting down")
go func() {
worker.Quit()
errorsChan <- errors.New("Worker quit gracefully")
}()
} else {
// Abort the program when user hits Ctrl+C second time in a row
errorsChan <- errors.New("Worker quit abruptly")
}

quitting = true
log.WARNING.Print("Waiting for running tasks to finish before shutting down")
go func() {
worker.Quit()
errorsChan <- errors.New("Worker quit")
}()
}
}
}()
Expand Down

0 comments on commit 85f83f4

Please sign in to comment.