Skip to content

Reusing the last completed task rather than re-requesting #182

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
71 changes: 0 additions & 71 deletions latest_backups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,6 @@ func TestGetLatestBackup(t *testing.T) {
]
}`,
),
getRequest(
t,
"/tasks/50ec6172-8475-4ef6-8b3c-d61e688d8fe5",
`{
"taskId": "50ec6172-8475-4ef6-8b3c-d61e688d8fe5",
"commandType": "databaseBackupStatusRequest",
"status": "processing-completed",
"description": "Request processing completed successfully and its resources are now being provisioned / de-provisioned.",
"timestamp": "2024-04-15T09:08:07.537915Z",
"response": {
"resourceId": 51051292,
"additionalResourceId": 12,
"resource": {}
},
"links": [
{
"href": "https://api-staging.qa.redislabs.com/v1/tasks/50ec6172-8475-4ef6-8b3c-d61e688d8fe5",
"type": "GET",
"rel": "self"
}
]
}`,
),
))

subject, err := clientFromTestServer(server, "key", "secret")
Expand Down Expand Up @@ -135,29 +112,6 @@ func TestGetFixedLatestBackup(t *testing.T) {
]
}`,
),
getRequest(
t,
"/tasks/ce2cbfea-9b15-4250-a516-f014161a8dd3",
`{
"taskId": "ce2cbfea-9b15-4250-a516-f014161a8dd3",
"commandType": "databaseBackupStatusRequest",
"status": "processing-completed",
"description": "Request processing completed successfully and its resources are now being provisioned / de-provisioned.",
"timestamp": "2024-04-15T09:52:26.101936Z",
"response": {
"resource": {
"status": "success"
}
},
"links": [
{
"href": "https://api-staging.qa.redislabs.com/v1/tasks/ce2cbfea-9b15-4250-a516-f014161a8dd3",
"type": "GET",
"rel": "self"
}
]
}`,
),
))

subject, err := clientFromTestServer(server, "key", "secret")
Expand Down Expand Up @@ -230,31 +184,6 @@ func TestGetAALatestBackup(t *testing.T) {
]
}`,
),
getRequest(
t,
"/tasks/ce2cbfea-9b15-4250-a516-f014161a8dd3",
`{
"taskId": "ce2cbfea-9b15-4250-a516-f014161a8dd3",
"commandType": "databaseBackupStatusRequest",
"status": "processing-error",
"description": "Task request failed during processing. See error information for failure details.",
"timestamp": "2024-04-15T09:52:26.101936Z",
"response": {
"error": {
"type": "DATABASE_BACKUP_DISABLED",
"status": "400 BAD_REQUEST",
"description": "Database backup is disabled"
}
},
"links": [
{
"href": "https://api-staging.qa.redislabs.com/v1/tasks/ce2cbfea-9b15-4250-a516-f014161a8dd3",
"type": "GET",
"rel": "self"
}
]
}`,
),
))

subject, err := clientFromTestServer(server, "key", "secret")
Expand Down
87 changes: 0 additions & 87 deletions latest_imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,31 +61,6 @@ func TestGetLatestImportTooEarly(t *testing.T) {
]
}`,
),
getRequest(
t,
"/tasks/1dfd6084-21df-40c6-829c-e9b4790e207e",
`{
"taskId": "1dfd6084-21df-40c6-829c-e9b4790e207e",
"commandType": "databaseImportStatusRequest",
"status": "processing-error",
"description": "Task request failed during processing. See error information for failure details.",
"timestamp": "2024-04-15T10:19:07.331898Z",
"response": {
"error": {
"type": "SUBSCRIPTION_NOT_ACTIVE",
"status": "403 FORBIDDEN",
"description": "Cannot preform any actions for subscription that is not in an active state"
}
},
"links": [
{
"href": "https://api-staging.qa.redislabs.com/v1/tasks/1dfd6084-21df-40c6-829c-e9b4790e207e",
"type": "GET",
"rel": "self"
}
]
}`,
),
))

subject, err := clientFromTestServer(server, "key", "secret")
Expand Down Expand Up @@ -143,31 +118,6 @@ func TestGetFixedLatestImport(t *testing.T) {
]
}`,
),
getRequest(
t,
"/tasks/e9232e43-3781-4263-a38e-f4d150e03475",
`{
"taskId": "e9232e43-3781-4263-a38e-f4d150e03475",
"commandType": "databaseImportStatusRequest",
"status": "processing-completed",
"description": "Request processing completed successfully and its resources are now being provisioned / de-provisioned.",
"timestamp": "2024-04-15T10:44:35.225468Z",
"response": {
"resourceId": 51051302,
"additionalResourceId": 110777,
"resource": {
"status": "importing"
}
},
"links": [
{
"href": "https://api-staging.qa.redislabs.com/v1/tasks/e9232e43-3781-4263-a38e-f4d150e03475",
"type": "GET",
"rel": "self"
}
]
}`,
),
))

subject, err := clientFromTestServer(server, "key", "secret")
Expand Down Expand Up @@ -251,43 +201,6 @@ func TestGetLatestImport(t *testing.T) {
]
}`,
),
getRequest(
t,
"/tasks/e9232e43-3781-4263-a38e-f4d150e03475",
`{
"taskId": "e9232e43-3781-4263-a38e-f4d150e03475",
"commandType": "databaseImportStatusRequest",
"status": "processing-completed",
"description": "Request processing completed successfully and its resources are now being provisioned / de-provisioned.",
"timestamp": "2024-04-15T10:44:35.225468Z",
"response": {
"resourceId": 51051302,
"additionalResourceId": 110777,
"resource": {
"failureReason": "file-corrupted",
"failureReasonParams": [
{
"key": "bytes_configured_bdb_limit",
"value": "1234"
},
{
"key": "bytes_of_expected_dataset",
"value": "5678"
}
],
"lastImportTime": "2024-05-21T10:36:26Z",
"status": "failed"
}
},
"links": [
{
"href": "https://api-staging.qa.redislabs.com/v1/tasks/e9232e43-3781-4263-a38e-f4d150e03475",
"type": "GET",
"rel": "self"
}
]
}`,
),
))

subject, err := clientFromTestServer(server, "key", "secret")
Expand Down
72 changes: 43 additions & 29 deletions service/latest_backups/model.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,52 @@
package latest_backups

import (
"encoding/json"
"fmt"
"regexp"
"time"

"github.com/RedisLabs/rediscloud-go-api/internal"
"github.com/RedisLabs/rediscloud-go-api/redis"
)

func fromInternal(t internal.Task) LatestBackupStatus {
lbs := LatestBackupStatus{
CommandType: t.CommandType,
Description: t.Description,
Status: t.Status,
ID: t.ID,
}
if t.Response == nil {
lbs.Response = nil
} else {
response := Response{
ID: t.Response.ID,
}

if t.Response.Resource == nil {
response.Resource = nil
} else {
j, err := t.Response.Resource.MarshalJSON()
if err != nil {
panic(nil)
}

var res Resource
if j != nil && len(j) > 0 {
err = json.Unmarshal(j, &res)
if err != nil {
panic(nil)
}
}

response.Resource = &res
}

response.Error = t.Response.Error
lbs.Response = &response
}
return lbs
}

type LatestBackupStatus struct {
CommandType *string `json:"commandType,omitempty"`
Description *string `json:"description,omitempty"`
Expand All @@ -22,9 +60,9 @@ func (o LatestBackupStatus) String() string {
}

type Response struct {
ID *int `json:"resourceId,omitempty"`
Resource *Resource `json:"resource,omitempty"`
Error *Error `json:"error,omitempty"`
ID *int `json:"resourceId,omitempty"`
Resource *Resource `json:"resource,omitempty"`
Error *internal.Error `json:"error,omitempty"`
}

func (o Response) String() string {
Expand All @@ -41,30 +79,6 @@ func (o Resource) String() string {
return internal.ToString(o)
}

type Error struct {
Type *string `json:"type,omitempty"`
Description *string `json:"description,omitempty"`
Status *string `json:"status,omitempty"`
}

func (e *Error) String() string {
return internal.ToString(e)
}

func (e *Error) StatusCode() string {
matches := errorStatusCode.FindStringSubmatch(redis.StringValue(e.Status))
if len(matches) == 2 {
return matches[1]
}
return ""
}

func (e *Error) Error() string {
return fmt.Sprintf("%s - %s: %s", redis.StringValue(e.Status), redis.StringValue(e.Type), redis.StringValue(e.Description))
}

var errorStatusCode = regexp.MustCompile("^(\\d*).*$")

type NotFound struct {
subId int
dbId int
Expand Down
35 changes: 9 additions & 26 deletions service/latest_backups/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type HttpClient interface {
}

type TaskWaiter interface {
Wait(ctx context.Context, id string) error
WaitForTask(ctx context.Context, id string) (*internal.Task, error)
}

type Log interface {
Expand Down Expand Up @@ -68,23 +68,16 @@ func (a *API) GetActiveActive(ctx context.Context, subscription int, database in

a.logger.Printf("Waiting for backup status request %d to complete", task.ID)

err = a.taskWaiter.Wait(ctx, *task.ID)

a.logger.Printf("Backup status request %d completed, possibly with error", task.ID, err)

var backupStatusTask *LatestBackupStatus
err = a.client.Get(ctx,
fmt.Sprintf("retrieve completed backup status task %d", task.ID),
"/tasks/"+*task.ID,
&backupStatusTask,
)

finishedTask, err := a.taskWaiter.WaitForTask(ctx, *task.ID)
if err != nil {
return nil, wrap404ErrorActiveActive(subscription, database, region,
fmt.Errorf("failed to retrieve completed backup status %d: %w", task.ID, err))
}

return backupStatusTask, nil
a.logger.Printf("Backup status request %d completed, possibly with error", task.ID, err)

lbs := fromInternal(*finishedTask)
return &lbs, nil
}

func (a *API) get(ctx context.Context, message string, address string) (*LatestBackupStatus, error) {
Expand All @@ -96,22 +89,12 @@ func (a *API) get(ctx context.Context, message string, address string) (*LatestB

a.logger.Printf("Waiting for backup status request %d to complete", task.ID)

err = a.taskWaiter.Wait(ctx, *task.ID)
finishedTask, err := a.taskWaiter.WaitForTask(ctx, *task.ID)

a.logger.Printf("Backup status request %d completed, possibly with error", task.ID, err)

var backupStatusTask *LatestBackupStatus
err = a.client.Get(ctx,
fmt.Sprintf("retrieve completed backup status task %d", task.ID),
"/tasks/"+*task.ID,
&backupStatusTask,
)

if err != nil {
return nil, fmt.Errorf("failed to retrieve completed backup status %d: %w", task.ID, err)
}

return backupStatusTask, nil
lbs := fromInternal(*finishedTask)
return &lbs, nil
}

func wrap404Error(subId int, dbId int, err error) error {
Expand Down
Loading