Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
vendor/
vendor/

# GoLand/IntelliJ
.idea

# VSCode workspace settings
.vscode

# macOS
*.DS_Store
26 changes: 15 additions & 11 deletions fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,16 @@ func (f *simpleFetcher) Fetch() {

func (f *simpleFetcher) tryFetchMessage() {
message, err := f.client.BRPopLPush(f.queue, f.inprogressQueue(), 1*time.Second).Result()

if err != nil {
// If redis returns null, the queue is empty.
// Just ignore empty queue errors; print all other errors.
if err != redis.Nil {
Logger.Println("ERR: ", f.queue, err)
}

time.Sleep(1 * time.Second)
} else {
switch err {
case nil:
f.sendMessage(message)
case redis.Nil:
// If redis returns null, the queue is empty.
// Just ignore empty queue errors.
time.Sleep(1*time.Second)
default:
// Print all other errors.
Comment thread
requaos marked this conversation as resolved.
Outdated
Logger.Println("ERR: ", f.queue, err)
}
}

Expand All @@ -108,7 +107,12 @@ func (f *simpleFetcher) sendMessage(message string) {
}

func (f *simpleFetcher) Acknowledge(message *Msg) {
f.client.LRem(f.inprogressQueue(), -1, message.OriginalJson()).Result()
count := int64(-1)
val, err := message.Get("unique").Bool()
if err == nil && val {
count = 0
}
f.client.LRem(f.inprogressQueue(), count, message.OriginalJson()).Result()
}

func (f *simpleFetcher) Messages() chan *Msg {
Expand Down
13 changes: 11 additions & 2 deletions middleware_retry.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package workers

import (
"crypto/sha1"
"encoding/hex"
"fmt"
"math"
"math/rand"
Expand Down Expand Up @@ -58,8 +60,15 @@ func RetryMiddleware(queue string, mgr *Manager, next JobFunc) JobFunc {

}()

err = next(message)
if err != nil {
switch next(message) {
case nil:
val, err := message.Get("unique").Bool()
if err == nil && val {
rc := mgr.opts.client

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I would like to avoid using the Redis client directly at this layer. What are the additional store functions that are needed to support this functionality?

As we stated a few years ago, we are moving in the direction of implementing at least one more additional persistent storage engine to go-workers2.

sum := sha1.Sum([]byte(message.Args().ToJson()))
rc.Del(hex.EncodeToString(sum[:])).Result()
}
default:
err = retryProcessError(queue, mgr, message, err)
}

Expand Down
22 changes: 21 additions & 1 deletion producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package workers

import (
"crypto/rand"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -31,6 +33,7 @@ type EnqueueOptions struct {
RetryCount int `json:"retry_count,omitempty"`
Retry bool `json:"retry,omitempty"`
At float64 `json:"at,omitempty"`
Unique bool `json:"unique,omitempty"`
}

func NewProducer(options Options) (*Producer, error) {
Expand Down Expand Up @@ -69,7 +72,25 @@ func (p *Producer) EnqueueAt(queue, class string, at time.Time, args interface{}
return p.EnqueueWithOptions(queue, class, args, EnqueueOptions{At: timeToSecondsWithNanoPrecision(at)})
}

func (p *Producer) EnqueueUnique(queue, class string, at time.Time, args interface{}) (string, error) {
return p.EnqueueWithOptions(queue, class, args, EnqueueOptions{At: nowToSecondsWithNanoPrecision(), Unique: true})
}

func (p *Producer) EnqueueWithOptions(queue, class string, args interface{}, opts EnqueueOptions) (string, error) {
rc := p.opts.client

if opts.Unique {
bytes, err := json.Marshal(args)
if err != nil {
return "", err
}
sum := sha1.Sum(bytes)
if !rc.SetNX(hex.EncodeToString(sum[:]), "unique", 0).Val() {
// already in the list
return "", nil
}
}

now := nowToSecondsWithNanoPrecision()
data := EnqueueData{
Queue: queue,
Expand All @@ -90,7 +111,6 @@ func (p *Producer) EnqueueWithOptions(queue, class string, args interface{}, opt
return data.Jid, err
}

rc := p.opts.client

_, err = rc.SAdd(p.opts.Namespace+"queues", queue).Result()
if err != nil {
Expand Down