2025-02-03 17:30:50 +00:00
|
|
|
package docinitrunner
|
2024-12-18 18:54:48 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2025-02-06 15:00:26 +00:00
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log/slog"
|
2025-03-05 12:05:46 +00:00
|
|
|
|
2025-02-07 12:12:51 +00:00
|
|
|
"queryorchestration/internal/document"
|
2025-02-03 17:30:50 +00:00
|
|
|
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"
|
|
|
|
|
)
|
|
|
|
|
|
2025-02-03 17:30:50 +00:00
|
|
|
const Name = "docInitRunner"
|
|
|
|
|
|
2025-01-24 16:12:25 +00:00
|
|
|
type Services struct {
|
2025-02-03 17:30:50 +00:00
|
|
|
Document *documentinit.Service
|
2025-01-24 16:12:25 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-03 17:30:50 +00:00
|
|
|
type Runner struct {
|
2024-12-23 16:49:53 +00:00
|
|
|
validator *validator.Validate
|
2025-01-24 16:12:25 +00:00
|
|
|
svc *Services
|
2024-12-18 18:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-03 17:30:50 +00:00
|
|
|
func New(validator *validator.Validate, svc *Services) Runner {
|
|
|
|
|
return Runner{
|
2024-12-23 16:49:53 +00:00
|
|
|
validator: validator,
|
2025-01-24 16:12:25 +00:00
|
|
|
svc: svc,
|
2024-12-18 18:54:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 12:12:51 +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"`
|
|
|
|
|
EventName string `json:"eventName"`
|
|
|
|
|
S3 S3EventRecordDetails `json:"s3"`
|
|
|
|
|
}
|
2025-02-06 15:00:26 +00:00
|
|
|
type S3EventNotification struct {
|
2025-02-07 12:12:51 +00:00
|
|
|
Records []S3EventRecord `json:"Records"`
|
2025-02-06 15:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-03 17:30:50 +00:00
|
|
|
func (s Runner) Process(ctx context.Context, req *types.Message) error {
|
2025-02-06 15:00:26 +00:00
|
|
|
var body S3EventNotification
|
2025-01-20 13:51:22 +00:00
|
|
|
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
|
|
|
|
2025-02-06 15:00:26 +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{
|
2025-02-07 12:12:51 +00:00
|
|
|
JobID: jobID,
|
|
|
|
|
Location: document.Location{
|
|
|
|
|
Bucket: record.S3.Bucket.Name,
|
|
|
|
|
Key: record.S3.Object.Key,
|
|
|
|
|
},
|
|
|
|
|
Hash: record.S3.Object.ETag,
|
2025-02-06 15:00:26 +00:00
|
|
|
})
|
|
|
|
|
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
|
|
|
|
|
}
|