Created
July 1, 2018 14:47
-
-
Save p4tin/cefafe03401bd28f21c12cba99a60c79 to your computer and use it in GitHub Desktop.
Capturing docker stop in a golang application (no need for S6!!)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from alpine:latest | |
COPY main / | |
CMD [ "./main" ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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