/
signature.go
96 lines (84 loc) · 2.53 KB
/
signature.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package tasks
import (
"fmt"
"github.com/RichardKnop/machinery/v1/utils"
"time"
"github.com/google/uuid"
)
// Arg represents a single argument passed to invocation fo a task
type Arg struct {
Name string `bson:"name"`
Type string `bson:"type"`
Value interface{} `bson:"value"`
}
// Headers represents the headers which should be used to direct the task
type Headers map[string]interface{}
// Set on Headers implements opentracing.TextMapWriter for trace propagation
func (h Headers) Set(key, val string) {
h[key] = val
}
// ForeachKey on Headers implements opentracing.TextMapReader for trace propagation.
// It is essentially the same as the opentracing.TextMapReader implementation except
// for the added casting from interface{} to string.
func (h Headers) ForeachKey(handler func(key, val string) error) error {
for k, v := range h {
// Skip any non string values
stringValue, ok := v.(string)
if !ok {
continue
}
if err := handler(k, stringValue); err != nil {
return err
}
}
return nil
}
// Signature represents a single task invocation
type Signature struct {
UUID string
Name string
RoutingKey string
ETA *time.Time
GroupUUID string
GroupTaskCount int
Args []Arg
Headers Headers
Priority uint8
Immutable bool
RetryCount int
RetryTimeout int
OnSuccess []*Signature
OnError []*Signature
ChordCallback *Signature
//MessageGroupId for Broker, e.g. SQS
BrokerMessageGroupId string
//ReceiptHandle of SQS Message
SQSReceiptHandle string
// StopTaskDeletionOnError used with sqs when we want to send failed messages to dlq,
// and don't want machinery to delete from source queue
StopTaskDeletionOnError bool
// IgnoreWhenTaskNotRegistered auto removes the request when there is no handeler available
// When this is true a task with no handler will be ignored and not placed back in the queue
IgnoreWhenTaskNotRegistered bool
}
// NewSignature creates a new task signature
func NewSignature(name string, args []Arg) (*Signature, error) {
signatureID := uuid.New().String()
return &Signature{
UUID: fmt.Sprintf("task_%v", signatureID),
Name: name,
Args: args,
}, nil
}
func CopySignatures(signatures ...*Signature) []*Signature {
var sigs = make([]*Signature, len(signatures))
for index, signature := range signatures {
sigs[index] = CopySignature(signature)
}
return sigs
}
func CopySignature(signature *Signature) *Signature {
var sig = new(Signature)
_ = utils.DeepCopy(sig, signature)
return sig
}