internal/importer/main.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | package importer import ( "context" "fmt" "os/exec" "slices" "strings" "time" "go.alanpearce.eu/searchix/internal/config" "go.alanpearce.eu/searchix/internal/fetcher" "go.alanpearce.eu/searchix/internal/index" "go.alanpearce.eu/searchix/internal/programs" "go.alanpearce.eu/x/log" "github.com/pkg/errors" ) func createSourceImporter( parent context.Context, log *log.Logger, meta *index.Meta, indexer *index.WriteIndex, forceUpdate bool, ) func(*config.Source) error { return func(source *config.Source) error { logger := log.With( "name", source.Key, "fetcher", source.Fetcher.String(), "timeout", source.Timeout.Duration, ) logger.Debug("starting fetcher") fetcher, err := fetcher.New(source, logger) if err != nil { return errors.WithMessage(err, "error creating fetcher") } sourceMeta := meta.GetSourceMeta(source.Key) if forceUpdate { sourceMeta.Updated = time.Time{} } previousUpdate := sourceMeta.Updated ctx, cancel := context.WithTimeout(parent, source.Timeout.Duration) defer cancel() files, err := fetcher.FetchIfNeeded(ctx, sourceMeta) if err != nil { var exerr *exec.ExitError if errors.As(err, &exerr) { lines := strings.Split(strings.TrimSpace(string(exerr.Stderr)), "\n") for _, line := range lines { logger.Error( "importer fetch failed", "stderr", line, "status", exerr.ExitCode(), ) } } return errors.WithMessage(err, "importer fetch failed") } logger.Info( "importer fetch succeeded", "previous", previousUpdate, "current", sourceMeta.Updated, "is_updated", sourceMeta.Updated.After(previousUpdate), "update_force", forceUpdate, ) if sourceMeta.Updated.After(previousUpdate) || forceUpdate { var pdb *programs.DB if source.Programs.Enable { pdb, err = programs.Instantiate(ctx, source, log.Named("programs")) if err != nil { logger.Warn("programs database instantiation failed", "error", err) } if pdb.Path != sourceMeta.ProgramsPath { sourceMeta.ProgramsPath = pdb.Path } } err = setRepoRevision(files.Revision, source) if err != nil { logger.Warn("could not set source repo revision", "error", err) } var processor Processor logger.Debug( "creating processor", "importer_type", source.Importer, "revision", source.Repo.Revision, ) switch source.Importer { case config.Options: logger.Debug("processor created", "file", fmt.Sprintf("%T", files.Options)) processor, err = NewOptionProcessor( files.Options, source, logger.Named("processor"), ) case config.Packages: processor, err = NewPackageProcessor( files.Packages, source, logger.Named("processor"), pdb, ) } if err != nil { return errors.WithMessagef(err, "failed to create processor") } hadWarnings, err := process(ctx, indexer, processor, logger) if err != nil { return errors.WithMessagef(err, "failed to process source") } if hadWarnings { logger.Warn("importer succeeded, but with warnings/errors") } else { logger.Info("importer succeeded") } } sourceMeta.Rev = source.Repo.Revision meta.SetSourceMeta(source.Key, sourceMeta) return nil } } 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(imp.config.Importer.Sources) == 0 { imp.log.Info("No sources enabled") return nil } imp.log.Debug("starting importer", "timeout", imp.config.Importer.Timeout.Duration) importCtx, cancelImport := context.WithTimeout( ctx, imp.config.Importer.Timeout.Duration, ) defer cancelImport() forceUpdate = forceUpdate || (onlyUpdateSources != nil && len(*onlyUpdateSources) > 0) meta := imp.indexer.Meta 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 } } err := importSource(source) if err != nil { imp.log.Error("import failed", "source", name, "error", err) } } err := imp.indexer.SaveMeta() if err != nil { return errors.Wrap(err, "failed to save metadata") } return nil } |