all repos — searchix @ 200857159766d3c6c5831450048348444c5666f4

Search engine for NixOS, nix-darwin, home-manager and NUR users

perf: pass context to importer for better tracing

Alan Pearce
commit

200857159766d3c6c5831450048348444c5666f4

parent

7496bb7e0652d86e2afd05d35e6632dab27c954a

M cmd/searchix-web/main.gocmd/searchix-web/main.go
@@ -53,12 +53,15 @@ }
log.SetLevel(cfg.LogLevel) + ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) + defer cancel() + s, err := searchix.New(cfg, logger) if err != nil { logger.Fatal("Failed to initialise searchix", "error", err) } - err = s.SetupIndex(&searchix.IndexOptions{ + err = s.SetupIndex(ctx, &searchix.IndexOptions{ Update: *update, Replace: *replace, LowMemory: cfg.Importer.LowMemory,
@@ -69,11 +72,8 @@ logger.Fatal("Failed to setup index", "error", err)
} if *replace || *update { - os.Exit(0) + return } - - ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) - defer cancel() go func() { err = s.Start(ctx, *dev)
M go.modgo.mod
@@ -57,6 +57,7 @@ github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mschoch/smat v0.2.0 // indirect github.com/ncruces/go-strftime v0.1.9 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect + github.com/stretchr/testify v1.10.0 // indirect github.com/sykesm/zap-logfmt v0.0.4 // indirect github.com/thessem/zap-prettyconsole v0.5.2 // indirect go.etcd.io/bbolt v1.3.11 // indirect
M go.sumgo.sum
@@ -127,8 +127,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= -github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/sykesm/zap-logfmt v0.0.4 h1:U2WzRvmIWG1wDLCFY3sz8UeEmsdHQjHFNlIdmroVFaI= github.com/sykesm/zap-logfmt v0.0.4/go.mod h1:AuBd9xQjAe3URrWT1BBDk2v2onAZHkZkWRMiYZXiZWA= github.com/tailscale/depaware v0.0.0-20210622194025-720c4b409502/go.mod h1:p9lPsd+cx33L3H9nNoecRRxPssFKUwwI50I3pZ0yT+8=
M gomod2nix.tomlgomod2nix.toml
@@ -139,6 +139,9 @@ hash = "sha256-vYmpyCE37eBYP/navhaLV4oX4/nu0Z/StAocLIFqrmM="
[mod."github.com/stoewer/go-strcase"] version = "v1.3.0" hash = "sha256-X0ilcefeqVQ44B9WT6euCMcigs7oLFypOQaGI33kGr8=" + [mod."github.com/stretchr/testify"] + version = "v1.10.0" + hash = "sha256-fJ4gnPr0vnrOhjQYQwJ3ARDKPsOtA7d4olQmQWR+wpI=" [mod."github.com/sykesm/zap-logfmt"] version = "v0.0.4" hash = "sha256-KXVFtOU54chusK8AhZrzrvbbNmzq1mNrhs/7OmO+huE="
M internal/importer/main.gointernal/importer/main.go
@@ -144,6 +144,7 @@ }
} func Start( + ctx context.Context, cfg *config.Config, log *log.Logger, indexer *index.WriteIndex,
@@ -158,7 +159,7 @@ }
log.Debug("starting importer", "timeout", cfg.Importer.Timeout.Duration) importCtx, cancelImport := context.WithTimeout( - context.Background(), + ctx, cfg.Importer.Timeout.Duration, ) defer cancelImport()
M internal/importer/main_test.gointernal/importer/main_test.go
@@ -1,6 +1,7 @@
package importer import ( + "context" "testing" "go.alanpearce.eu/searchix/internal/config"
@@ -21,7 +22,14 @@ if err != nil {
b.Fatal(err) } - err = Start(&cfg, logger.Named("importer"), write, false, &[]string{"nixpkgs"}) + err = Start( + context.Background(), + &cfg, + logger.Named("importer"), + write, + false, + &[]string{"nixpkgs"}, + ) if err != nil { b.Fatal(err) }
M searchix.gosearchix.go
@@ -44,7 +44,7 @@ LowMemory bool
Logger *log.Logger } -func (s *Server) SetupIndex(options *IndexOptions) error { +func (s *Server) SetupIndex(ctx context.Context, options *IndexOptions) error { var i uint cfgEnabledSources := make([]string, len(s.cfg.Importer.Sources)) for key := range s.cfg.Importer.Sources {
@@ -78,6 +78,7 @@ "update",
options.Update, ) err = importer.Start( + ctx, s.cfg, s.log.Named("importer"), write,
@@ -105,7 +106,14 @@ return slices.Contains(cfgEnabledSources, s)
}) if len(newSources) > 0 { s.log.Info("adding new sources", "sources", newSources) - err := importer.Start(s.cfg, options.Logger.Named("importer"), write, false, &newSources) + err := importer.Start( + ctx, + s.cfg, + options.Logger.Named("importer"), + write, + false, + &newSources, + ) if err != nil { return errors.Wrap(err, "Failed to update index with new sources") }
@@ -189,7 +197,7 @@ MonitorSlug: monitorSlug,
Status: sentry.CheckInStatusInProgress, }, monitorConfig) - err = importer.Start(s.cfg, s.log.Named("importer"), s.writeIndex, false, nil) + err = importer.Start(ctx, s.cfg, s.log.Named("importer"), s.writeIndex, false, nil) s.wg.Done() if err != nil { s.log.Warn("error updating index", "error", err)