Skip to content
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

Requaos/unique jobs #27

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
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
2 changes: 1 addition & 1 deletion fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (f *simpleFetcher) tryFetchMessage() {
}
} else {
f.sendMessage(message)
}
}
}

func (f *simpleFetcher) sendMessage(message string) {
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 @@ -56,8 +58,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
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
21 changes: 21 additions & 0 deletions 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 @@ -71,7 +74,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 Down