func()

in internal/sourcemap/metadata_fetcher.go [138:189]


func (s *MetadataESFetcher) sync(ctx context.Context) error {
	tx := s.tracer.StartTransaction("MetadataESFetcher.sync", "")
	defer tx.End()
	ctx = apm.ContextWithTransaction(ctx, tx)

	sourcemaps := make(map[identifier]string)

	result, err := s.initialSearch(ctx, sourcemaps)
	if err != nil {
		if e := apm.CaptureError(ctx, err); e != nil {
			e.Send()
		}

		return err
	}

	scrollID := result.ScrollID

	if scrollID == "" {
		s.update(ctx, sourcemaps)
		return nil
	}

	for {
		result, err = s.scrollsearch(ctx, scrollID, sourcemaps)
		if err != nil {
			s.clearScroll(ctx, scrollID)
			if e := apm.CaptureError(ctx, err); e != nil {
				e.Send()
			}
			return fmt.Errorf("failed scroll search: %w", err)
		}

		// From the docs: The initial search request and each subsequent scroll
		// request each return a _scroll_id. While the _scroll_id may change between
		// requests, it doesn't always change - in any case, only the most recently
		// received _scroll_id should be used.
		if result.ScrollID != "" {
			scrollID = result.ScrollID
		}

		// Stop if there are no new updates
		if len(result.Hits.Hits) == 0 {
			break
		}
	}

	s.clearScroll(ctx, scrollID)

	s.update(ctx, sourcemaps)
	return nil
}