Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(db/create): ask to wake sleeping groups before create #900

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions internal/cmd/db_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/spf13/cobra"
"github.com/tursodatabase/turso-cli/internal"
"github.com/tursodatabase/turso-cli/internal/flags"
"github.com/tursodatabase/turso-cli/internal/prompt"
"github.com/tursodatabase/turso-cli/internal/turso"
)

Expand Down Expand Up @@ -75,14 +74,16 @@ var createCmd = &cobra.Command{
}

start := time.Now()
spinner := prompt.Spinner(fmt.Sprintf("Creating database %s in group %s...", internal.Emph(name), internal.Emph(group)))
defer spinner.Stop()
spinnerText := fmt.Sprintf("Creating database %s in group %s...", internal.Emph(name), internal.Emph(group))

if _, err = client.Databases.Create(name, location, "", "", group, schemaFlag, typeFlag == "schema", seed, sizeLimitFlag); err != nil {
_, err = RetryOnSleepingGroup(client, group, spinnerText, func() (any, error) {
return client.Databases.Create(name, location, "", "", group, schemaFlag, typeFlag == "schema", seed, sizeLimitFlag)
})

if err != nil {
return fmt.Errorf("could not create database %s: %w", name, err)
}

spinner.Stop()
elapsed := time.Since(start)
fmt.Printf("Created database %s at group %s in %s.\n\n", internal.Emph(name), internal.Emph(group), elapsed.Round(time.Millisecond).String())

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func groupsTable(groups []turso.Group) [][]string {
func aggregateGroupStatus(group turso.Group) string {
status := "Healthy"
if group.Archived {
return "Archived 💤"
return "Archived"
}
allIdle := true
for _, locationStatus := range group.Status.Locations {
Expand Down
52 changes: 52 additions & 0 deletions internal/cmd/group_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cmd

import (
"fmt"
"strings"
"time"

"github.com/tursodatabase/turso-cli/internal"
"github.com/tursodatabase/turso-cli/internal/prompt"
"github.com/tursodatabase/turso-cli/internal/turso"
)

func RetryOnSleepingGroup(client *turso.Client, group string, mainSpinnerText string, action func() (any, error)) (any, error) {
mainSpinner := prompt.Spinner(mainSpinnerText)
result, actionErr := action()
if actionErr == nil {
return result, nil
}

errMsg := actionErr.Error()
if strings.Contains(errMsg, "group_sleeping") ||
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fear group_sleeping will never be reached because we don't actually return or formalise the API error from the turso client.

(strings.Contains(errMsg, "cannot create database on group") && strings.Contains(errMsg, "because it is archived")) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be reached, but I hate having to pass in the string and detect errors like this. I would much prefer to use group_sleeping from the error response code field.


mainSpinner.Stop()
fmt.Printf("Error: %s\n\n", errMsg)
time.Sleep(time.Second)

promptMsg := fmt.Sprintf("The group %s is currently archived. Do you want to unarchive it now??", internal.Emph(group))
confirmed, err := promptConfirmation(promptMsg)
if err != nil {
return nil, fmt.Errorf("could not get prompt confirmed by user: %w", err)
}
if !confirmed {
return nil, fmt.Errorf("cannot perform action on an archived group")
}

err = unarchiveGroup(client, group)
if err != nil {
return nil, fmt.Errorf("failed to wake up group: %w", err)
}

fmt.Printf("Retrying...\n")
time.Sleep(time.Second)

mainSpinner = prompt.Spinner(mainSpinnerText)

Check failure on line 45 in internal/cmd/group_utils.go

View workflow job for this annotation

GitHub Actions / build

this value of mainSpinner is never used (SA4006)

return action()
}

mainSpinner.Stop()
return nil, actionErr
}
Loading