Files
query-orchestration/api/docInitRunner/runner.go
T

105 lines
2.1 KiB
Go
Raw Normal View History

package docinitrunner
2024-12-18 18:54:48 +00:00
import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
documentinit "queryorchestration/internal/document/init"
2024-12-18 18:54:48 +00:00
2024-12-23 16:49:53 +00:00
"github.com/go-playground/validator/v10"
2024-12-18 18:54:48 +00:00
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
)
const Name = "docInitRunner"
type Services struct {
Document *documentinit.Service
}
type Runner struct {
2024-12-23 16:49:53 +00:00
validator *validator.Validate
svc *Services
2024-12-18 18:54:48 +00:00
}
func New(validator *validator.Validate, svc *Services) Runner {
return Runner{
2024-12-23 16:49:53 +00:00
validator: validator,
svc: svc,
2024-12-18 18:54:48 +00:00
}
}
type S3EventNotification struct {
Records []struct {
EventVersion string `json:"eventVersion"`
EventSource string `json:"eventSource"`
AwsRegion string `json:"awsRegion"`
EventTime string `json:"eventTime"`
EventName string `json:"eventName"`
S3 struct {
Bucket struct {
Name string `json:"name"`
Arn string `json:"arn"`
} `json:"bucket"`
Object struct {
Key string `json:"key"`
Size int64 `json:"size"`
ETag string `json:"eTag"`
VersionId string `json:"versionId"`
} `json:"object"`
} `json:"s3"`
} `json:"Records"`
}
func (s Runner) Process(ctx context.Context, req *types.Message) error {
var body S3EventNotification
err := json.Unmarshal([]byte(*req.Body), &body)
2024-12-18 18:54:48 +00:00
if err != nil {
return err
}
2024-12-23 16:49:53 +00:00
err = s.validator.Struct(body)
if err != nil {
return err
}
2024-12-20 17:35:33 +00:00
hasErr := false
for _, record := range body.Records {
err := func() error {
if record.EventName != "ObjectCreated:Put" {
return fmt.Errorf("invalid event name: %s", record.EventName)
}
jobID, err := s.svc.Document.GetJobIDFromKey(record.S3.Object.Key)
if err != nil {
return err
}
_, err = s.svc.Document.Create(ctx, &documentinit.Create{
JobID: jobID,
Location: record.S3.Object.Key,
Bucket: record.S3.Bucket.Name,
Hash: record.S3.Object.ETag,
})
if err != nil {
return err
}
return nil
}()
if err != nil {
hasErr = true
slog.Error("Error processing record", "err", err)
}
}
if hasErr {
return errors.New("error processing records")
2024-12-18 18:54:48 +00:00
}
return nil
}