fix: refresh table dispatchers after binding activation
This commit is contained in:
@@ -13,12 +13,13 @@ import (
|
|||||||
|
|
||||||
type validatedSettingsRepository struct {
|
type validatedSettingsRepository struct {
|
||||||
*postgres.SettingsRepository
|
*postgres.SettingsRepository
|
||||||
clientID string
|
clientID string
|
||||||
secret string
|
secret string
|
||||||
testSupport func(context.Context, settings.ResourceBinding) error
|
testSupport func(context.Context, settings.ResourceBinding) error
|
||||||
saveBinding func(context.Context, settings.ResourceBinding) error
|
validateTable func(context.Context, settings.ResourceBinding) (map[string]string, error)
|
||||||
restart func()
|
saveBinding func(context.Context, settings.ResourceBinding) error
|
||||||
restartDelay time.Duration
|
restart func()
|
||||||
|
restartDelay time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *validatedSettingsRepository) TestAndActivateSupportGroup(ctx context.Context, binding settings.ResourceBinding) error {
|
func (r *validatedSettingsRepository) TestAndActivateSupportGroup(ctx context.Context, binding settings.ResourceBinding) error {
|
||||||
@@ -60,35 +61,73 @@ func (r *validatedSettingsRepository) TestAndActivateSupportGroup(ctx context.Co
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *validatedSettingsRepository) SaveBinding(ctx context.Context, binding settings.ResourceBinding) error {
|
func (r *validatedSettingsRepository) SaveBinding(ctx context.Context, binding settings.ResourceBinding) error {
|
||||||
|
activate := false
|
||||||
switch binding.Kind {
|
switch binding.Kind {
|
||||||
case "question_table", "faq_table":
|
case "question_table", "faq_table":
|
||||||
client, err := questiontable.NewClient(questiontable.Config{
|
var mapping map[string]string
|
||||||
ClientID: r.clientID, Secret: r.secret, BaseID: binding.BaseID,
|
var err error
|
||||||
SheetID: binding.SheetID, OperatorID: binding.OperatorID,
|
if r.validateTable != nil {
|
||||||
}, nil)
|
mapping, err = r.validateTable(ctx, binding)
|
||||||
if err != nil {
|
} else {
|
||||||
return errors.New("table_binding_invalid_config")
|
mapping, err = r.readAndMapTable(ctx, binding)
|
||||||
}
|
}
|
||||||
fields, err := client.ReadFields(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
required := questiontable.QuestionSchema()
|
|
||||||
if binding.Kind == "faq_table" {
|
|
||||||
required = questiontable.FAQSchema()
|
|
||||||
}
|
|
||||||
mapping, err := questiontable.MapRequiredFields(fields, required)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
binding.FieldMapping = mapping
|
binding.FieldMapping = mapping
|
||||||
binding.ValidationCode = "schema_ok"
|
binding.ValidationCode = "schema_ok"
|
||||||
binding.Activate = true
|
binding.Activate = true
|
||||||
|
activate = true
|
||||||
case "support_group":
|
case "support_group":
|
||||||
binding.ValidationCode = "pending_test_message"
|
binding.ValidationCode = "pending_test_message"
|
||||||
binding.Activate = false
|
binding.Activate = false
|
||||||
default:
|
default:
|
||||||
return errors.New("unsupported_binding_kind")
|
return errors.New("unsupported_binding_kind")
|
||||||
}
|
}
|
||||||
return r.SettingsRepository.SaveBinding(ctx, binding)
|
var err error
|
||||||
|
if r.saveBinding != nil {
|
||||||
|
err = r.saveBinding(ctx, binding)
|
||||||
|
} else if r.SettingsRepository != nil {
|
||||||
|
err = r.SettingsRepository.SaveBinding(ctx, binding)
|
||||||
|
} else {
|
||||||
|
err = errors.New("binding_repository_unavailable")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if activate {
|
||||||
|
r.requestRestart()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *validatedSettingsRepository) readAndMapTable(ctx context.Context, binding settings.ResourceBinding) (map[string]string, error) {
|
||||||
|
client, err := questiontable.NewClient(questiontable.Config{
|
||||||
|
ClientID: r.clientID, Secret: r.secret, BaseID: binding.BaseID,
|
||||||
|
SheetID: binding.SheetID, OperatorID: binding.OperatorID,
|
||||||
|
}, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("table_binding_invalid_config")
|
||||||
|
}
|
||||||
|
fields, err := client.ReadFields(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
required := questiontable.QuestionSchema()
|
||||||
|
if binding.Kind == "faq_table" {
|
||||||
|
required = questiontable.FAQSchema()
|
||||||
|
}
|
||||||
|
return questiontable.MapRequiredFields(fields, required)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *validatedSettingsRepository) requestRestart() {
|
||||||
|
if r.restart == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
if r.restartDelay > 0 {
|
||||||
|
time.Sleep(r.restartDelay)
|
||||||
|
}
|
||||||
|
r.restart()
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,63 @@ import (
|
|||||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/settings"
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/settings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestTableBindingRestartsOnlyAfterActiveSave(t *testing.T) {
|
||||||
|
restarted := make(chan struct{}, 1)
|
||||||
|
var saved settings.ResourceBinding
|
||||||
|
repo := &validatedSettingsRepository{
|
||||||
|
validateTable: func(_ context.Context, binding settings.ResourceBinding) (map[string]string, error) {
|
||||||
|
return map[string]string{"answer": "field-answer"}, nil
|
||||||
|
},
|
||||||
|
saveBinding: func(_ context.Context, binding settings.ResourceBinding) error {
|
||||||
|
saved = binding
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
restart: func() { restarted <- struct{}{} },
|
||||||
|
restartDelay: 0,
|
||||||
|
}
|
||||||
|
if err := repo.SaveBinding(context.Background(), settings.ResourceBinding{Kind: "question_table", TenantKey: "tenant", BaseID: "base", SheetID: "sheet", OperatorID: "operator"}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !saved.Activate || saved.ValidationCode != "schema_ok" || saved.FieldMapping["answer"] != "field-answer" {
|
||||||
|
t.Fatalf("binding=%+v", saved)
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-restarted:
|
||||||
|
case <-time.After(50 * time.Millisecond):
|
||||||
|
t.Fatal("restart not requested")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTableBindingDoesNotRestartOnValidationOrSaveFailure(t *testing.T) {
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
validateErr error
|
||||||
|
saveErr error
|
||||||
|
}{
|
||||||
|
{name: "validation failure", validateErr: errors.New("schema failed")},
|
||||||
|
{name: "save failure", saveErr: errors.New("save failed")},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
restarted := make(chan struct{}, 1)
|
||||||
|
repo := &validatedSettingsRepository{
|
||||||
|
validateTable: func(context.Context, settings.ResourceBinding) (map[string]string, error) {
|
||||||
|
return map[string]string{"answer": "field"}, tc.validateErr
|
||||||
|
},
|
||||||
|
saveBinding: func(context.Context, settings.ResourceBinding) error { return tc.saveErr },
|
||||||
|
restart: func() { restarted <- struct{}{} },
|
||||||
|
}
|
||||||
|
if err := repo.SaveBinding(context.Background(), settings.ResourceBinding{Kind: "question_table"}); err == nil {
|
||||||
|
t.Fatal("expected failure")
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-restarted:
|
||||||
|
t.Fatal("must not restart after failed activation")
|
||||||
|
case <-time.After(50 * time.Millisecond):
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSupportGroupActivationRestartsOnlyAfterTestAndSaveSucceed(t *testing.T) {
|
func TestSupportGroupActivationRestartsOnlyAfterTestAndSaveSucceed(t *testing.T) {
|
||||||
for _, tc := range []struct {
|
for _, tc := range []struct {
|
||||||
name string
|
name string
|
||||||
|
|||||||
Reference in New Issue
Block a user