@@ -2,7 +2,9 @@ package redis
2
2
3
3
import (
4
4
"context"
5
+ "embed"
5
6
"fmt"
7
+ "io/fs"
6
8
"log/slog"
7
9
"time"
8
10
@@ -19,6 +21,11 @@ import (
19
21
20
22
var _ backend.Backend = (* redisBackend )(nil )
21
23
24
+ //go:embed scripts/*.lua
25
+ var luaScripts embed.FS
26
+
27
+ var completeWorkflowTaskCmd * redis.Script
28
+
22
29
func NewRedisBackend (client redis.UniversalClient , opts ... RedisBackendOption ) (* redisBackend , error ) {
23
30
workflowQueue , err := newTaskQueue [any ](client , "workflows" )
24
31
if err != nil {
@@ -52,15 +59,13 @@ func NewRedisBackend(client redis.UniversalClient, opts ...RedisBackendOption) (
52
59
// them, loads them. This doesn't work when using (transactional) pipelines, so eagerly load them on startup.
53
60
ctx := context .Background ()
54
61
cmds := map [string ]* redis.StringCmd {
55
- "addEventsToStreamCmd" : addEventsToStreamCmd .Load (ctx , rb .rdb ),
56
- "addFutureEventCmd" : addFutureEventCmd .Load (ctx , rb .rdb ),
57
- "futureEventsCmd" : futureEventsCmd .Load (ctx , rb .rdb ),
58
- "removeFutureEventCmd" : removeFutureEventCmd .Load (ctx , rb .rdb ),
59
- "removePendingEventsCmd" : removePendingEventsCmd .Load (ctx , rb .rdb ),
60
- "requeueInstanceCmd" : requeueInstanceCmd .Load (ctx , rb .rdb ),
61
- "deleteInstanceCmd" : deleteCmd .Load (ctx , rb .rdb ),
62
- "expireInstanceCmd" : expireCmd .Load (ctx , rb .rdb ),
63
- "addPayloadsCmd" : addPayloadsCmd .Load (ctx , rb .rdb ),
62
+ "addEventsToStreamCmd" : addEventsToStreamCmd .Load (ctx , rb .rdb ),
63
+ "addFutureEventCmd" : addFutureEventCmd .Load (ctx , rb .rdb ),
64
+ "futureEventsCmd" : futureEventsCmd .Load (ctx , rb .rdb ),
65
+ "removeFutureEventCmd" : removeFutureEventCmd .Load (ctx , rb .rdb ),
66
+ "deleteInstanceCmd" : deleteCmd .Load (ctx , rb .rdb ),
67
+ "expireInstanceCmd" : expireCmd .Load (ctx , rb .rdb ),
68
+ "addPayloadsCmd" : addPayloadsCmd .Load (ctx , rb .rdb ),
64
69
}
65
70
for name , cmd := range cmds {
66
71
// fmt.Println(name, cmd.Val())
@@ -70,6 +75,25 @@ func NewRedisBackend(client redis.UniversalClient, opts ...RedisBackendOption) (
70
75
}
71
76
}
72
77
78
+ // Load all Lua scripts
79
+
80
+ cmdMapping := map [string ]* * redis.Script {
81
+ "complete_workflow_task.lua" : & completeWorkflowTaskCmd ,
82
+ }
83
+
84
+ for scriptFile , cmd := range cmdMapping {
85
+ scriptContent , err := fs .ReadFile (luaScripts , "scripts/" + scriptFile )
86
+ if err != nil {
87
+ return nil , fmt .Errorf ("reading Lua script %s: %w" , scriptFile , err )
88
+ }
89
+
90
+ * cmd = redis .NewScript (string (scriptContent ))
91
+
92
+ if c := (* cmd ).Load (ctx , rb .rdb ); c .Err () != nil {
93
+ return nil , fmt .Errorf ("loading Lua script %s: %w" , scriptFile , c .Err ())
94
+ }
95
+ }
96
+
73
97
return rb , nil
74
98
}
75
99
0 commit comments