Files
lincodex/backend/internal/adapter/primary/cli/semanticbackfill/command.go
T

121 lines
4.0 KiB
Go

// Package semanticbackfill implements the operator command that schedules
// vector jobs for existing published candidates and needs, for example after
// semantic matching is enabled, the semantic policy is published, or the ML
// model changes. The jobs run in the backend worker; the command only
// schedules them and prints the resume cursor of every page.
package semanticbackfill
import (
"context"
"errors"
"flag"
"fmt"
"io"
"time"
"github.com/google/uuid"
"lincodex/backend/config"
"lincodex/backend/internal/adapter/secondary/postgres"
"lincodex/backend/internal/usecase/semantic"
)
const usage = "usage: semantic-backfill candidates|needs|all --run <label> [--after <uuid>]"
// Command is a parsed, validated request.
type Command struct {
Scopes []semantic.BackfillScope
Run string
After uuid.UUID
}
// Runner schedules the jobs of one scope.
type Runner interface {
Run(ctx context.Context, scope semantic.BackfillScope, run string, after uuid.UUID, report func(semantic.BackfillProgress) error) error
}
// Parse validates every argument before Run reads configuration or connects.
func Parse(args []string) (Command, error) {
if len(args) == 0 {
return Command{}, errors.New(usage)
}
var scopes []semantic.BackfillScope
switch args[0] {
case string(semantic.BackfillCandidates):
scopes = []semantic.BackfillScope{semantic.BackfillCandidates}
case string(semantic.BackfillNeeds):
scopes = []semantic.BackfillScope{semantic.BackfillNeeds}
case "all":
scopes = []semantic.BackfillScope{semantic.BackfillCandidates, semantic.BackfillNeeds}
default:
return Command{}, errors.New(usage)
}
flags := flag.NewFlagSet("semantic-backfill", flag.ContinueOnError)
flags.SetOutput(io.Discard)
run := flags.String("run", "", "run label; a new label recomputes again")
after := flags.String("after", "", "resume after this identifier")
if err := flags.Parse(args[1:]); err != nil || flags.NArg() != 0 || *run == "" {
return Command{}, errors.New(usage)
}
command := Command{Scopes: scopes, Run: *run}
if *after != "" {
if len(scopes) != 1 {
return Command{}, errors.New("--after needs a single scope")
}
parsed, err := uuid.Parse(*after)
if err != nil {
return Command{}, errors.New("--after must be a UUID")
}
command.After = parsed
}
return command, nil
}
// Run parses the command, loads only DATABASE_URL, and schedules.
func Run(ctx context.Context, args []string, lookup config.LookupEnv, output io.Writer, clock func() time.Time) error {
command, err := Parse(args)
if err != nil {
return err
}
if lookup == nil || output == nil || clock == nil {
return errors.New("semantic backfill command dependencies are required")
}
databaseURL := config.DatabaseURLFromEnv(lookup)
if databaseURL == "" {
return errors.New("DATABASE_URL is not set")
}
pool, err := postgres.NewPool(ctx, databaseURL)
if err != nil {
return errors.New("database configuration is invalid")
}
defer pool.Close()
if err := pool.Ping(ctx); err != nil {
return errors.New("database is unavailable")
}
backfill, err := semantic.NewBackfill(postgres.NewTransactor(pool), clock)
if err != nil {
return err
}
return Execute(ctx, command, backfill, output)
}
// Execute runs every scope and prints one line per page.
func Execute(ctx context.Context, command Command, runner Runner, output io.Writer) error {
for _, scope := range command.Scopes {
total := 0
err := runner.Run(ctx, scope, command.Run, command.After, func(progress semantic.BackfillProgress) error {
total += progress.Scheduled
_, err := fmt.Fprintf(output, "%s: scheduled %d (total %d), resume with --after %s\n",
progress.Scope, progress.Scheduled, total, progress.After)
return err
})
if err != nil {
return errors.New("the backfill stopped; run it again with --after of the last printed line")
}
if _, err := fmt.Fprintf(output, "%s: done, %d jobs scheduled; the backend worker computes them\n", scope, total); err != nil {
return errors.New("could not write the result")
}
}
return nil
}