Merge pull request #133 from benbjohnson/sigterm
Catch sigterm & add shutdown logging
This commit is contained in:
@@ -79,8 +79,7 @@ func (m *Main) Run(ctx context.Context, args []string) (err error) {
|
||||
|
||||
// Setup signal handler.
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
ch := make(chan os.Signal, 1)
|
||||
signal.Notify(ch, os.Interrupt)
|
||||
ch := signalChan()
|
||||
go func() { <-ch; cancel() }()
|
||||
|
||||
if err := c.Run(ctx); err != nil {
|
||||
@@ -90,9 +89,14 @@ func (m *Main) Run(ctx context.Context, args []string) (err error) {
|
||||
// Wait for signal to stop program.
|
||||
<-ctx.Done()
|
||||
signal.Reset()
|
||||
fmt.Println("signal received, litestream shutting down")
|
||||
|
||||
// Gracefully close.
|
||||
return c.Close()
|
||||
if err := c.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("litestream shut down")
|
||||
return nil
|
||||
|
||||
case "restore":
|
||||
return (&RestoreCommand{}).Run(ctx, args)
|
||||
|
||||
@@ -4,6 +4,9 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
const defaultConfigPath = "/etc/litestream.yml"
|
||||
@@ -15,3 +18,9 @@ func isWindowsService() (bool, error) {
|
||||
func runWindowsService(ctx context.Context) error {
|
||||
panic("cannot run windows service as unix process")
|
||||
}
|
||||
|
||||
func signalChan() <-chan os.Signal {
|
||||
ch := make(chan os.Signal, 1)
|
||||
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
|
||||
return ch
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
"golang.org/x/sys/windows/svc"
|
||||
@@ -103,3 +104,9 @@ func (w *eventlogWriter) Write(p []byte) (n int, err error) {
|
||||
elog := (*eventlog.Log)(w)
|
||||
return 0, elog.Info(1, string(p))
|
||||
}
|
||||
|
||||
func signalChan() <-chan os.Signal {
|
||||
ch := make(chan os.Signal, 1)
|
||||
signal.Notify(ch, os.Interrupt)
|
||||
return ch
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user