Skip to content
Open
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
22 changes: 22 additions & 0 deletions enqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ type EnqueueData struct {
EnqueueOptions
}

type EnqueueDataProxy EnqueueData

func (e EnqueueData) MarshalJSON() ([]byte, error) {
o := e.EnqueueOptions
if e.EnqueueOptions.RetryCount > 0 {
s := struct {
EnqueueDataProxy
Retry int `json:"retry,omitempty"`
RetryCount int `json:"retry_count"`
At float64 `json:"at,omitempty"`
}{EnqueueDataProxy(e), o.RetryCount, 0, o.At}
return json.Marshal(s)
}

return json.Marshal(struct {
EnqueueDataProxy
RetryCount int `json:"retry_count,omitempty"`
Retry bool `json:"retry,omitempty"`
At float64 `json:"at,omitempty"`
}{EnqueueDataProxy(e), o.RetryCount, o.Retry, o.At})
}

type EnqueueOptions struct {
RetryCount int `json:"retry_count,omitempty"`
Retry bool `json:"retry,omitempty"`
Expand Down
22 changes: 17 additions & 5 deletions enqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,23 @@ func EnqueueSpec(c gospec.Context) {
c.Expect(ea, IsWithin(0.1), nowToSecondsWithNanoPrecision())
})

c.Specify("has retry and retry_count when set", func() {
EnqueueWithOptions("enqueue6", "Compare", []string{"foo", "bar"}, EnqueueOptions{RetryCount: 13, Retry: true})
c.Specify("sets retry count to `retry`", func() {
EnqueueWithOptions("enqueue6", "Compare", []string{"foo", "bar"}, EnqueueOptions{RetryCount: 13})

bytes, _ := redis.Bytes(conn.Do("lpop", "prod:queue:enqueue6"))
var result map[string]interface{}
json.Unmarshal(bytes, &result)
c.Expect(result["class"], Equals, "Compare")

retry := result["retry"].(float64)
c.Expect(retry, Equals, float64(13))

retryCount := result["retry_count"].(float64)
c.Expect(retryCount, Equals, float64(0))
})

c.Specify("sets Retry correctly when no count given", func() {
EnqueueWithOptions("enqueue6", "Compare", []string{"foo", "bar"}, EnqueueOptions{Retry: true})

bytes, _ := redis.Bytes(conn.Do("lpop", "prod:queue:enqueue6"))
var result map[string]interface{}
Expand All @@ -82,9 +97,6 @@ func EnqueueSpec(c gospec.Context) {

retry := result["retry"].(bool)
c.Expect(retry, Equals, true)

retryCount := int(result["retry_count"].(float64))
c.Expect(retryCount, Equals, 13)
})
})

Expand Down