Skip to content

Commit

Permalink
Update deserialization code for content of the retry queue.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefannegrea committed Jan 25, 2021
1 parent e57b417 commit 4315594
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 48 deletions.
14 changes: 2 additions & 12 deletions api_retries.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,8 @@ func (s *apiServer) Retries(w http.ResponseWriter, req *http.Request) {

// Retries stores retry information
type Retries struct {
TotalRetryCount int64 `json:"total_retry_count"`
RetryJobs []RetryJobStats `json:"retry_jobs"`
}

// RetryJobStats stores information about a single retry job
type RetryJobStats struct {
Class string `json:"class"`
ErrorMessage string `json:"error_message"`
FailedAt string `json:"failed_at"`
JobID string `json:"jid"`
Queue string `json:"queue"`
RetryCount int64 `json:"retry_count"`
TotalRetryCount int64 `json:"total_retry_count"`
RetryJobs []*Msg `json:"retry_jobs"`
}

func parseURLQuery(req *http.Request) (uint64, int64, string, error) {
Expand Down
41 changes: 29 additions & 12 deletions api_retries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package workers

import (
"context"
"encoding/json"
"errors"
"log"
"net/http/httptest"
Expand All @@ -26,6 +27,7 @@ func TestRetries_NotEmpty(t *testing.T) {
logger: log.New(os.Stdout, "go-workers2: ", log.Ldate|log.Lmicroseconds),
}

// test API replies without registered workers
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/retries", nil)
a.Retries(recorder, request)
Expand All @@ -34,6 +36,23 @@ func TestRetries_NotEmpty(t *testing.T) {

ctx := context.Background()

// test API replies with registered workers
opts, err := setupTestOptionsWithNamespace("prod")
assert.NoError(t, err)

mgr := &Manager{opts: opts}
a.registerManager(mgr)

recorder = httptest.NewRecorder()
request = httptest.NewRequest("GET", "/retries", nil)
a.Retries(recorder, request)

actualWithManagerBytes := recorder.Body.Bytes()
actualReplyParsed := []*Retries{}
err = json.Unmarshal(actualWithManagerBytes, &actualReplyParsed)
assert.NoError(t, err)
assert.Equal(t, []*Retries{{}}, actualReplyParsed)

//puts messages in retry queue when they fail
message, _ := NewMsg("{\"jid\":\"2\",\"retry\":true}")

Expand All @@ -52,24 +71,22 @@ func TestRetries_NotEmpty(t *testing.T) {
},
},
}
for _, tt := range tests {
opts, err := setupTestOptionsWithNamespace("prod")
assert.NoError(t, err)

mgr := &Manager{opts: opts}

a.registerManager(mgr)

ctx = context.Background()
for index, test := range tests {
// Test panic
wares.build("myqueue", mgr, tt.f)(message)
wares.build("myqueue", mgr, test.f)(message)

retries, _ := opts.client.ZRange(ctx, retryQueue(opts.Namespace), 0, 1).Result()
assert.Len(t, retries, 1)
assert.Equal(t, message.ToJson(), retries[0])
retries, err := opts.client.ZRange(ctx, retryQueue(opts.Namespace), 0, -1).Result()
assert.NoError(t, err)
assert.Len(t, retries, index+1)
assert.Equal(t, message.ToJson(), retries[index])
}

recorder = httptest.NewRecorder()
request = httptest.NewRequest("GET", "/retries", nil)
a.Retries(recorder, request)

assert.NoError(t, err)
assert.NotEqual(t, "[]\n", recorder.Body.String())
assert.NotEqual(t, string(actualWithManagerBytes), recorder.Body.String())
}
32 changes: 8 additions & 24 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,35 +233,19 @@ func (m *Manager) GetRetries(page uint64, pageSize int64, match string) (Retries
return Retries{}, err
}

retryJobs, err := getRetryJSON(storeRetries.RetryJobs)
if err != nil {
return Retries{}, err
}

return Retries{
TotalRetryCount: storeRetries.TotalRetryCount,
RetryJobs: retryJobs,
}, nil
}

func getRetryJSON(retryJobs []string) ([]RetryJobStats, error) {
var retryJobStats []RetryJobStats
for _, r := range retryJobs {
var retryJobs []*Msg
for _, r := range storeRetries.RetryJobs {
// parse json from string of retry data
retryJob, err := NewMsg(r)
if err != nil {
return nil, err
return Retries{}, err
}

retryJobStats = append(retryJobStats, RetryJobStats{
Class: retryJob.Get("class").MustString(),
ErrorMessage: retryJob.Get("error_message").MustString(),
FailedAt: retryJob.Get("failed_at").MustString(),
JobID: retryJob.Get("jid").MustString(),
Queue: retryJob.Get("queue").MustString(),
RetryCount: retryJob.Get("retry_count").MustInt64(),
})
retryJobs = append(retryJobs, retryJob)
}

return retryJobStats, nil
return Retries{
TotalRetryCount: storeRetries.TotalRetryCount,
RetryJobs: retryJobs,
}, nil
}

0 comments on commit 4315594

Please sign in to comment.