From b93b2bafa56ee79fa5d70b4c15901981215fa935 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 23 Oct 2020 13:30:29 -0400 Subject: Rig up dowork event queue and graceful termination --- cmd/alps/main.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'cmd') diff --git a/cmd/alps/main.go b/cmd/alps/main.go index 4c358d5..dedd6cb 100644 --- a/cmd/alps/main.go +++ b/cmd/alps/main.go @@ -1,11 +1,13 @@ package main import ( + "context" "flag" "fmt" "os" "os/signal" "syscall" + "time" "git.sr.ht/~emersion/alps" "github.com/fernet/fernet-go" @@ -74,15 +76,29 @@ func main() { e.Logger.SetLevel(log.DEBUG) } + go e.Start(addr) + + s.Queue.Start(context.Background()) + sigs := make(chan os.Signal, 1) - signal.Notify(sigs, syscall.SIGUSR1) - go func() { - for range sigs { + signal.Notify(sigs, syscall.SIGUSR1, syscall.SIGINT) + + for sig := range sigs { + if sig == syscall.SIGUSR1 { if err := s.Reload(); err != nil { e.Logger.Errorf("Failed to reload server: %v", err) } + } else if sig == syscall.SIGINT { + break } - }() + } + + ctx, cancel := context.WithDeadline(context.Background(), + time.Now().Add(30*time.Second)) + e.Shutdown(ctx) + cancel() - e.Logger.Fatal(e.Start(addr)) + e.Logger.Print("Waiting for work queues to finish...") + s.Queue.Shutdown() + e.Logger.Print("Shut down.") } -- cgit v1.2.3