fixed: graceful shutdown (#3083)

* fixed: graceful shutdown
* updated test for cron scheduler shutdown
pull/3084/head
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 { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return ctx.Err() return nil
default: default:
// This error is ignored on purpose. The system // This error is ignored on purpose. The system
// should not exit the runner on error. The run // should not exit the runner on error. The run

View File

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

View File

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

View File

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

View File

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