Skip to content

Instantly share code, notes, and snippets.

@fieldju
Created May 3, 2022 01:43
Show Gist options
  • Save fieldju/f044a558f269d1f5fa40d1ed83fe6eec to your computer and use it in GitHub Desktop.
Save fieldju/f044a558f269d1f5fa40d1ed83fe6eec to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
log "github.com/sirupsen/logrus"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
func main() {
log.Info("starting")
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
var wg sync.WaitGroup
c := make(chan string)
go func() {
wg.Add(1)
defer wg.Done()
done := false
for !done {
select {
case <-ctx.Done():
log.Infof("exiting for loop")
done = true
break
case m := <-c:
log.Infof("msg: %s", m)
}
}
log.Infof("gracefully shutting down...")
time.Sleep(1 * time.Second)
log.Infof("gracefully shutdown complete")
}()
go func() {
for {
time.Sleep(1 * time.Second)
c <- fmt.Sprintf("The date time is: %s", time.Now().Format(time.RFC850))
}
}()
<-ctx.Done()
log.Infof("Recieved shutdown signal, waiting for sync groups")
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment