Merged in feature/jobsync (pull request #63)
Document Sync and Job Sync * docsyncfunc * startedQueryWork * morefixes * jobsyncsvcstart * save * jobsynctext * save * save * docsync * shorttest * jobsync * jobsyncupdateoncollectorupdate * passlivetest * collectortest * lint
This commit is contained in:
@@ -41,7 +41,7 @@ func (s *Service) clean(ctx context.Context, id uuid.UUID) error {
|
||||
return err
|
||||
}
|
||||
|
||||
version := s.svc.Document.GetCleanVersion()
|
||||
version := s.svc.Version.GetVersion()
|
||||
|
||||
err = s.cfg.GetDBQueries().AddDocumentCleanEntry(ctx, &repository.AddDocumentCleanEntryParams{
|
||||
Documentid: docId,
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/document"
|
||||
cleanversion "queryorchestration/internal/document/clean/version"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -26,7 +27,7 @@ func TestClean(t *testing.T) {
|
||||
svc := Service{
|
||||
cfg: cfg,
|
||||
svc: &Services{
|
||||
Document: document.New(cfg),
|
||||
Version: cleanversion.New(cfg),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/document"
|
||||
cleanversion "queryorchestration/internal/document/clean/version"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/objectstore"
|
||||
"queryorchestration/internal/serviceconfig/queue/documenttext"
|
||||
queuemock "queryorchestration/mocks/queue"
|
||||
"testing"
|
||||
@@ -22,7 +22,6 @@ import (
|
||||
type DocCleanConfig struct {
|
||||
serviceconfig.BaseConfig
|
||||
documenttext.DocTextConfig
|
||||
objectstore.ObjectStoreConfig
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
@@ -43,7 +42,7 @@ func TestCreate(t *testing.T) {
|
||||
svc := Service{
|
||||
cfg: cfg,
|
||||
svc: &Services{
|
||||
Document: document.New(cfg),
|
||||
Version: cleanversion.New(cfg),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -1,20 +1,18 @@
|
||||
package documentclean
|
||||
|
||||
import (
|
||||
"queryorchestration/internal/document"
|
||||
cleanversion "queryorchestration/internal/document/clean/version"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/objectstore"
|
||||
"queryorchestration/internal/serviceconfig/queue/documenttext"
|
||||
)
|
||||
|
||||
type ConfigProvider interface {
|
||||
serviceconfig.ConfigProvider
|
||||
objectstore.ConfigProvider
|
||||
documenttext.ConfigProvider
|
||||
}
|
||||
|
||||
type Services struct {
|
||||
Document *document.Service
|
||||
Version *cleanversion.Service
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
|
||||
@@ -3,7 +3,6 @@ package documentclean_test
|
||||
import (
|
||||
documentclean "queryorchestration/internal/document/clean"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/objectstore"
|
||||
"queryorchestration/internal/serviceconfig/queue/documenttext"
|
||||
"testing"
|
||||
|
||||
@@ -13,7 +12,6 @@ import (
|
||||
type DocCleanConfig struct {
|
||||
serviceconfig.BaseConfig
|
||||
documenttext.DocTextConfig
|
||||
objectstore.ObjectStoreConfig
|
||||
}
|
||||
|
||||
func TestService(t *testing.T) {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package cleanversion
|
||||
|
||||
import (
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
cfg serviceconfig.ConfigProvider
|
||||
}
|
||||
|
||||
func New(cfg serviceconfig.ConfigProvider) *Service {
|
||||
return &Service{
|
||||
cfg,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package cleanversion_test
|
||||
|
||||
import (
|
||||
cleanversion "queryorchestration/internal/document/clean/version"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
svc := cleanversion.New(nil)
|
||||
assert.NotNil(t, svc)
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package cleanversion
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func (s *Service) GetVersion() int32 {
|
||||
// TODO - actual version
|
||||
return 1
|
||||
}
|
||||
|
||||
func (s *Service) IsValidVersion(v int32) error {
|
||||
currentVersion := s.GetVersion()
|
||||
|
||||
if v < 1 || v > currentVersion {
|
||||
return fmt.Errorf("document clean code version must be in the range 0 < version <= %d", currentVersion)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package cleanversion_test
|
||||
|
||||
import (
|
||||
cleanversion "queryorchestration/internal/document/clean/version"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestIsValidVersion(t *testing.T) {
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
svc := cleanversion.New(cfg)
|
||||
|
||||
assert.Nil(t, svc.IsValidVersion(1))
|
||||
assert.Error(t, svc.IsValidVersion(2))
|
||||
assert.Error(t, svc.IsValidVersion(0))
|
||||
assert.Error(t, svc.IsValidVersion(-1))
|
||||
}
|
||||
|
||||
func TestGetVersion(t *testing.T) {
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
svc := cleanversion.New(cfg)
|
||||
|
||||
assert.Equal(t, int32(1), svc.GetVersion())
|
||||
}
|
||||
@@ -6,11 +6,10 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
doccleanrunner "queryorchestration/api/docCleanRunner"
|
||||
docsyncrunner "queryorchestration/api/docSyncRunner"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/document"
|
||||
"queryorchestration/internal/job"
|
||||
"queryorchestration/internal/serviceconfig/queue"
|
||||
"regexp"
|
||||
|
||||
@@ -26,11 +25,6 @@ type Create struct {
|
||||
}
|
||||
|
||||
func (s *Service) Create(ctx context.Context, doc *Create) (uuid.UUID, error) {
|
||||
j, err := s.svc.Job.Get(ctx, doc.JobID)
|
||||
if err != nil {
|
||||
return uuid.Nil, err
|
||||
}
|
||||
|
||||
params, err := s.getCreateParams(ctx, doc)
|
||||
if err != nil {
|
||||
return uuid.Nil, err
|
||||
@@ -42,7 +36,12 @@ func (s *Service) Create(ctx context.Context, doc *Create) (uuid.UUID, error) {
|
||||
}
|
||||
|
||||
if params.ID == nil {
|
||||
err = s.informCreate(ctx, id, j)
|
||||
err := s.cfg.SendToQueue(ctx, &queue.SendParams{
|
||||
QueueURL: s.cfg.GetDocumentSyncURL(),
|
||||
Body: docsyncrunner.Body{
|
||||
ID: id,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return uuid.Nil, err
|
||||
}
|
||||
@@ -120,32 +119,19 @@ func (s *Service) submitCreate(ctx context.Context, params *createDocumentParams
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (s *Service) informCreate(ctx context.Context, id uuid.UUID, j *job.Job) error {
|
||||
if !j.CanSync {
|
||||
return nil
|
||||
}
|
||||
|
||||
err := s.cfg.SendToQueue(ctx, &queue.SendParams{
|
||||
QueueURL: s.cfg.GetDocumentCleanURL(),
|
||||
Body: doccleanrunner.Body{
|
||||
ID: id,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) GetJobIDFromKey(key string) (uuid.UUID, error) {
|
||||
re := regexp.MustCompile(`^.+/(.+)/.+$`)
|
||||
re := regexp.MustCompile(`^(.+)/(.+)/.+$`)
|
||||
match := re.FindStringSubmatch(key)
|
||||
if match == nil || len(match) != 2 {
|
||||
if match == nil || len(match) != 3 {
|
||||
return uuid.Nil, fmt.Errorf("no job id match found in key")
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(match[1])
|
||||
_, err := uuid.Parse(match[1])
|
||||
if err != nil {
|
||||
return uuid.Nil, err
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(match[2])
|
||||
if err != nil {
|
||||
return uuid.Nil, err
|
||||
}
|
||||
|
||||
@@ -4,15 +4,10 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"queryorchestration/internal/client"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/document"
|
||||
"queryorchestration/internal/job"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/objectstore"
|
||||
"queryorchestration/internal/serviceconfig/queue/documentclean"
|
||||
objectstoremock "queryorchestration/mocks/objectstore"
|
||||
queuemock "queryorchestration/mocks/queue"
|
||||
"testing"
|
||||
|
||||
@@ -23,12 +18,6 @@ import (
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type DocInitConfig struct {
|
||||
serviceconfig.BaseConfig
|
||||
documentclean.DocCleanConfig
|
||||
objectstore.ObjectStoreConfig
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -39,14 +28,11 @@ func TestCreate(t *testing.T) {
|
||||
cfg := &DocInitConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
mockStore := objectstoremock.NewMockS3Client(t)
|
||||
cfg.StoreClient = mockStore
|
||||
mockSQS := queuemock.NewMockSQSClient(t)
|
||||
cfg.QueueClient = mockSQS
|
||||
cfg.DocumentSyncURL = "/i/am/here"
|
||||
|
||||
svc := New(cfg, &Services{
|
||||
Job: job.New(cfg, &job.Services{
|
||||
Client: client.New(cfg),
|
||||
}),
|
||||
})
|
||||
svc := New(cfg)
|
||||
|
||||
j := job.Job{
|
||||
ID: uuid.New(),
|
||||
@@ -62,14 +48,6 @@ func TestCreate(t *testing.T) {
|
||||
Key: "/i/am/here",
|
||||
}
|
||||
|
||||
pool.ExpectQuery("name: GetJob :one").WithArgs(database.MustToDBUUID(j.ID)).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "clientId", "canSync"}).
|
||||
AddRow(database.MustToDBUUID(j.ID), database.MustToDBUUID(j.ClientID), j.CanSync),
|
||||
)
|
||||
pool.ExpectQuery("name: GetClient :one").WithArgs(database.MustToDBUUID(j.ClientID)).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "name", "canSync"}).
|
||||
AddRow(database.MustToDBUUID(j.ClientID), "client_name", true),
|
||||
)
|
||||
pool.ExpectQuery("name: GetDocumentIDByHash :one").WithArgs(doc.Hash, database.MustToDBUUID(doc.JobID)).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id"}),
|
||||
)
|
||||
@@ -82,6 +60,29 @@ func TestCreate(t *testing.T) {
|
||||
pool.ExpectExec("name: AddDocumentEntry :exec").WithArgs(database.MustToDBUUID(doc.ID), location.Bucket, location.Key).
|
||||
WillReturnResult(pgxmock.NewResult("", 1))
|
||||
pool.ExpectCommit()
|
||||
pool.ExpectQuery("name: GetDocument :one").WithArgs(database.MustToDBUUID(doc.ID)).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "jobId", "hash"}).
|
||||
AddRow(database.MustToDBUUID(doc.ID), database.MustToDBUUID(doc.JobID), doc.Hash),
|
||||
)
|
||||
pool.ExpectQuery("name: GetJob :one").WithArgs(database.MustToDBUUID(j.ID)).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "clientId", "canSync"}).
|
||||
AddRow(database.MustToDBUUID(j.ID), database.MustToDBUUID(j.ClientID), j.CanSync),
|
||||
)
|
||||
pool.ExpectQuery("name: GetClient :one").WithArgs(database.MustToDBUUID(j.ClientID)).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "name", "canSync"}).
|
||||
AddRow(database.MustToDBUUID(j.ClientID), "client_name", true),
|
||||
)
|
||||
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.DocumentSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", doc.ID.String())
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
id, err := svc.Create(ctx, &Create{
|
||||
JobID: doc.JobID,
|
||||
@@ -92,37 +93,6 @@ func TestCreate(t *testing.T) {
|
||||
assert.Equal(t, doc.ID, id)
|
||||
}
|
||||
|
||||
func TestInformCreate(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
mockSQS := queuemock.NewMockSQSClient(t)
|
||||
|
||||
cfg := &DocInitConfig{}
|
||||
cfg.QueueClient = mockSQS
|
||||
cfg.DocumentCleanURL = "/i/am/here"
|
||||
svc := Service{
|
||||
cfg: cfg,
|
||||
}
|
||||
id := uuid.New()
|
||||
j := &job.Job{}
|
||||
|
||||
err := svc.informCreate(ctx, id, j)
|
||||
assert.NoError(t, err)
|
||||
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.DocumentCleanURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", id)
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
j.CanSync = true
|
||||
err = svc.informCreate(ctx, id, j)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestGetCreateParams(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -133,14 +103,8 @@ func TestGetCreateParams(t *testing.T) {
|
||||
cfg := &DocInitConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
mockStore := objectstoremock.NewMockS3Client(t)
|
||||
cfg.StoreClient = mockStore
|
||||
|
||||
svc := New(cfg, &Services{
|
||||
Job: job.New(cfg, &job.Services{
|
||||
Client: client.New(cfg),
|
||||
}),
|
||||
})
|
||||
svc := New(cfg)
|
||||
|
||||
doc := document.Document{
|
||||
JobID: uuid.New(),
|
||||
@@ -178,14 +142,8 @@ func TestGetCreateParamsExisting(t *testing.T) {
|
||||
cfg := &DocInitConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
mockStore := objectstoremock.NewMockS3Client(t)
|
||||
cfg.StoreClient = mockStore
|
||||
|
||||
svc := New(cfg, &Services{
|
||||
Job: job.New(cfg, &job.Services{
|
||||
Client: client.New(cfg),
|
||||
}),
|
||||
})
|
||||
svc := New(cfg)
|
||||
|
||||
doc := document.Document{
|
||||
ID: uuid.New(),
|
||||
@@ -227,14 +185,8 @@ func TestGetCreateParamsCurrentDocErr(t *testing.T) {
|
||||
cfg := &DocInitConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
mockStore := objectstoremock.NewMockS3Client(t)
|
||||
cfg.StoreClient = mockStore
|
||||
|
||||
svc := New(cfg, &Services{
|
||||
Job: job.New(cfg, &job.Services{
|
||||
Client: client.New(cfg),
|
||||
}),
|
||||
})
|
||||
svc := New(cfg)
|
||||
|
||||
doc := document.Document{
|
||||
JobID: uuid.New(),
|
||||
@@ -266,11 +218,7 @@ func TestSubmitCreate(t *testing.T) {
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
svc := New(cfg, &Services{
|
||||
Job: job.New(cfg, &job.Services{
|
||||
Client: client.New(cfg),
|
||||
}),
|
||||
})
|
||||
svc := New(cfg)
|
||||
|
||||
doc := document.Document{
|
||||
ID: uuid.New(),
|
||||
@@ -312,11 +260,7 @@ func TestSubmitCreateExists(t *testing.T) {
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
|
||||
svc := New(cfg, &Services{
|
||||
Job: job.New(cfg, &job.Services{
|
||||
Client: client.New(cfg),
|
||||
}),
|
||||
})
|
||||
svc := New(cfg)
|
||||
|
||||
doc := document.Document{
|
||||
ID: uuid.New(),
|
||||
@@ -357,7 +301,10 @@ func TestGetJobIDFromKey(t *testing.T) {
|
||||
assert.Error(t, err)
|
||||
|
||||
id := uuid.New()
|
||||
aid, err := svc.GetJobIDFromKey(fmt.Sprintf("aaa/%s/bbb", id.String()))
|
||||
_, err = svc.GetJobIDFromKey(fmt.Sprintf("aaa/%s/bbb", id.String()))
|
||||
assert.Error(t, err)
|
||||
|
||||
aid, err := svc.GetJobIDFromKey(fmt.Sprintf("%s/%s/bbb", uuid.NewString(), id.String()))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, id, aid)
|
||||
}
|
||||
|
||||
@@ -1,28 +1,21 @@
|
||||
package documentinit
|
||||
|
||||
import (
|
||||
"queryorchestration/internal/job"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/queue/documentclean"
|
||||
documentsync "queryorchestration/internal/serviceconfig/queue/documentsync"
|
||||
)
|
||||
|
||||
type Services struct {
|
||||
Job *job.Service
|
||||
}
|
||||
|
||||
type ConfigProvider interface {
|
||||
serviceconfig.ConfigProvider
|
||||
documentclean.ConfigProvider
|
||||
documentsync.ConfigProvider
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
cfg ConfigProvider
|
||||
svc *Services
|
||||
}
|
||||
|
||||
func New(cfg ConfigProvider, svc *Services) *Service {
|
||||
func New(cfg ConfigProvider) *Service {
|
||||
return &Service{
|
||||
cfg,
|
||||
svc,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,20 @@
|
||||
package documentinit_test
|
||||
package documentinit
|
||||
|
||||
import (
|
||||
"queryorchestration/internal/document"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/queue/documentsync"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
svc := document.New(nil)
|
||||
assert.NotNil(t, svc)
|
||||
type DocInitConfig struct {
|
||||
serviceconfig.BaseConfig
|
||||
documentsync.DocSyncConfig
|
||||
}
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
svc := New(&DocInitConfig{})
|
||||
assert.NotNil(t, svc)
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package documentsync
|
||||
|
||||
import (
|
||||
"queryorchestration/internal/document"
|
||||
"queryorchestration/internal/job"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/queue/documentclean"
|
||||
)
|
||||
|
||||
type Services struct {
|
||||
Document *document.Service
|
||||
Job *job.Service
|
||||
}
|
||||
|
||||
type ConfigProvider interface {
|
||||
serviceconfig.ConfigProvider
|
||||
documentclean.ConfigProvider
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
cfg ConfigProvider
|
||||
svc *Services
|
||||
}
|
||||
|
||||
func New(cfg ConfigProvider, svc *Services) *Service {
|
||||
return &Service{
|
||||
cfg,
|
||||
svc,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package documentsync_test
|
||||
|
||||
import (
|
||||
documentsync "queryorchestration/internal/document/sync"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/queue/documentclean"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type DocSyncConfig struct {
|
||||
serviceconfig.BaseConfig
|
||||
documentclean.DocCleanConfig
|
||||
}
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
svc := documentsync.New(&DocSyncConfig{}, &documentsync.Services{})
|
||||
assert.NotNil(t, svc)
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package documentsync
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
doccleanrunner "queryorchestration/api/docCleanRunner"
|
||||
"queryorchestration/internal/serviceconfig/queue"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func (s *Service) Sync(ctx context.Context, id uuid.UUID) error {
|
||||
doc, err := s.svc.Document.Get(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
j, err := s.svc.Job.Get(ctx, doc.JobID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !j.CanSync {
|
||||
slog.Debug("not syncing document", "id", id.String())
|
||||
return nil
|
||||
}
|
||||
|
||||
slog.Debug("syncing document", "id", id.String())
|
||||
|
||||
err = s.cfg.SendToQueue(ctx, &queue.SendParams{
|
||||
QueueURL: s.cfg.GetDocumentCleanURL(),
|
||||
Body: doccleanrunner.Body{
|
||||
ID: id,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package documentsync_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"queryorchestration/internal/client"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/document"
|
||||
documentsync "queryorchestration/internal/document/sync"
|
||||
"queryorchestration/internal/job"
|
||||
queuemock "queryorchestration/mocks/queue"
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/sqs"
|
||||
"github.com/google/uuid"
|
||||
"github.com/pashagolub/pgxmock/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestSync(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open pgxmock database: %v", err)
|
||||
}
|
||||
cfg := &DocSyncConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
mockSQS := queuemock.NewMockSQSClient(t)
|
||||
cfg.QueueClient = mockSQS
|
||||
cfg.DocumentCleanURL = "/i/am/here"
|
||||
|
||||
svc := documentsync.New(cfg, &documentsync.Services{
|
||||
Document: document.New(cfg),
|
||||
Job: job.New(cfg, &job.Services{
|
||||
Client: client.New(cfg),
|
||||
}),
|
||||
})
|
||||
|
||||
j := job.Job{
|
||||
ID: uuid.New(),
|
||||
ClientID: uuid.New(),
|
||||
CanSync: true,
|
||||
}
|
||||
doc := document.Document{
|
||||
ID: uuid.New(),
|
||||
JobID: j.ID,
|
||||
Hash: "example_hash",
|
||||
}
|
||||
|
||||
pool.ExpectQuery("name: GetDocument :one").WithArgs(database.MustToDBUUID(doc.ID)).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "jobId", "hash"}).
|
||||
AddRow(database.MustToDBUUID(doc.ID), database.MustToDBUUID(doc.JobID), doc.Hash),
|
||||
)
|
||||
pool.ExpectQuery("name: GetJob :one").WithArgs(database.MustToDBUUID(j.ID)).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "clientId", "canSync"}).
|
||||
AddRow(database.MustToDBUUID(j.ID), database.MustToDBUUID(j.ClientID), j.CanSync),
|
||||
)
|
||||
pool.ExpectQuery("name: GetClient :one").WithArgs(database.MustToDBUUID(j.ClientID)).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "name", "canSync"}).
|
||||
AddRow(database.MustToDBUUID(j.ClientID), "client_name", true),
|
||||
)
|
||||
|
||||
mockSQS.EXPECT().
|
||||
SendMessage(
|
||||
mock.Anything,
|
||||
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
||||
return *in.QueueUrl == cfg.DocumentCleanURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", doc.ID)
|
||||
}),
|
||||
mock.Anything,
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
err = svc.Sync(ctx, doc.ID)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/document"
|
||||
textversion "queryorchestration/internal/document/text/version"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/objectstore"
|
||||
"queryorchestration/internal/serviceconfig/queue/querysync"
|
||||
queuemock "queryorchestration/mocks/queue"
|
||||
"testing"
|
||||
@@ -21,7 +21,6 @@ import (
|
||||
|
||||
type DocTextConfig struct {
|
||||
serviceconfig.BaseConfig
|
||||
objectstore.ObjectStoreConfig
|
||||
querysync.QuerySyncConfig
|
||||
}
|
||||
|
||||
@@ -43,7 +42,7 @@ func TestCreate(t *testing.T) {
|
||||
svc := Service{
|
||||
cfg: cfg,
|
||||
svc: &Services{
|
||||
Document: document.New(cfg),
|
||||
Version: textversion.New(cfg),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ func (s *Service) extract(ctx context.Context, id uuid.UUID) error {
|
||||
return err
|
||||
}
|
||||
|
||||
version := s.svc.Document.GetTextVersion()
|
||||
version := s.svc.Version.GetVersion()
|
||||
|
||||
err = s.cfg.GetDBQueries().AddDocumentTextEntry(ctx, &repository.AddDocumentTextEntryParams{
|
||||
Documentid: docId,
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/document"
|
||||
textversion "queryorchestration/internal/document/text/version"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -26,7 +27,7 @@ func TestExtract(t *testing.T) {
|
||||
svc := Service{
|
||||
cfg: cfg,
|
||||
svc: &Services{
|
||||
Document: document.New(cfg),
|
||||
Version: textversion.New(cfg),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -1,20 +1,18 @@
|
||||
package documenttext
|
||||
|
||||
import (
|
||||
"queryorchestration/internal/document"
|
||||
textversion "queryorchestration/internal/document/text/version"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/objectstore"
|
||||
"queryorchestration/internal/serviceconfig/queue/querysync"
|
||||
)
|
||||
|
||||
type ConfigProvider interface {
|
||||
serviceconfig.ConfigProvider
|
||||
objectstore.ConfigProvider
|
||||
querysync.ConfigProvider
|
||||
}
|
||||
|
||||
type Services struct {
|
||||
Document *document.Service
|
||||
Version *textversion.Service
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package documenttext_test
|
||||
|
||||
import (
|
||||
"queryorchestration/internal/document"
|
||||
documenttext "queryorchestration/internal/document/text"
|
||||
textversion "queryorchestration/internal/document/text/version"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/objectstore"
|
||||
"queryorchestration/internal/serviceconfig/queue/querysync"
|
||||
"testing"
|
||||
|
||||
@@ -13,14 +12,13 @@ import (
|
||||
|
||||
type DocTextConfig struct {
|
||||
serviceconfig.BaseConfig
|
||||
objectstore.ObjectStoreConfig
|
||||
querysync.QuerySyncConfig
|
||||
}
|
||||
|
||||
func TestService(t *testing.T) {
|
||||
cfg := &DocTextConfig{}
|
||||
svc := documenttext.New(cfg, &documenttext.Services{
|
||||
Document: document.New(cfg),
|
||||
Version: textversion.New(cfg),
|
||||
})
|
||||
assert.NotNil(t, svc)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package textversion
|
||||
|
||||
import (
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
cfg serviceconfig.ConfigProvider
|
||||
}
|
||||
|
||||
func New(cfg serviceconfig.ConfigProvider) *Service {
|
||||
return &Service{
|
||||
cfg,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package textversion_test
|
||||
|
||||
import (
|
||||
textversion "queryorchestration/internal/document/clean/version"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
svc := textversion.New(nil)
|
||||
assert.NotNil(t, svc)
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package textversion
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func (s *Service) GetVersion() int32 {
|
||||
// TODO - actual version
|
||||
return 1
|
||||
}
|
||||
|
||||
func (s *Service) IsValidVersion(v int32) error {
|
||||
currentVersion := s.GetVersion()
|
||||
|
||||
if v < 1 || v > currentVersion {
|
||||
return fmt.Errorf("document text code version must be in the range 0 < version <= %d", currentVersion)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package textversion_test
|
||||
|
||||
import (
|
||||
textversion "queryorchestration/internal/document/clean/version"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestIsValidVersion(t *testing.T) {
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
svc := textversion.New(cfg)
|
||||
|
||||
assert.Nil(t, svc.IsValidVersion(1))
|
||||
assert.Error(t, svc.IsValidVersion(2))
|
||||
assert.Error(t, svc.IsValidVersion(0))
|
||||
assert.Error(t, svc.IsValidVersion(-1))
|
||||
}
|
||||
|
||||
func TestGetVersion(t *testing.T) {
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
svc := textversion.New(cfg)
|
||||
|
||||
assert.Equal(t, int32(1), svc.GetVersion())
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package document
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func (s *Service) GetCleanVersion() int32 {
|
||||
// TODO - actual version
|
||||
return 1
|
||||
}
|
||||
|
||||
func (s *Service) GetTextVersion() int32 {
|
||||
// TODO - actual version
|
||||
return 1
|
||||
}
|
||||
|
||||
func (s *Service) IsValidCleanVersion(v int32) error {
|
||||
currentVersion := s.GetCleanVersion()
|
||||
|
||||
if v < 1 || v > currentVersion {
|
||||
return fmt.Errorf("document clean code version must be in the range 0 < version <= %d", currentVersion)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) IsValidTextVersion(v int32) error {
|
||||
currentVersion := s.GetTextVersion()
|
||||
|
||||
if v < 1 || v > currentVersion {
|
||||
return fmt.Errorf("document text code version must be in the range 0 < version <= %d", currentVersion)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package document_test
|
||||
|
||||
import (
|
||||
"queryorchestration/internal/document"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestIsValidCleanVersion(t *testing.T) {
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
svc := document.New(cfg)
|
||||
|
||||
assert.Nil(t, svc.IsValidCleanVersion(1))
|
||||
assert.Error(t, svc.IsValidCleanVersion(2))
|
||||
assert.Error(t, svc.IsValidCleanVersion(0))
|
||||
assert.Error(t, svc.IsValidCleanVersion(-1))
|
||||
}
|
||||
|
||||
func TestIsValidTextVersion(t *testing.T) {
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
svc := document.New(cfg)
|
||||
|
||||
assert.Nil(t, svc.IsValidTextVersion(1))
|
||||
assert.Error(t, svc.IsValidTextVersion(2))
|
||||
assert.Error(t, svc.IsValidTextVersion(0))
|
||||
assert.Error(t, svc.IsValidTextVersion(-1))
|
||||
}
|
||||
|
||||
func TestGetCleanVersion(t *testing.T) {
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
svc := document.New(cfg)
|
||||
|
||||
assert.Equal(t, int32(1), svc.GetCleanVersion())
|
||||
}
|
||||
|
||||
func TestGetTextVersion(t *testing.T) {
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
svc := document.New(cfg)
|
||||
|
||||
assert.Equal(t, int32(1), svc.GetTextVersion())
|
||||
}
|
||||
Reference in New Issue
Block a user