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

124 lines
2.7 KiB
Go
Raw Normal View History

package docinitrunner
2024-12-18 18:54:48 +00:00
import (
"context"
"encoding/json"
"log/slog"
2025-03-05 12:05:46 +00:00
"queryorchestration/internal/document"
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 S3Bucket struct {
Name string `json:"name"`
Arn string `json:"arn"`
}
type S3Object struct {
Key string `json:"key"`
Size int64 `json:"size"`
ETag string `json:"eTag"`
VersionId string `json:"versionId"`
}
type S3EventRecordDetails struct {
Bucket S3Bucket `json:"bucket"`
Object S3Object `json:"object"`
}
type S3EventRecord struct {
EventVersion string `json:"eventVersion"`
EventSource string `json:"eventSource"`
AwsRegion string `json:"awsRegion"`
EventTime string `json:"eventTime"`
2025-03-19 11:54:14 +00:00
EventName EventS3 `json:"eventName"`
S3 S3EventRecordDetails `json:"s3"`
}
type S3EventNotification struct {
Records []S3EventRecord `json:"Records"`
}
2025-03-19 11:54:14 +00:00
type EventS3 string
const (
2025-03-19 11:54:14 +00:00
EventS3ObjectCreatedPut = "ObjectCreated:Put"
)
func (s Runner) Process(ctx context.Context, req *types.Message) bool {
var body S3EventNotification
err := json.Unmarshal([]byte(*req.Body), &body)
2024-12-18 18:54:48 +00:00
if err != nil {
slog.Error("parsing body", "messageId", *req.MessageId, "body", *req.Body)
return true
2024-12-18 18:54:48 +00:00
}
2024-12-23 16:49:53 +00:00
err = s.validator.Struct(body)
if err != nil {
slog.Error("invalid body", "messageId", *req.MessageId, "error", err)
return true
2024-12-23 16:49:53 +00:00
}
2024-12-20 17:35:33 +00:00
for _, record := range body.Records {
err := s.processRecord(ctx, record)
if err != nil {
slog.Error("unable to process", "error", err)
return false
}
}
return true
}
func (s Runner) processRecord(ctx context.Context, record S3EventRecord) error {
if !s.isSupportedEvent(record.EventName) {
slog.Warn("unsupported event", "name", record.EventName)
return nil
}
clientID, err := s.svc.Document.GetClientIDFromKey(record.S3.Object.Key)
if err != nil {
slog.Error("unable to find client id", "key", record.S3.Object.Key, "error", err)
return nil
}
_, err = s.svc.Document.Create(ctx, &documentinit.Create{
ClientID: clientID,
Location: document.Location{
Bucket: record.S3.Bucket.Name,
Key: record.S3.Object.Key,
},
Hash: record.S3.Object.ETag,
})
if err != nil {
return err
2024-12-18 18:54:48 +00:00
}
return nil
}
2025-03-19 11:54:14 +00:00
func (s Runner) isSupportedEvent(name EventS3) bool {
return name == EventS3ObjectCreatedPut
}