fixed: graceful shutdown (#3083)

* fixed: graceful shutdown
* updated test for cron scheduler shutdown
This commit is contained in:
Marko Gaćeša 2021-05-27 14:52:09 +02:00 committed by GitHub
parent f274fe6704
commit ea6566b059
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 21 deletions

View file

@ -539,7 +539,7 @@ func (r *Runner) start(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return ctx.Err()
return nil
default:
// This error is ignored on purpose. The system
// should not exit the runner on error. The run

View file

@ -20,6 +20,7 @@ import (
"net/http"
"os"
"path/filepath"
"time"
"golang.org/x/crypto/acme/autocert"
"golang.org/x/sync/errgroup"
@ -43,7 +44,11 @@ func (s Server) ListenAndServe(ctx context.Context) error {
} else if s.Key != "" {
return s.listenAndServeTLS(ctx)
}
return s.listenAndServe(ctx)
err := s.listenAndServe(ctx)
if err == http.ErrServerClosed {
err = nil
}
return err
}
func (s Server) listenAndServe(ctx context.Context) error {
@ -53,10 +58,12 @@ func (s Server) listenAndServe(ctx context.Context) error {
Handler: s.Handler,
}
g.Go(func() error {
select {
case <-ctx.Done():
return s1.Shutdown(ctx)
}
<-ctx.Done()
ctxShutdown, cancelFunc := context.WithTimeout(context.Background(), time.Minute)
defer cancelFunc()
return s1.Shutdown(ctxShutdown)
})
g.Go(func() error {
return s1.ListenAndServe()
@ -84,12 +91,20 @@ func (s Server) listenAndServeTLS(ctx context.Context) error {
)
})
g.Go(func() error {
select {
case <-ctx.Done():
s1.Shutdown(ctx)
s2.Shutdown(ctx)
return nil
}
<-ctx.Done()
var gShutdown errgroup.Group
ctxShutdown, cancelFunc := context.WithTimeout(context.Background(), time.Minute)
defer cancelFunc()
gShutdown.Go(func() error {
return s1.Shutdown(ctxShutdown)
})
gShutdown.Go(func() error {
return s2.Shutdown(ctxShutdown)
})
return gShutdown.Wait()
})
return g.Wait()
}
@ -124,12 +139,20 @@ func (s Server) listenAndServeAcme(ctx context.Context) error {
return s2.ListenAndServeTLS("", "")
})
g.Go(func() error {
select {
case <-ctx.Done():
s1.Shutdown(ctx)
s2.Shutdown(ctx)
return nil
}
<-ctx.Done()
var gShutdown errgroup.Group
ctxShutdown, cancelFunc := context.WithTimeout(context.Background(), time.Minute)
defer cancelFunc()
gShutdown.Go(func() error {
return s1.Shutdown(ctxShutdown)
})
gShutdown.Go(func() error {
return s2.Shutdown(ctxShutdown)
})
return gShutdown.Wait()
})
return g.Wait()
}

View file

@ -69,7 +69,7 @@ func (r *Reaper) Start(ctx context.Context, dur time.Duration) error {
for {
select {
case <-ctx.Done():
return ctx.Err()
return nil
case <-ticker.C:
r.reap(ctx)
}

View file

@ -52,7 +52,7 @@ func (s *Scheduler) Start(ctx context.Context, dur time.Duration) error {
for {
select {
case <-ctx.Done():
return ctx.Err()
return nil
case <-ticker.C:
s.run(ctx)
}

View file

@ -88,7 +88,7 @@ func TestCron_Cancel(t *testing.T) {
s := new(Scheduler)
err := s.Start(ctx, time.Minute)
if err != context.Canceled {
if err != nil {
t.Errorf("Expect cron scheduler exits when context is canceled")
}
}