Make sure that HugoSites is always closed when done

Including all the integration tests.
This commit is contained in:
Bjørn Erik Pedersen 2024-10-20 11:25:16 +02:00
parent d37606d2c2
commit 352be5ba87
4 changed files with 32 additions and 4 deletions

View file

@ -42,6 +42,7 @@ import (
"github.com/gohugoio/hugo/common/hugo" "github.com/gohugoio/hugo/common/hugo"
"github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/common/paths" "github.com/gohugoio/hugo/common/paths"
"github.com/gohugoio/hugo/common/types"
"github.com/gohugoio/hugo/config" "github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/config/allconfig" "github.com/gohugoio/hugo/config/allconfig"
"github.com/gohugoio/hugo/deps" "github.com/gohugoio/hugo/deps"
@ -66,6 +67,12 @@ func Execute(args []string) error {
} }
args = mapLegacyArgs(args) args = mapLegacyArgs(args)
cd, err := x.Execute(context.Background(), args) cd, err := x.Execute(context.Background(), args)
if cd != nil {
if closer, ok := cd.Root.Command.(types.Closer); ok {
closer.Close()
}
}
if err != nil { if err != nil {
if err == errHelp { if err == errHelp {
cd.CobraCommand.Help() cd.CobraCommand.Help()
@ -149,6 +156,18 @@ func (r *rootCommand) isVerbose() bool {
return r.logger.Level() <= logg.LevelInfo return r.logger.Level() <= logg.LevelInfo
} }
func (r *rootCommand) Close() error {
if r.hugoSites != nil {
r.hugoSites.DeleteFunc(func(key configKey, value *hugolib.HugoSites) bool {
if value != nil {
value.Close()
}
return false
})
}
return nil
}
func (r *rootCommand) Build(cd *simplecobra.Commandeer, bcfg hugolib.BuildCfg, cfg config.Provider) (*hugolib.HugoSites, error) { func (r *rootCommand) Build(cd *simplecobra.Commandeer, bcfg hugolib.BuildCfg, cfg config.Provider) (*hugolib.HugoSites, error) {
h, err := r.Hugo(cfg) h, err := r.Hugo(cfg)
if err != nil { if err != nil {

View file

@ -1012,10 +1012,6 @@ func (c *serverCommand) serve() error {
c.r.Println("Error:", err) c.r.Println("Error:", err)
} }
if h := c.hugoTry(); h != nil {
h.Close()
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel() defer cancel()
wg2, ctx := errgroup.WithContext(ctx) wg2, ctx := errgroup.WithContext(ctx)

7
deps/deps.go vendored
View file

@ -98,6 +98,8 @@ type Deps struct {
// TODO(bep) rethink this re. a plugin setup, but this will have to do for now. // TODO(bep) rethink this re. a plugin setup, but this will have to do for now.
WasmDispatchers *warpc.Dispatchers WasmDispatchers *warpc.Dispatchers
isClosed bool
*globalErrHandler *globalErrHandler
} }
@ -345,6 +347,11 @@ func (d *Deps) TextTmpl() tpl.TemplateParseFinder {
} }
func (d *Deps) Close() error { func (d *Deps) Close() error {
if d.isClosed {
return nil
}
d.isClosed = true
if d.MemCache != nil { if d.MemCache != nil {
d.MemCache.Stop() d.MemCache.Stop()
} }

View file

@ -421,6 +421,12 @@ func (s *IntegrationTestBuilder) Build() *IntegrationTestBuilder {
s.Assert(err, qt.IsNil) s.Assert(err, qt.IsNil)
} }
s.Cleanup(func() {
if h := s.H; h != nil {
s.Assert(h.Close(), qt.IsNil)
}
})
return s return s
} }