mirror of https://github.com/harness/drone.git
fixed: graceful shutdown (#3083)
* fixed: graceful shutdown * updated test for cron scheduler shutdownpull/3084/head
parent
f274fe6704
commit
ea6566b059
|
@ -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
|
||||||
|
|
|
@ -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()
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue