@@ -2,71 +2,13 @@ package redis
22
33import (
44 "context"
5- "encoding/json"
65 "fmt"
76 "strconv"
87 "time"
98
10- "github.com/cschleiden/go-workflows/backend/history"
11- "github.com/cschleiden/go-workflows/core"
129 redis "github.com/redis/go-redis/v9"
1310)
1411
15- // Adds an event to be delivered in the future. Not cluster-safe.
16- // KEYS[1] - future event zset key
17- // KEYS[2] - future event key
18- // KEYS[3] - instance payload key
19- // ARGV[1] - timestamp/score for set
20- // ARGV[2] - Instance segment
21- // ARGV[3] - event id
22- // ARGV[4] - event data
23- // ARGV[5] - event payload
24- var addFutureEventCmd = redis .NewScript (`
25- redis.call("ZADD", KEYS[1], ARGV[1], KEYS[2])
26- redis.call("HSET", KEYS[2], "instance", ARGV[2], "id", ARGV[3], "event", ARGV[4])
27- redis.call("HSETNX", KEYS[3], ARGV[3], ARGV[5])
28- return 0
29- ` )
30-
31- func addFutureEventP (ctx context.Context , p redis.Pipeliner , instance * core.WorkflowInstance , event * history.Event ) error {
32- eventData , err := marshalEventWithoutAttributes (event )
33- if err != nil {
34- return err
35- }
36-
37- payloadEventData , err := json .Marshal (event .Attributes )
38- if err != nil {
39- return err
40- }
41-
42- return addFutureEventCmd .Run (
43- ctx , p ,
44- []string {futureEventsKey (), futureEventKey (instance , event .ScheduleEventID ), payloadKey (instance )},
45- strconv .FormatInt (event .VisibleAt .UnixMilli (), 10 ),
46- instanceSegment (instance ),
47- event .ID ,
48- string (eventData ),
49- string (payloadEventData ),
50- ).Err ()
51- }
52-
53- // Remove a scheduled future event. Not cluster-safe.
54- // KEYS[1] - future event zset key
55- // KEYS[2] - future event key
56- // KEYS[3] - instance payload key
57- var removeFutureEventCmd = redis .NewScript (`
58- redis.call("ZREM", KEYS[1], KEYS[2])
59- local eventID = redis.call("HGET", KEYS[2], "id")
60- redis.call("HDEL", KEYS[3], eventID)
61- return redis.call("DEL", KEYS[2])
62- ` )
63-
64- // removeFutureEvent removes a scheduled future event for the given event. Events are associated via their ScheduleEventID
65- func removeFutureEventP (ctx context.Context , p redis.Pipeliner , instance * core.WorkflowInstance , event * history.Event ) {
66- key := futureEventKey (instance , event .ScheduleEventID )
67- removeFutureEventCmd .Run (ctx , p , []string {futureEventsKey (), key , payloadKey (instance )})
68- }
69-
7012// Find all due future events. For each event:
7113// - Look up event data
7214// - Add to pending event stream for workflow instance
0 commit comments