Catch sigterm & add shutdown logging
This commit changes the signal handler for `replicate` to catch `syscall.SIGTERM` for non-Windows installations. It also adds some logging to indicat when a shutdown has been initiated and when it has finished.
This commit is contained in:
@@ -79,8 +79,7 @@ func (m *Main) Run(ctx context.Context, args []string) (err error) {
|
|||||||
|
|
||||||
// Setup signal handler.
|
// Setup signal handler.
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
ch := make(chan os.Signal, 1)
|
ch := signalChan()
|
||||||
signal.Notify(ch, os.Interrupt)
|
|
||||||
go func() { <-ch; cancel() }()
|
go func() { <-ch; cancel() }()
|
||||||
|
|
||||||
if err := c.Run(ctx); err != nil {
|
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.
|
// Wait for signal to stop program.
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
signal.Reset()
|
signal.Reset()
|
||||||
|
fmt.Println("signal received, litestream shutting down")
|
||||||
|
|
||||||
// Gracefully close.
|
// Gracefully close.
|
||||||
return c.Close()
|
if err := c.Close(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Println("litestream shut down")
|
||||||
|
return nil
|
||||||
|
|
||||||
case "restore":
|
case "restore":
|
||||||
return (&RestoreCommand{}).Run(ctx, args)
|
return (&RestoreCommand{}).Run(ctx, args)
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultConfigPath = "/etc/litestream.yml"
|
const defaultConfigPath = "/etc/litestream.yml"
|
||||||
@@ -15,3 +18,9 @@ func isWindowsService() (bool, error) {
|
|||||||
func runWindowsService(ctx context.Context) error {
|
func runWindowsService(ctx context.Context) error {
|
||||||
panic("cannot run windows service as unix process")
|
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"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
|
||||||
"golang.org/x/sys/windows"
|
"golang.org/x/sys/windows"
|
||||||
"golang.org/x/sys/windows/svc"
|
"golang.org/x/sys/windows/svc"
|
||||||
@@ -103,3 +104,9 @@ func (w *eventlogWriter) Write(p []byte) (n int, err error) {
|
|||||||
elog := (*eventlog.Log)(w)
|
elog := (*eventlog.Log)(w)
|
||||||
return 0, elog.Info(1, string(p))
|
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