Skip to content

Commit cf2a319

Browse files
committed
Use go-retry for UpdateApplicationUser
1 parent b0c4b32 commit cf2a319

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

internal/pkg/application/application.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -87,26 +87,33 @@ func UpdateApplicationInstance(ctx context.Context, cfg *config.Config, instance
8787
func UpdateApplicationUser(ctx context.Context, target *resolved.Instance, gcpProject *resolved.GcpProject, mgr *common_main.Manager) error {
8888
mgr.Logger.Info("updating application user", "user", target.AppUsername)
8989

90-
getCtx, cancel := context.WithTimeout(ctx, 1*time.Minute)
91-
defer cancel()
92-
for {
93-
mgr.Logger.Info("waiting for user to be up to date", "user", target.AppUsername)
94-
sqlUser, err := mgr.SqlUserClient.Get(getCtx, target.AppUsername)
90+
b := retry.NewConstant(5 * time.Second)
91+
b = retry.WithMaxDuration(5*time.Minute, b)
92+
93+
err := retry.Do(ctx, b, func(ctx context.Context) error {
94+
sqlUser, err := mgr.SqlUserClient.Get(ctx, target.AppUsername)
9595
if err != nil {
9696
if errors.IsNotFound(err) {
97-
time.Sleep(1 * time.Second)
98-
continue
97+
mgr.Logger.Warn("user not found, retrying", "user", target.AppUsername)
98+
return retry.RetryableError(err)
9999
}
100100
return fmt.Errorf("failed to get sql user: %w", err)
101101
}
102102

103103
if sqlUser.Status.Conditions[0].Reason != "UpToDate" {
104-
time.Sleep(3 * time.Second)
105-
continue
104+
mgr.Logger.Info("user not up to date, retrying", "user", target.AppUsername)
105+
return retry.RetryableError(fmt.Errorf("user not up to date"))
106106
}
107-
break
107+
108+
return nil
109+
})
110+
111+
if err != nil {
112+
return err
108113
}
109114

115+
mgr.Logger.Info("user is up to date, setting database password", "user", target.AppUsername)
116+
110117
return database.SetDatabasePassword(ctx, target.Name, target.AppUsername, target.AppPassword, gcpProject, mgr)
111118
}
112119

0 commit comments

Comments
 (0)