mirror of
https://github.com/SphericalKat/medium.rip.git
synced 2026-10-21 01:34:22 +00:00
1 parent
6117c82d32
commit
244804fe85
6 files changed
+168
-4
No files matched your search
@@ -0,0 +1,34 @@
|
||||
package lifecycle
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// ShutdownListener listens for shutdown OS signals and then gracefully stops tasks using the context
|
||||
func ShutdownListener(
|
||||
wg *sync.WaitGroup,
|
||||
cf *context.CancelFunc,
|
||||
) {
|
||||
// create channel to notify on system signals
|
||||
termChan := make(chan os.Signal, 1)
|
||||
signal.Notify(termChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
// wait for signal
|
||||
sig := <-termChan
|
||||
log.Printf("Received signal %v, gracefully shutting down services", sig)
|
||||
|
||||
// close channel
|
||||
close(termChan)
|
||||
|
||||
// cancel background context
|
||||
(*cf)()
|
||||
|
||||
// mark shutdown task as done
|
||||
wg.Done()
|
||||
}
|
||||
Reference in new issue
Block a user