Skip to content

Commit 8e659f2

Browse files
authored
Merge pull request #193 from RedisLabs/remove-extra-calls
Remove extra API call when retrieving latest backup and import Statuses
2 parents 7dcf80f + 09dd092 commit 8e659f2

File tree

7 files changed

+147
-242
lines changed

7 files changed

+147
-242
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
All notable changes to this project will be documented in this file.
33
See updating [Changelog example here](https://keepachangelog.com/en/1.0.0/).
44

5+
## 0.21.0
6+
7+
### Added
8+
9+
* Handling API rate limits to wait or retry calls depending on the current window or remaining limits.
10+
11+
### Fixed
12+
13+
* Removed extra API call when retrieving latest backup and import Statuses.
14+
515
## 0.8.0
616

717
### Added

internal/model.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ type Error struct {
3636
Status *string `json:"status,omitempty"`
3737
}
3838

39-
func (o Error) String() string {
40-
return ToString(o)
39+
func (e Error) String() string {
40+
return ToString(e)
4141
}
4242

4343
func (e *Error) StatusCode() string {

internal/service.go

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -115,56 +115,14 @@ func (a *api) waitForTaskToComplete(ctx context.Context, id string) (*Task, erro
115115
a.logger.Println(err)
116116
}))
117117
if err != nil {
118-
return nil, err
118+
return task, err
119119
}
120120

121121
return task, nil
122122
}
123123

124124
func (a *api) WaitForTask(ctx context.Context, id string) (*Task, error) {
125-
var task *Task
126-
notFoundCount := 0
127-
err := retry.Do(
128-
func() error {
129-
var err error
130-
task, err = a.get(ctx, id)
131-
if err != nil {
132-
// An error is a terminal state (any repeated pre-task 404s will have been exhausted by this point)
133-
return nil
134-
}
135-
136-
status := redis.StringValue(task.Status)
137-
138-
if _, ok := processingStates[status]; !ok {
139-
// The task is no longer processing for whatever reason
140-
return nil
141-
}
142-
143-
return fmt.Errorf("task %s not processed yet: %s", id, status)
144-
},
145-
retry.Attempts(math.MaxUint16),
146-
retry.Delay(1*time.Second),
147-
retry.MaxDelay(30*time.Second),
148-
retry.RetryIf(func(err error) bool {
149-
if !retry.IsRecoverable(err) {
150-
return false
151-
}
152-
if _, ok := err.(*taskNotFoundError); ok {
153-
notFoundCount++
154-
if notFoundCount > max404Errors {
155-
return false
156-
}
157-
}
158-
return true
159-
}),
160-
retry.LastErrorOnly(true), retry.Context(ctx), retry.OnRetry(func(_ uint, err error) {
161-
a.logger.Println(err)
162-
}))
163-
if err != nil {
164-
return nil, err
165-
}
166-
167-
return task, nil
125+
return a.waitForTaskToComplete(ctx, id)
168126
}
169127

170128
func (a *api) get(ctx context.Context, id string) (*Task, error) {

latest_backups_test.go

Lines changed: 16 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -57,29 +57,6 @@ func TestGetLatestBackup(t *testing.T) {
5757
]
5858
}`,
5959
),
60-
getRequest(
61-
t,
62-
"/tasks/50ec6172-8475-4ef6-8b3c-d61e688d8fe5",
63-
`{
64-
"taskId": "50ec6172-8475-4ef6-8b3c-d61e688d8fe5",
65-
"commandType": "databaseBackupStatusRequest",
66-
"status": "processing-completed",
67-
"description": "Request processing completed successfully and its resources are now being provisioned / de-provisioned.",
68-
"timestamp": "2024-04-15T09:08:07.537915Z",
69-
"response": {
70-
"resourceId": 51051292,
71-
"additionalResourceId": 12,
72-
"resource": {}
73-
},
74-
"links": [
75-
{
76-
"href": "https://api-staging.qa.redislabs.com/v1/tasks/50ec6172-8475-4ef6-8b3c-d61e688d8fe5",
77-
"type": "GET",
78-
"rel": "self"
79-
}
80-
]
81-
}`,
82-
),
8360
))
8461

8562
subject, err := clientFromTestServer(server, "key", "secret")
@@ -135,29 +112,6 @@ func TestGetFixedLatestBackup(t *testing.T) {
135112
]
136113
}`,
137114
),
138-
getRequest(
139-
t,
140-
"/tasks/ce2cbfea-9b15-4250-a516-f014161a8dd3",
141-
`{
142-
"taskId": "ce2cbfea-9b15-4250-a516-f014161a8dd3",
143-
"commandType": "databaseBackupStatusRequest",
144-
"status": "processing-completed",
145-
"description": "Request processing completed successfully and its resources are now being provisioned / de-provisioned.",
146-
"timestamp": "2024-04-15T09:52:26.101936Z",
147-
"response": {
148-
"resource": {
149-
"status": "success"
150-
}
151-
},
152-
"links": [
153-
{
154-
"href": "https://api-staging.qa.redislabs.com/v1/tasks/ce2cbfea-9b15-4250-a516-f014161a8dd3",
155-
"type": "GET",
156-
"rel": "self"
157-
}
158-
]
159-
}`,
160-
),
161115
))
162116

163117
subject, err := clientFromTestServer(server, "key", "secret")
@@ -230,36 +184,26 @@ func TestGetAALatestBackup(t *testing.T) {
230184
]
231185
}`,
232186
),
233-
getRequest(
234-
t,
235-
"/tasks/ce2cbfea-9b15-4250-a516-f014161a8dd3",
236-
`{
237-
"taskId": "ce2cbfea-9b15-4250-a516-f014161a8dd3",
238-
"commandType": "databaseBackupStatusRequest",
239-
"status": "processing-error",
240-
"description": "Task request failed during processing. See error information for failure details.",
241-
"timestamp": "2024-04-15T09:52:26.101936Z",
242-
"response": {
243-
"error": {
244-
"type": "DATABASE_BACKUP_DISABLED",
245-
"status": "400 BAD_REQUEST",
246-
"description": "Database backup is disabled"
247-
}
248-
},
249-
"links": [
250-
{
251-
"href": "https://api-staging.qa.redislabs.com/v1/tasks/ce2cbfea-9b15-4250-a516-f014161a8dd3",
252-
"type": "GET",
253-
"rel": "self"
254-
}
255-
]
256-
}`,
257-
),
258187
))
259188

260189
subject, err := clientFromTestServer(server, "key", "secret")
261190
require.NoError(t, err)
262191

263-
_, err = subject.LatestBackup.GetActiveActive(context.TODO(), 12, 34, "eu-west-2")
192+
actual, err := subject.LatestBackup.GetActiveActive(context.TODO(), 12, 34, "eu-west-2")
264193
require.NoError(t, err)
194+
195+
assert.Equal(t, &latest_backups.LatestBackupStatus{
196+
CommandType: redis.String("databaseBackupStatusRequest"),
197+
Description: redis.String("Task request failed during processing. See error information for failure details."),
198+
Status: redis.String("processing-error"),
199+
ID: redis.String("ce2cbfea-9b15-4250-a516-f014161a8dd3"),
200+
Response: &latest_backups.Response{
201+
Error: &latest_backups.Error{
202+
Type: redis.String("DATABASE_BACKUP_DISABLED"),
203+
Description: redis.String("Database backup is disabled"),
204+
Status: redis.String("400 BAD_REQUEST"),
205+
},
206+
},
207+
}, actual)
208+
265209
}

latest_imports_test.go

Lines changed: 15 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -61,38 +61,27 @@ func TestGetLatestImportTooEarly(t *testing.T) {
6161
]
6262
}`,
6363
),
64-
getRequest(
65-
t,
66-
"/tasks/1dfd6084-21df-40c6-829c-e9b4790e207e",
67-
`{
68-
"taskId": "1dfd6084-21df-40c6-829c-e9b4790e207e",
69-
"commandType": "databaseImportStatusRequest",
70-
"status": "processing-error",
71-
"description": "Task request failed during processing. See error information for failure details.",
72-
"timestamp": "2024-04-15T10:19:07.331898Z",
73-
"response": {
74-
"error": {
75-
"type": "SUBSCRIPTION_NOT_ACTIVE",
76-
"status": "403 FORBIDDEN",
77-
"description": "Cannot preform any actions for subscription that is not in an active state"
78-
}
79-
},
80-
"links": [
81-
{
82-
"href": "https://api-staging.qa.redislabs.com/v1/tasks/1dfd6084-21df-40c6-829c-e9b4790e207e",
83-
"type": "GET",
84-
"rel": "self"
85-
}
86-
]
87-
}`,
88-
),
8964
))
9065

9166
subject, err := clientFromTestServer(server, "key", "secret")
9267
require.NoError(t, err)
9368

94-
_, err = subject.LatestImport.Get(context.TODO(), 12, 34)
69+
actual, err := subject.LatestImport.Get(context.TODO(), 12, 34)
9570
require.NoError(t, err)
71+
72+
assert.Equal(t, &latest_imports.LatestImportStatus{
73+
CommandType: redis.String("databaseImportStatusRequest"),
74+
Description: redis.String("Task request failed during processing. See error information for failure details."),
75+
Status: redis.String("processing-error"),
76+
ID: redis.String("1dfd6084-21df-40c6-829c-e9b4790e207e"),
77+
Response: &latest_imports.Response{
78+
Error: &latest_imports.Error{
79+
Type: redis.String("SUBSCRIPTION_NOT_ACTIVE"),
80+
Description: redis.String("Cannot preform any actions for subscription that is not in an active state"),
81+
Status: redis.String("403 FORBIDDEN"),
82+
},
83+
},
84+
}, actual)
9685
}
9786

9887
func TestGetFixedLatestImport(t *testing.T) {
@@ -143,31 +132,6 @@ func TestGetFixedLatestImport(t *testing.T) {
143132
]
144133
}`,
145134
),
146-
getRequest(
147-
t,
148-
"/tasks/e9232e43-3781-4263-a38e-f4d150e03475",
149-
`{
150-
"taskId": "e9232e43-3781-4263-a38e-f4d150e03475",
151-
"commandType": "databaseImportStatusRequest",
152-
"status": "processing-completed",
153-
"description": "Request processing completed successfully and its resources are now being provisioned / de-provisioned.",
154-
"timestamp": "2024-04-15T10:44:35.225468Z",
155-
"response": {
156-
"resourceId": 51051302,
157-
"additionalResourceId": 110777,
158-
"resource": {
159-
"status": "importing"
160-
}
161-
},
162-
"links": [
163-
{
164-
"href": "https://api-staging.qa.redislabs.com/v1/tasks/e9232e43-3781-4263-a38e-f4d150e03475",
165-
"type": "GET",
166-
"rel": "self"
167-
}
168-
]
169-
}`,
170-
),
171135
))
172136

173137
subject, err := clientFromTestServer(server, "key", "secret")
@@ -251,43 +215,6 @@ func TestGetLatestImport(t *testing.T) {
251215
]
252216
}`,
253217
),
254-
getRequest(
255-
t,
256-
"/tasks/e9232e43-3781-4263-a38e-f4d150e03475",
257-
`{
258-
"taskId": "e9232e43-3781-4263-a38e-f4d150e03475",
259-
"commandType": "databaseImportStatusRequest",
260-
"status": "processing-completed",
261-
"description": "Request processing completed successfully and its resources are now being provisioned / de-provisioned.",
262-
"timestamp": "2024-04-15T10:44:35.225468Z",
263-
"response": {
264-
"resourceId": 51051302,
265-
"additionalResourceId": 110777,
266-
"resource": {
267-
"failureReason": "file-corrupted",
268-
"failureReasonParams": [
269-
{
270-
"key": "bytes_configured_bdb_limit",
271-
"value": "1234"
272-
},
273-
{
274-
"key": "bytes_of_expected_dataset",
275-
"value": "5678"
276-
}
277-
],
278-
"lastImportTime": "2024-05-21T10:36:26Z",
279-
"status": "failed"
280-
}
281-
},
282-
"links": [
283-
{
284-
"href": "https://api-staging.qa.redislabs.com/v1/tasks/e9232e43-3781-4263-a38e-f4d150e03475",
285-
"type": "GET",
286-
"rel": "self"
287-
}
288-
]
289-
}`,
290-
),
291218
))
292219

293220
subject, err := clientFromTestServer(server, "key", "secret")

0 commit comments

Comments
 (0)