droboticflow/internal/store/object_sync_workflows.go

175 lines
7.1 KiB
Go

package store
import (
"context"
"droboticflow/internal/domain"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
"sort"
"strings"
"time"
)
func (s *MemoryStore) GetObjectSyncReconcile(_ context.Context, id string) (*domain.ObjectSyncReconcileRecord, error) {
s.mu.Lock()
defer s.mu.Unlock()
v, ok := s.objectSyncReconciles[id]
if !ok {
return nil, nil
}
return &v, nil
}
func (s *MongoStore) GetObjectSyncReconcile(ctx context.Context, id string) (*domain.ObjectSyncReconcileRecord, error) {
return mongoObjectSyncGet[domain.ObjectSyncReconcileRecord](ctx, s.objectSyncReconciles, bson.M{"id": id})
}
func (s *MemoryStore) GetObjectSyncRelease(_ context.Context, id string) (*domain.ObjectSyncReleaseRecord, error) {
s.mu.Lock()
defer s.mu.Unlock()
v, ok := s.objectSyncReleases[id]
if !ok {
return nil, nil
}
return &v, nil
}
func (s *MongoStore) GetObjectSyncRelease(ctx context.Context, id string) (*domain.ObjectSyncReleaseRecord, error) {
return mongoObjectSyncGet[domain.ObjectSyncReleaseRecord](ctx, s.objectSyncReleases, bson.M{"id": id})
}
func (s *MemoryStore) ListObjectSyncReconciles(_ context.Context, q domain.ObjectSyncListQuery) (domain.ObjectSyncReconcilePage, error) {
s.mu.Lock()
defer s.mu.Unlock()
rows := make([]domain.ObjectSyncReconcileRecord, 0)
after := decodeObjectSyncCursor(q.Cursor)
for _, v := range s.objectSyncReconciles {
if v.OrganizationID == normalizedOrganizationID(q.OrganizationID) && (after == "" || objectSyncTaskCursor(v.CreatedAt, v.ID) < after) && (q.Status == "" || v.Status == q.Status) {
rows = append(rows, v)
}
}
sort.Slice(rows, func(i, j int) bool {
return objectSyncTaskCursor(rows[i].CreatedAt, rows[i].ID) > objectSyncTaskCursor(rows[j].CreatedAt, rows[j].ID)
})
limit := objectSyncLimit(q.Limit)
p := domain.ObjectSyncReconcilePage{Items: rows}
if len(rows) > limit {
p.Items = rows[:limit]
p.NextCursor = encodeObjectSyncCursor(objectSyncTaskCursor(p.Items[limit-1].CreatedAt, p.Items[limit-1].ID))
}
return p, nil
}
func (s *MongoStore) ListObjectSyncReconciles(ctx context.Context, q domain.ObjectSyncListQuery) (domain.ObjectSyncReconcilePage, error) {
filter := bson.M{"organizationId": normalizedOrganizationID(q.OrganizationID)}
if c := decodeObjectSyncCursor(q.Cursor); c != "" {
created, id, ok := strings.Cut(c, "\x00")
if !ok {
return domain.ObjectSyncReconcilePage{}, fmt.Errorf("invalid task cursor")
}
filter["$or"] = bson.A{bson.M{"createdAt": bson.M{"$lt": created}}, bson.M{"createdAt": created, "id": bson.M{"$lt": id}}}
}
if q.Status != "" {
filter["status"] = q.Status
}
limit := objectSyncLimit(q.Limit)
cursor, err := s.objectSyncReconciles.Find(ctx, filter, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}, {Key: "id", Value: -1}}).SetLimit(int64(limit+1)))
if err != nil {
return domain.ObjectSyncReconcilePage{}, err
}
rows, err := decodeMany[domain.ObjectSyncReconcileRecord](ctx, cursor)
p := domain.ObjectSyncReconcilePage{Items: rows}
if len(rows) > limit {
p.Items = rows[:limit]
p.NextCursor = encodeObjectSyncCursor(objectSyncTaskCursor(p.Items[limit-1].CreatedAt, p.Items[limit-1].ID))
}
return p, err
}
func (s *MemoryStore) ListObjectSyncReleasesPage(_ context.Context, q domain.ObjectSyncListQuery) (domain.ObjectSyncReleasePage, error) {
s.mu.Lock()
defer s.mu.Unlock()
rows := make([]domain.ObjectSyncReleaseRecord, 0)
after := decodeObjectSyncCursor(q.Cursor)
for _, v := range s.objectSyncReleases {
if v.OrganizationID == normalizedOrganizationID(q.OrganizationID) && (after == "" || objectSyncTaskCursor(v.CreatedAt, v.ID) < after) && (q.Status == "" || v.Status == q.Status) {
rows = append(rows, v)
}
}
sort.Slice(rows, func(i, j int) bool {
return objectSyncTaskCursor(rows[i].CreatedAt, rows[i].ID) > objectSyncTaskCursor(rows[j].CreatedAt, rows[j].ID)
})
limit := objectSyncLimit(q.Limit)
p := domain.ObjectSyncReleasePage{Items: rows}
if len(rows) > limit {
p.Items = rows[:limit]
p.NextCursor = encodeObjectSyncCursor(objectSyncTaskCursor(p.Items[limit-1].CreatedAt, p.Items[limit-1].ID))
}
return p, nil
}
func (s *MongoStore) ListObjectSyncReleasesPage(ctx context.Context, q domain.ObjectSyncListQuery) (domain.ObjectSyncReleasePage, error) {
filter := bson.M{"organizationId": normalizedOrganizationID(q.OrganizationID)}
if c := decodeObjectSyncCursor(q.Cursor); c != "" {
created, id, ok := strings.Cut(c, "\x00")
if !ok {
return domain.ObjectSyncReleasePage{}, fmt.Errorf("invalid task cursor")
}
filter["$or"] = bson.A{bson.M{"createdAt": bson.M{"$lt": created}}, bson.M{"createdAt": created, "id": bson.M{"$lt": id}}}
}
if q.Status != "" {
filter["status"] = q.Status
}
limit := objectSyncLimit(q.Limit)
cursor, err := s.objectSyncReleases.Find(ctx, filter, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}, {Key: "id", Value: -1}}).SetLimit(int64(limit+1)))
if err != nil {
return domain.ObjectSyncReleasePage{}, err
}
rows, err := decodeMany[domain.ObjectSyncReleaseRecord](ctx, cursor)
p := domain.ObjectSyncReleasePage{Items: rows}
if len(rows) > limit {
p.Items = rows[:limit]
p.NextCursor = encodeObjectSyncCursor(objectSyncTaskCursor(p.Items[limit-1].CreatedAt, p.Items[limit-1].ID))
}
return p, err
}
func (s *MemoryStore) RetryObjectSyncReconcile(_ context.Context, org, id string) (bool, error) {
s.mu.Lock()
defer s.mu.Unlock()
v, ok := s.objectSyncReconciles[id]
if !ok || v.OrganizationID != normalizedOrganizationID(org) || v.Status != "failed" {
return false, nil
}
v.Status = "pending"
v.Error = ""
v.UpdatedAt = nowISO()
v.CompletedAt = ""
v.Scanned = 0
v.Drifted = 0
v.CoordinatorOwner = ""
v.CoordinatorLeaseUntil = time.Time{}
s.objectSyncReconciles[id] = v
return true, nil
}
func (s *MongoStore) RetryObjectSyncReconcile(ctx context.Context, org, id string) (bool, error) {
r, e := s.objectSyncReconciles.UpdateOne(ctx, bson.M{"id": id, "organizationId": normalizedOrganizationID(org), "status": "failed"}, bson.M{"$set": bson.M{"status": "pending", "error": "", "updatedAt": nowISO(), "completedAt": "", "scanned": 0, "drifted": 0}, "$unset": bson.M{"coordinatorOwner": "", "coordinatorLeaseUntil": ""}})
return e == nil && r.MatchedCount == 1, e
}
func (s *MemoryStore) RetryObjectSyncRelease(_ context.Context, org, id string) (bool, error) {
s.mu.Lock()
defer s.mu.Unlock()
v, ok := s.objectSyncReleases[id]
if !ok || v.OrganizationID != normalizedOrganizationID(org) || v.Status != "failed" {
return false, nil
}
v.Status = "pending"
v.Error = ""
v.UpdatedAt = nowISO()
v.CoordinatorOwner = ""
v.CoordinatorLeaseUntil = time.Time{}
s.objectSyncReleases[id] = v
return true, nil
}
func (s *MongoStore) RetryObjectSyncRelease(ctx context.Context, org, id string) (bool, error) {
r, e := s.objectSyncReleases.UpdateOne(ctx, bson.M{"id": id, "organizationId": normalizedOrganizationID(org), "status": "failed"}, bson.M{"$set": bson.M{"status": "pending", "error": "", "updatedAt": nowISO()}, "$unset": bson.M{"coordinatorOwner": "", "coordinatorLeaseUntil": ""}})
return e == nil && r.MatchedCount == 1, e
}
func objectSyncTaskCursor(created, id string) string { return created + "\x00" + id }