Skip to content

Instantly share code, notes, and snippets.

@bobbypage
Last active January 15, 2021 05:01
Show Gist options
  • Save bobbypage/a95048136c52b1de35881538806c6f68 to your computer and use it in GitHub Desktop.
Save bobbypage/a95048136c52b1de35881538806c6f68 to your computer and use it in GitHub Desktop.
shutdown-example-app
/*
Dockerfile
FROM golang:1.15 AS builder
WORKDIR /shutdown-example-app
COPY main.go /shutdown-example-app
RUN go build -o shutdown-example-app
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o shutdown-example-app .
FROM scratch
WORKDIR /root/
COPY --from=builder /shutdown-example-app .
CMD ["./shutdown-example-app"]
*/
package main
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
)
func startWorking() {
shutdown := make(chan int)
//create a notification channel to shutdown
sigChan := make(chan os.Signal, 1)
go func() {
for {
log.Println(fmt.Sprintf("Working %s", time.Now().String()))
time.Sleep(1 * time.Second)
}
}()
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigChan
for {
log.Println(fmt.Sprintf("Got SIGTERM Shutdown %s", time.Now().String()))
time.Sleep(1 * time.Second)
continue
}
}()
<-shutdown
}
func main() {
log.Println("Started shutdown-example-app")
startWorking()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment