about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--internal/importer/importer.go4
-rw-r--r--internal/importer/main.go37
-rw-r--r--internal/importer/main_test.go6
-rw-r--r--searchix.go15
4 files changed, 34 insertions, 28 deletions
diff --git a/internal/importer/importer.go b/internal/importer/importer.go
index 99f7e7a..0b2db43 100644
--- a/internal/importer/importer.go
+++ b/internal/importer/importer.go
@@ -9,10 +9,6 @@ import (
 	"go.alanpearce.eu/x/log"
 )
 
-type Importer interface {
-	Import(context.Context, *index.WriteIndex) (bool, error)
-}
-
 type Processor interface {
 	Process(context.Context) (<-chan nix.Importable, <-chan error)
 }
diff --git a/internal/importer/main.go b/internal/importer/main.go
index 2af1666..967c5f6 100644
--- a/internal/importer/main.go
+++ b/internal/importer/main.go
@@ -143,33 +143,48 @@ func createSourceImporter(
 	}
 }
 
-func Start(
-	ctx context.Context,
+type Importer struct {
+	config  *config.Config
+	log     *log.Logger
+	indexer *index.WriteIndex
+}
+
+func New(
 	cfg *config.Config,
 	log *log.Logger,
 	indexer *index.WriteIndex,
+) *Importer {
+	return &Importer{
+		config:  cfg,
+		log:     log,
+		indexer: indexer,
+	}
+}
+
+func (imp *Importer) Start(
+	ctx context.Context,
 	forceUpdate bool,
 	onlyUpdateSources *[]string,
 ) error {
-	if len(cfg.Importer.Sources) == 0 {
-		log.Info("No sources enabled")
+	if len(imp.config.Importer.Sources) == 0 {
+		imp.log.Info("No sources enabled")
 
 		return nil
 	}
 
-	log.Debug("starting importer", "timeout", cfg.Importer.Timeout.Duration)
+	imp.log.Debug("starting importer", "timeout", imp.config.Importer.Timeout.Duration)
 	importCtx, cancelImport := context.WithTimeout(
 		ctx,
-		cfg.Importer.Timeout.Duration,
+		imp.config.Importer.Timeout.Duration,
 	)
 	defer cancelImport()
 
 	forceUpdate = forceUpdate || (onlyUpdateSources != nil && len(*onlyUpdateSources) > 0)
 
-	meta := indexer.Meta
+	meta := imp.indexer.Meta
 
-	importSource := createSourceImporter(importCtx, log, meta, indexer, forceUpdate)
-	for name, source := range cfg.Importer.Sources {
+	importSource := createSourceImporter(importCtx, imp.log, meta, imp.indexer, forceUpdate)
+	for name, source := range imp.config.Importer.Sources {
 		if onlyUpdateSources != nil && len(*onlyUpdateSources) > 0 {
 			if !slices.Contains(*onlyUpdateSources, name) {
 				continue
@@ -177,11 +192,11 @@ func Start(
 		}
 		err := importSource(source)
 		if err != nil {
-			log.Error("import failed", "source", name, "error", err)
+			imp.log.Error("import failed", "source", name, "error", err)
 		}
 	}
 
-	err := indexer.SaveMeta()
+	err := imp.indexer.SaveMeta()
 	if err != nil {
 		return errors.Wrap(err, "failed to save metadata")
 	}
diff --git a/internal/importer/main_test.go b/internal/importer/main_test.go
index 9e19b8c..576d681 100644
--- a/internal/importer/main_test.go
+++ b/internal/importer/main_test.go
@@ -22,11 +22,9 @@ func BenchmarkImporterLowMemory(b *testing.B) {
 		b.Fatal(err)
 	}
 
-	err = Start(
+	imp := New(&cfg, logger.Named("importer"), write)
+	err = imp.Start(
 		context.Background(),
-		&cfg,
-		logger.Named("importer"),
-		write,
 		false,
 		&[]string{"nixpkgs"},
 	)
diff --git a/searchix.go b/searchix.go
index 615afc6..be76af6 100644
--- a/searchix.go
+++ b/searchix.go
@@ -77,11 +77,9 @@ func (s *Server) SetupIndex(ctx context.Context, options *IndexOptions) error {
 			"update",
 			options.Update,
 		)
-		err = importer.Start(
+		imp := importer.New(s.cfg, s.log.Named("importer"), write)
+		err = imp.Start(
 			ctx,
-			s.cfg,
-			s.log.Named("importer"),
-			write,
 			options.Replace || options.Update,
 			nil,
 		)
@@ -106,11 +104,9 @@ func (s *Server) SetupIndex(ctx context.Context, options *IndexOptions) error {
 			})
 			if len(newSources) > 0 {
 				s.log.Info("adding new sources", "sources", newSources)
-				err := importer.Start(
+				imp := importer.New(s.cfg, options.Logger.Named("importer"), write)
+				err := imp.Start(
 					ctx,
-					s.cfg,
-					options.Logger.Named("importer"),
-					write,
 					false,
 					&newSources,
 				)
@@ -197,7 +193,8 @@ func (s *Server) startUpdateTimer(
 				Status:      sentry.CheckInStatusInProgress,
 			}, monitorConfig)
 
-			err = importer.Start(ctx, s.cfg, s.log.Named("importer"), s.writeIndex, false, nil)
+			imp := importer.New(s.cfg, s.log.Named("importer"), s.writeIndex)
+			err = imp.Start(ctx, false, nil)
 			s.wg.Done()
 			if err != nil {
 				s.log.Warn("error updating index", "error", err)