Files
query-orchestration/api/docCleanRunner/runner.go
T
Jay Brown aafe7d5b5f Merged in feature/batch-status (pull request #215)
Implement and test the batch status feature

* working
2026-03-12 18:53:42 +00:00

73 lines
2.0 KiB
Go

package doccleanrunner
import (
"context"
"log/slog"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/document/batch/outcome"
documentclean "queryorchestration/internal/document/clean"
"github.com/google/uuid"
)
const Name = "docCleanRunner"
// Services holds the dependencies for the docCleanRunner.
//
// Fields:
// - Clean: service for cleaning/validating documents
// - Outcome: reporter for writing batch pipeline outcomes
type Services struct {
Clean *documentclean.Service
Outcome *outcome.Reporter
}
// Runner processes document clean messages from the SQS queue.
type Runner struct {
svc *Services
}
// New creates a new docCleanRunner.
//
// Parameters:
// - svc: the runner's service dependencies
//
// Returns:
// - Runner: the runner instance
func New(svc *Services) Runner {
return Runner{
svc: svc,
}
}
// Body is the SQS message body for docCleanRunner.
type Body struct {
DocumentID uuid.UUID `json:"id" validate:"required,uuid"`
}
// Process handles a single document clean message. Returns true to acknowledge
// the message (success or validation failure), or false to requeue (infrastructure error).
// Validation failures are acknowledged (not requeued) to prevent infinite retry loops
// for permanently-invalid documents.
func (s Runner) Process(ctx context.Context, body Body) bool {
result, err := s.svc.Clean.Clean(ctx, body.DocumentID)
if err != nil {
slog.Error("unable to process", "error", err)
return false // infrastructure error -> requeue
}
outcomeName := repository.BatchOutcomeStatusCleanPassed
var errorDetail *string
if !result.Passed {
outcomeName = repository.BatchOutcomeStatusCleanFailed
errorDetail = result.FailReason
}
if updateErr := s.svc.Outcome.Update(ctx, body.DocumentID, outcomeName, errorDetail); updateErr != nil {
slog.Error("failed to update batch outcome",
"document_id", body.DocumentID, "outcome", outcomeName, "error", updateErr)
}
return true // validation pass or fail -> acknowledge (don't requeue bad PDFs)
}