2022-04-26 17:57:01 +00:00
|
|
|
package gracefulshutdown
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-04-27 14:28:42 +00:00
|
|
|
"fmt"
|
2022-04-26 17:57:01 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/vulcanize/ipld-ethcl-indexer/pkg/loghelper"
|
|
|
|
)
|
|
|
|
|
|
|
|
// operation is a clean up function on shutting down
|
|
|
|
type Operation func(ctx context.Context) error
|
|
|
|
|
2022-04-28 18:50:29 +00:00
|
|
|
var (
|
|
|
|
TimeoutErr = func(timeout string) error {
|
|
|
|
return fmt.Errorf("The Timeout %s, has been elapsed, the application will forcefully exit", timeout)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-04-26 17:57:01 +00:00
|
|
|
// gracefulShutdown waits for termination syscalls and doing clean up operations after received it
|
2022-04-27 14:28:42 +00:00
|
|
|
func Shutdown(ctx context.Context, timeout time.Duration, ops map[string]Operation) (<-chan struct{}, <-chan error) {
|
|
|
|
waitCh := make(chan struct{})
|
|
|
|
errCh := make(chan error)
|
2022-04-26 17:57:01 +00:00
|
|
|
go func() {
|
|
|
|
s := make(chan os.Signal, 1)
|
|
|
|
|
|
|
|
// add any other syscalls that you want to be notified with
|
|
|
|
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
|
|
|
|
<-s
|
|
|
|
|
|
|
|
log.Info("Shutting Down your application")
|
|
|
|
|
|
|
|
// set timeout for the ops to be done to prevent system hang
|
|
|
|
timeoutFunc := time.AfterFunc(timeout, func() {
|
2022-04-28 18:50:29 +00:00
|
|
|
log.Warnf(TimeoutErr(timeout.String()).Error())
|
|
|
|
errCh <- TimeoutErr(timeout.String())
|
2022-04-27 14:28:42 +00:00
|
|
|
return
|
2022-04-26 17:57:01 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
defer timeoutFunc.Stop()
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
// Do the operations asynchronously to save time
|
|
|
|
for key, op := range ops {
|
|
|
|
wg.Add(1)
|
|
|
|
innerOp := op
|
|
|
|
innerKey := key
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
log.Infof("cleaning up: %s", innerKey)
|
|
|
|
if err := innerOp(ctx); err != nil {
|
|
|
|
loghelper.LogError(err).Errorf("%s: clean up failed: %s", innerKey, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("%s was shutdown gracefully", innerKey)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
2022-04-27 14:28:42 +00:00
|
|
|
close(waitCh)
|
2022-04-26 17:57:01 +00:00
|
|
|
}()
|
|
|
|
|
2022-04-27 14:28:42 +00:00
|
|
|
return waitCh, errCh
|
2022-04-26 17:57:01 +00:00
|
|
|
}
|