@@ -34,31 +34,29 @@ func marshalEventWithoutAttributes(event *history.Event) (string, error) {
34
34
return string (data ), nil
35
35
}
36
36
37
- // KEYS[1..n] - payload keys
37
+ // KEYS[1 - payload key
38
38
// ARGV[1..n] - payload values
39
39
var addPayloadsCmd = redis .NewScript (`
40
- for i = 1, #ARGV do
41
- redis.pcall("SET ", KEYS[i ], ARGV[i], "NX" )
40
+ for i = 1, #ARGV, 2 do
41
+ redis.pcall("HSETNX ", KEYS[1 ], ARGV[i], ARGV[i+1] )
42
42
end
43
43
44
44
return 0
45
45
` )
46
46
47
- func addEventPayloads (ctx context.Context , p redis.Pipeliner , events []* history.Event ) error {
48
- keys := make ([]string , 0 )
49
- values := make ([]interface {}, 0 )
47
+ func addEventPayloadsP (ctx context.Context , p redis.Pipeliner , instance * core.WorkflowInstance , events []* history.Event ) error {
48
+ args := make ([]interface {}, 0 )
50
49
51
50
for _ , event := range events {
52
51
payload , err := json .Marshal (event .Attributes )
53
52
if err != nil {
54
53
return fmt .Errorf ("marshaling event payload: %w" , err )
55
54
}
56
55
57
- keys = append (keys , payloadKey (event .ID ))
58
- values = append (values , string (payload ))
56
+ args = append (args , event .ID , string (payload ))
59
57
}
60
58
61
- return addPayloadsCmd .Run (ctx , p , keys , values ... ).Err ()
59
+ return addPayloadsCmd .Run (ctx , p , [] string { payloadKey ( instance )}, args ... ).Err ()
62
60
}
63
61
64
62
func addEventToStreamP (ctx context.Context , p redis.Pipeliner , streamKey string , event * history.Event ) error {
@@ -110,15 +108,16 @@ func addEventsToStreamP(ctx context.Context, p redis.Pipeliner, streamKey string
110
108
// Adds an event to be delivered in the future. Not cluster-safe.
111
109
// KEYS[1] - future event zset key
112
110
// KEYS[2] - future event key
113
- // KEYS[3] - future event payload key
111
+ // KEYS[3] - instance payload key
114
112
// ARGV[1] - timestamp
115
113
// ARGV[2] - Instance segment
116
- // ARGV[3] - event data
117
- // ARGV[4] - event payload
114
+ // ARGV[3] - event id
115
+ // ARGV[4] - event data
116
+ // ARGV[5] - event payload
118
117
var addFutureEventCmd = redis .NewScript (`
119
118
redis.call("ZADD", KEYS[1], ARGV[1], KEYS[2])
120
- redis.call("HSET", KEYS[2], "instance", ARGV[2], "event ", ARGV[3], "payload ", KEYS[3 ])
121
- redis.call("SET ", KEYS[3], ARGV[4 ], "NX" )
119
+ redis.call("HSET", KEYS[2], "instance", ARGV[2], "id ", ARGV[3], "event ", ARGV[4 ])
120
+ redis.call("HSETNX ", KEYS[3], ARGV[3 ], ARGV[5] )
122
121
return 0
123
122
` )
124
123
@@ -135,9 +134,10 @@ func addFutureEventP(ctx context.Context, p redis.Pipeliner, instance *core.Work
135
134
136
135
return addFutureEventCmd .Run (
137
136
ctx , p ,
138
- []string {futureEventsKey (), futureEventKey (instance , event .ScheduleEventID ), payloadKey (event . ID )},
137
+ []string {futureEventsKey (), futureEventKey (instance , event .ScheduleEventID ), payloadKey (instance )},
139
138
strconv .FormatInt (event .VisibleAt .UnixMilli (), 10 ),
140
139
instanceSegment (instance ),
140
+ event .ID ,
141
141
string (eventData ),
142
142
string (payloadEventData ),
143
143
).Err ()
@@ -146,15 +146,16 @@ func addFutureEventP(ctx context.Context, p redis.Pipeliner, instance *core.Work
146
146
// Remove a scheduled future event. Not cluster-safe.
147
147
// KEYS[1] - future event zset key
148
148
// KEYS[2] - future event key
149
+ // KEYS[3] - instance payload key
149
150
var removeFutureEventCmd = redis .NewScript (`
150
151
redis.call("ZREM", KEYS[1], KEYS[2])
151
- local k = redis.call("HGET", KEYS[2], "payload ")
152
- redis.call("DEL ", k )
152
+ local eventID = redis.call("HGET", KEYS[2], "id ")
153
+ redis.call("HDEL ", KEYS[3], eventID )
153
154
return redis.call("DEL", KEYS[2])
154
155
` )
155
156
156
157
// removeFutureEvent removes a scheduled future event for the given event. Events are associated via their ScheduleEventID
157
158
func removeFutureEventP (ctx context.Context , p redis.Pipeliner , instance * core.WorkflowInstance , event * history.Event ) {
158
159
key := futureEventKey (instance , event .ScheduleEventID )
159
- removeFutureEventCmd .Run (ctx , p , []string {futureEventsKey (), key })
160
+ removeFutureEventCmd .Run (ctx , p , []string {futureEventsKey (), key , payloadKey ( instance ) })
160
161
}
0 commit comments