Skip to content

Instantly share code, notes, and snippets.

@p4tin
Created July 1, 2018 14:47
Show Gist options
  • Save p4tin/cefafe03401bd28f21c12cba99a60c79 to your computer and use it in GitHub Desktop.
Save p4tin/cefafe03401bd28f21c12cba99a60c79 to your computer and use it in GitHub Desktop.
Capturing docker stop in a golang application (no need for S6!!)
from alpine:latest
COPY main /
CMD [ "./main" ]
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
shutdown := make(chan int)
//create a notification channel to shutdown
sigChan := make(chan os.Signal, 1)
go func() {
for {
fmt.Println("Working...")
time.Sleep(3 * time.Second)
}
}()
//register for interupt (Ctrl+C) and SIGTERM (docker)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigChan
fmt.Println("Shutting down...")
shutdown <- 1
}()
<-shutdown
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment