feat(scraper): bound article enrichment diagnostics (#40)
This commit is contained in:
100
backend/cmd/ingestion-bench/main.go
Normal file
100
backend/cmd/ingestion-bench/main.go
Normal file
@ -0,0 +1,100 @@
|
||||
// ingestion-bench deliberately imports ingestion only, not application startup,
|
||||
// database, generation, ranking or synthesis composition.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/tradarr/backend/internal/ingestionbench"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"time"
|
||||
)
|
||||
|
||||
func run(args []string) int {
|
||||
flags := flag.NewFlagSet("ingestion-bench", flag.ContinueOnError)
|
||||
flags.SetOutput(io.Discard)
|
||||
mode := flags.String("mode", "offline", "")
|
||||
allow := flags.Bool("allow-live", false, "")
|
||||
limit := flags.Int("limit", 3, "")
|
||||
script := flags.String("fixture-script", "scraper-service/bench/fixtures-server.js", "")
|
||||
if flags.Parse(args) != nil || flags.NArg() != 0 || *limit < 1 || *limit > 3 || ingestionbench.ValidateMode(*mode, *allow) != nil {
|
||||
fmt.Fprintln(os.Stderr, "invalid_bench_options")
|
||||
return 2
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
|
||||
defer cancel()
|
||||
var articleClient *http.Client
|
||||
var rpcClient *http.Client
|
||||
scraperURL, username, password, symbol := "", "offline-fixture", "offline-fixture", "FIXTURE"
|
||||
if *mode == "offline" {
|
||||
var cleanup func()
|
||||
var err error
|
||||
scraperURL, cleanup, err = ingestionbench.StartOfflineScraper(ctx, *script)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "fixture_service_unavailable")
|
||||
return 1
|
||||
}
|
||||
defer cleanup()
|
||||
rpcClient, err = ingestionbench.OfflineRPCClient(scraperURL)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "fixture_service_unavailable")
|
||||
return 1
|
||||
}
|
||||
articleClient = &http.Client{Transport: ingestionbench.OfflineTransport{}}
|
||||
} else {
|
||||
var err error
|
||||
scraperURL, err = ingestionbench.TrustedScraperURL(os.Getenv("INGESTION_SCRAPER_URL"))
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "invalid_scraper_service")
|
||||
return 2
|
||||
}
|
||||
username, err = ingestionbench.ReadCredentialFile(os.Getenv("BLOOMBERG_USERNAME_FILE"))
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "credentials_unavailable")
|
||||
return 2
|
||||
}
|
||||
password, err = ingestionbench.ReadCredentialFile(os.Getenv("BLOOMBERG_PASSWORD_FILE"))
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "credentials_unavailable")
|
||||
return 2
|
||||
}
|
||||
symbol = os.Getenv("INGESTION_YAHOO_SYMBOL")
|
||||
if symbol == "" {
|
||||
symbol = "AAPL"
|
||||
}
|
||||
if !regexp.MustCompile(`^[A-Za-z0-9.^= -]{1,64}$`).MatchString(symbol) {
|
||||
fmt.Fprintln(os.Stderr, "invalid_bench_symbol")
|
||||
return 2
|
||||
}
|
||||
articleClient = ingestionbench.LiveArticleClient()
|
||||
rpcClient = ingestionbench.RPCClient()
|
||||
}
|
||||
sources := ingestionbench.ProductionSources(articleClient, rpcClient, scraperURL, username, password, symbol)
|
||||
rows, err := ingestionbench.Run(ctx, sources, *limit)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "bench_unavailable")
|
||||
return 1
|
||||
}
|
||||
if json.NewEncoder(os.Stdout).Encode(struct {
|
||||
Mode string `json:"mode"`
|
||||
Sources []ingestionbench.Row `json:"sources"`
|
||||
}{*mode, rows}) != nil {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
func execute(args []string) (code int) {
|
||||
defer func() {
|
||||
if recover() != nil {
|
||||
fmt.Fprintln(os.Stderr, "bench_unavailable")
|
||||
code = 1
|
||||
}
|
||||
}()
|
||||
return run(args)
|
||||
}
|
||||
func main() { os.Exit(execute(os.Args[1:])) }
|
||||
46
backend/cmd/ingestion-bench/main_test.go
Normal file
46
backend/cmd/ingestion-bench/main_test.go
Normal file
@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestInvalidOrUnconfirmedLiveModeNeverStartsIngestion(t *testing.T) {
|
||||
for _, args := range [][]string{{"--mode=live"}, {"--mode=offline", "--allow-live"}, {"--limit=0"}, {"--limit=4"}, {"--mode=unknown"}} {
|
||||
if run(args) != 2 {
|
||||
t.Fatal("invalid configuration accepted")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidScraperEndpointIsRejectedBeforeCredentialReadAndAnyPOST(t *testing.T) {
|
||||
var posted atomic.Int64
|
||||
trap := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { posted.Add(1); w.WriteHeader(204) }))
|
||||
defer trap.Close()
|
||||
// Nonexistent fixture files make an accidental credential read observable as
|
||||
// credentials_unavailable, without reading any real credential or going live.
|
||||
for _, endpoint := range []string{trap.URL, "http://ingestion-scraper:22", "http://ingestion-scraper:8080", "http://127.0.0.1:3001"} {
|
||||
t.Setenv("INGESTION_SCRAPER_URL", endpoint)
|
||||
t.Setenv("BLOOMBERG_USERNAME_FILE", filepath.Join(t.TempDir(), "nonexistent-username"))
|
||||
t.Setenv("BLOOMBERG_PASSWORD_FILE", filepath.Join(t.TempDir(), "nonexistent-password"))
|
||||
previous := os.Stderr
|
||||
r, w, err := os.Pipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
os.Stderr = w
|
||||
code := run([]string{"--mode=live", "--allow-live"})
|
||||
os.Stderr = previous
|
||||
w.Close()
|
||||
message, _ := io.ReadAll(r)
|
||||
r.Close()
|
||||
if code != 2 || string(message) != "invalid_scraper_service\n" || posted.Load() != 0 {
|
||||
t.Fatal("invalid service reached credential read or POST")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user