2025-03-10 11:03:00 +00:00
|
|
|
package clientsync
|
2025-02-12 19:00:25 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
|
|
|
|
"errors"
|
2025-02-14 10:56:24 +00:00
|
|
|
"log/slog"
|
2025-03-05 12:05:46 +00:00
|
|
|
|
2025-02-12 19:00:25 +00:00
|
|
|
docsyncrunner "queryorchestration/api/docSyncRunner"
|
|
|
|
|
"queryorchestration/internal/database"
|
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
|
|
|
"queryorchestration/internal/serviceconfig/queue"
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (s *Service) Sync(ctx context.Context, id uuid.UUID) error {
|
|
|
|
|
hasMore := true
|
|
|
|
|
offset := int32(0)
|
|
|
|
|
dbid := database.MustToDBUUID(id)
|
|
|
|
|
|
|
|
|
|
for hasMore {
|
2025-03-10 11:03:00 +00:00
|
|
|
ids, err := s.cfg.GetDBQueries().ListDocumentIDsBatch(ctx, &repository.ListDocumentIDsBatchParams{
|
2025-02-12 19:00:25 +00:00
|
|
|
Batchsize: s.batchSize,
|
|
|
|
|
Pageoffset: offset,
|
2025-03-10 11:03:00 +00:00
|
|
|
Clientid: dbid,
|
2025-02-12 19:00:25 +00:00
|
|
|
})
|
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
|
return nil
|
|
|
|
|
} else if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-14 10:56:24 +00:00
|
|
|
slog.Debug("batch processing", "id", id, "offset", offset, "length", len(ids))
|
|
|
|
|
|
2025-02-12 19:00:25 +00:00
|
|
|
for _, id := range ids {
|
|
|
|
|
err := s.cfg.SendToQueue(ctx, &queue.SendParams{
|
|
|
|
|
QueueURL: s.cfg.GetDocumentSyncURL(),
|
|
|
|
|
Body: docsyncrunner.Body{
|
|
|
|
|
ID: database.MustToUUID(id.ID),
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
offset += s.batchSize
|
2025-02-17 17:25:33 +00:00
|
|
|
if len(ids) == 0 || ids[0].Totalcount == nil || int64(offset) >= *ids[0].Totalcount {
|
2025-02-12 19:00:25 +00:00
|
|
|
hasMore = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|