func Run()

in internal/mode/indexer/indexer.go [128:225]


func Run(opts *Options, version, buildTime string) error {
	// Handle version flag if set
	if opts.PrintVersion {
		fmt.Printf("%s %s (built at: %s)\n", os.Args[0], version, buildTime) // nolint:forbidigo
		return nil
	}

	middleware_logger.SetUpLogger()

	var callbackAPIInstance callback.CallbackAPI

	// Tune GOMAXPROCS to match Linux container CPU quota.
	_, _ = maxprocs.Set()

	indexingLock := indexing_lock.NewIndexingLock()

	ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
	defer cancel()

	g, ctx := errgroup.WithContext(ctx)

	s := &server.IndexServer{
		PathPrefix: opts.PathPrefix,
		IndexBuilder: server.DefaultIndexBuilder{
			IndexDir: opts.IndexDir,
		},
		IndexingLock: indexingLock,
	}

	if opts.GitlabURL != "" && opts.SelfURL != "" {
		secret, err := secretreader.ReadSecret(opts.SecretFilePath)
		if err != nil {
			return fmt.Errorf("could not read secret from the file_path: %s %w", opts.SecretFilePath, err)
		}
		callbackAPIInstance, err = callback.NewCallbackAPI(opts.GitlabURL, opts.NodeUUID, secret, &http.Client{})

		if err != nil {
			return err
		}

		s.CallbackAPI = callbackAPIInstance

		concurrency := runtime.GOMAXPROCS(0)
		slog.Info("starting taskRequest", "node_name", opts.NodeName, "index_url", opts.SelfURL, "search_url", opts.SelfURL, "gitlab_url", opts.GitlabURL, "concurrency", concurrency)
		taskRequest, err := task_request.NewTaskRequestTimer(&task_request.NewTaskRequestTimerParams{
			IndexDir:     opts.IndexDir,
			NodeName:     opts.NodeName,
			NodeUUID:     opts.NodeUUID,
			Version:      version,
			SelfURL:      opts.SelfURL,
			SearchURL:    opts.SearchURL,
			GitlabURL:    opts.GitlabURL,
			Secret:       secret,
			Concurrency:  concurrency,
			IndexingLock: indexingLock,
		})
		if err != nil {
			return err
		}

		g.Go(func() error {
			return taskRequest.Start(ctx, s)
		})
	}

	p := profiler.NewProfiler()
	p.Init(serviceName, version)

	httpServer := &http.Server{ //nolint:gosec
		Addr:    opts.Listen,
		Handler: s.Router(),
	}

	g.Go(func() error {
		if err := s.StartIndexingAPI(httpServer); err != nil && err != http.ErrServerClosed { //nolint:errorlint
			return fmt.Errorf("failed to start indexing API: %w", err)
		}

		return nil
	})

	g.Go(func() error {
		return s.StartFileCleaner(ctx)
	})

	<-ctx.Done()

	slog.Info("gracefully shutting down...")

	g.Go(func() error {
		timeoutCtx, timeoutCancel := context.WithTimeout(context.Background(), serverShutdownDelay)
		defer timeoutCancel()

		return httpServer.Shutdown(timeoutCtx)
	})

	return g.Wait()
}