Skip to content

Commit 3fae335

Browse files
Derk SchooltinkDerkSch
authored andcommitted
swap glide SDK for valkey-go SDK
1 parent 55280f0 commit 3fae335

15 files changed

Lines changed: 260 additions & 301 deletions

backend/redis/redis.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
var _ backend.Backend = (*redisBackend)(nil)
2020

2121
//go:embed scripts
22-
var luaScripts embed.FS
22+
var Luas embed.FS
2323

2424
var (
2525
createWorkflowInstanceCmd *redis.Script
@@ -85,7 +85,7 @@ func NewRedisBackend(client redis.UniversalClient, opts ...RedisBackendOption) (
8585

8686
func loadScripts(ctx context.Context, rdb redis.UniversalClient, cmdMapping map[string]**redis.Script) error {
8787
for scriptFile, cmd := range cmdMapping {
88-
scriptContent, err := fs.ReadFile(luaScripts, "scripts/"+scriptFile)
88+
scriptContent, err := fs.ReadFile(Luas, "scripts/"+scriptFile)
8989
if err != nil {
9090
return fmt.Errorf("reading Lua script %s: %w", scriptFile, err)
9191
}

backend/valkey/activity.go

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/cschleiden/go-workflows/backend"
88
"github.com/cschleiden/go-workflows/backend/history"
99
"github.com/cschleiden/go-workflows/workflow"
10-
"github.com/valkey-io/valkey-glide/go/v2/options"
1110
)
1211

1312
func (vb *valkeyBackend) PrepareActivityQueues(ctx context.Context, queues []workflow.Queue) error {
@@ -56,26 +55,23 @@ func (vb *valkeyBackend) CompleteActivityTask(ctx context.Context, task *backend
5655
activityQueueKeys := vb.activityQueue.Keys(task.Queue)
5756
workflowQueueKeys := vb.workflowQueue.Keys(workflow.Queue(instance.Queue))
5857

59-
_, err = vb.client.InvokeScriptWithOptions(ctx, completeActivityTaskScript, options.ScriptOptions{
60-
Keys: []string{
61-
activityQueueKeys.SetKey,
62-
activityQueueKeys.StreamKey,
63-
vb.keys.pendingEventsKey(task.WorkflowInstance),
64-
vb.keys.payloadKey(task.WorkflowInstance),
65-
vb.workflowQueue.queueSetKey,
66-
workflowQueueKeys.SetKey,
67-
workflowQueueKeys.StreamKey,
68-
},
69-
Args: []string{
70-
task.ID,
71-
vb.activityQueue.groupName,
72-
result.ID,
73-
eventData,
74-
payload,
75-
vb.workflowQueue.groupName,
76-
instanceSegment(task.WorkflowInstance),
77-
},
78-
})
58+
err = completeActivityTaskScript.Exec(ctx, vb.client, []string{
59+
activityQueueKeys.SetKey,
60+
activityQueueKeys.StreamKey,
61+
vb.keys.pendingEventsKey(task.WorkflowInstance),
62+
vb.keys.payloadKey(task.WorkflowInstance),
63+
vb.workflowQueue.queueSetKey,
64+
workflowQueueKeys.SetKey,
65+
workflowQueueKeys.StreamKey,
66+
}, []string{
67+
task.ID,
68+
vb.activityQueue.groupName,
69+
result.ID,
70+
eventData,
71+
payload,
72+
vb.workflowQueue.groupName,
73+
instanceSegment(task.WorkflowInstance),
74+
}).Error()
7975

8076
if err != nil {
8177
return fmt.Errorf("completing activity task: %w", err)

backend/valkey/delete.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,23 @@ import (
55
"fmt"
66

77
"github.com/cschleiden/go-workflows/core"
8-
"github.com/valkey-io/valkey-glide/go/v2/options"
98
)
109

1110
// deleteInstance deletes an instance from Valkey. It does not attempt to remove any future events or pending
1211
// workflow tasks. It's assumed that the instance is in the finished state.
1312
//
1413
// Note: might want to revisit this in the future if we want to support removing hung instances.
1514
func (vb *valkeyBackend) deleteInstance(ctx context.Context, instance *core.WorkflowInstance) error {
16-
_, err := vb.client.InvokeScriptWithOptions(ctx, deleteInstanceScript, options.ScriptOptions{
17-
Keys: []string{
18-
vb.keys.instanceKey(instance),
19-
vb.keys.pendingEventsKey(instance),
20-
vb.keys.historyKey(instance),
21-
vb.keys.payloadKey(instance),
22-
vb.keys.activeInstanceExecutionKey(instance.InstanceID),
23-
vb.keys.instancesByCreation(),
24-
},
25-
Args: []string{instanceSegment(instance)},
26-
})
15+
err := deleteInstanceScript.Exec(ctx, vb.client, []string{
16+
vb.keys.instanceKey(instance),
17+
vb.keys.pendingEventsKey(instance),
18+
vb.keys.historyKey(instance),
19+
vb.keys.payloadKey(instance),
20+
vb.keys.activeInstanceExecutionKey(instance.InstanceID),
21+
vb.keys.instancesByCreation(),
22+
}, []string{
23+
instanceSegment(instance),
24+
}).Error()
2725

2826
if err != nil {
2927
return fmt.Errorf("failed to delete instance: %w", err)

backend/valkey/diagnostics.go

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,32 @@ import (
88
"github.com/cschleiden/go-workflows/core"
99
"github.com/cschleiden/go-workflows/diag"
1010
"github.com/cschleiden/go-workflows/internal/log"
11-
"github.com/valkey-io/valkey-glide/go/v2/constants"
12-
"github.com/valkey-io/valkey-glide/go/v2/options"
1311
)
1412

1513
var _ diag.Backend = (*valkeyBackend)(nil)
1614

1715
func (vb *valkeyBackend) GetWorkflowInstances(ctx context.Context, afterInstanceID, afterExecutionID string, count int) ([]*diag.WorkflowInstanceRef, error) {
18-
start := options.NewInclusiveScoreBoundary(0)
19-
end := options.NewInfiniteScoreBoundary(constants.PositiveInfinity)
20-
21-
zrangeInput := &options.RangeByScore{
22-
Start: start,
23-
End: end,
24-
Reverse: true,
25-
Limit: &options.Limit{
26-
Offset: 0,
27-
Count: int64(count),
28-
},
29-
}
16+
zrangeCmd := vb.client.B().Zrange().Key(vb.keys.instancesByCreation()).Min("0").Max("-1").Rev().Limit(0, int64(count))
3017

3118
if afterInstanceID != "" {
3219
afterSegmentID := instanceSegment(core.NewWorkflowInstance(afterInstanceID, afterExecutionID))
33-
scores, err := vb.client.ZMScore(ctx, vb.keys.instancesByCreation(), []string{afterSegmentID})
20+
scores, err := vb.client.Do(ctx, vb.client.B().Zscore().Key(vb.keys.instancesByCreation()).Member(afterSegmentID).Build()).AsFloat64()
3421
if err != nil {
3522
return nil, fmt.Errorf("getting instance score for %v: %w", afterSegmentID, err)
3623
}
3724

38-
if len(scores) == 0 {
25+
if scores == 0 {
3926
vb.Options().Logger.Error("could not find instance %v",
4027
log.NamespaceKey+".valkey.afterInstanceID", afterInstanceID,
4128
log.NamespaceKey+".valkey.afterExecutionID", afterExecutionID,
4229
)
4330
return nil, nil
4431
}
4532

46-
end := options.NewScoreBoundary(scores[0].Value(), false)
47-
zrangeInput.End = end
33+
zrangeCmd = vb.client.B().Zrange().Key(vb.keys.instancesByCreation()).Min("-inf").Max(fmt.Sprintf("(%f", scores)).Rev().Limit(0, int64(count))
4834
}
4935

50-
instanceSegments, err := vb.client.ZRange(ctx, vb.keys.instancesByCreation(), zrangeInput)
36+
instanceSegments, err := vb.client.Do(ctx, zrangeCmd.Build()).AsStrSlice()
5137
if err != nil {
5238
return nil, fmt.Errorf("getting instances: %w", err)
5339
}
@@ -61,19 +47,20 @@ func (vb *valkeyBackend) GetWorkflowInstances(ctx context.Context, afterInstance
6147
instanceKeys = append(instanceKeys, vb.keys.instanceKeyFromSegment(r))
6248
}
6349

64-
instances, err := vb.client.MGet(ctx, instanceKeys)
50+
cmd := vb.client.B().Mget().Key(instanceKeys...)
51+
instances, err := vb.client.Do(ctx, cmd.Build()).AsStrSlice()
6552
if err != nil {
6653
return nil, fmt.Errorf("getting instances: %w", err)
6754
}
6855

6956
instanceRefs := make([]*diag.WorkflowInstanceRef, 0, len(instances))
7057
for _, instance := range instances {
71-
if instance.IsNil() {
58+
if instance == "" {
7259
continue
7360
}
7461

7562
var state instanceState
76-
if err := json.Unmarshal([]byte(instance.Value()), &state); err != nil {
63+
if err := json.Unmarshal([]byte(instance), &state); err != nil {
7764
return nil, fmt.Errorf("unmarshaling instance state: %w", err)
7865
}
7966

backend/valkey/events_future.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,12 @@ import (
55
"fmt"
66
"strconv"
77
"time"
8-
9-
"github.com/valkey-io/valkey-glide/go/v2/options"
108
)
119

1210
func scheduleFutureEvents(ctx context.Context, vb *valkeyBackend) error {
1311
now := time.Now().UnixMilli()
1412
nowStr := strconv.FormatInt(now, 10)
15-
_, err := vb.client.InvokeScriptWithOptions(ctx, futureEventsScript, options.ScriptOptions{
16-
Keys: []string{
17-
vb.keys.futureEventsKey(),
18-
},
19-
Args: []string{nowStr, vb.keys.prefix},
20-
})
13+
err := futureEventsScript.Exec(ctx, vb.client, []string{vb.keys.futureEventsKey()}, []string{nowStr, vb.keys.prefix}).Error()
2114

2215
if err != nil {
2316
return fmt.Errorf("checking future events: %w", err)

backend/valkey/expire.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"time"
88

99
"github.com/cschleiden/go-workflows/core"
10-
"github.com/valkey-io/valkey-glide/go/v2/options"
1110
)
1211

1312
func (vb *valkeyBackend) setWorkflowInstanceExpiration(ctx context.Context, instance *core.WorkflowInstance, expiration time.Duration) error {
@@ -17,22 +16,19 @@ func (vb *valkeyBackend) setWorkflowInstanceExpiration(ctx context.Context, inst
1716
exp := time.Now().Add(expiration).UnixMilli()
1817
expStr := strconv.FormatInt(exp, 10)
1918

20-
_, err := vb.client.InvokeScriptWithOptions(ctx, expireWorkflowInstanceScript, options.ScriptOptions{
21-
Keys: []string{
22-
vb.keys.instancesByCreation(),
23-
vb.keys.instancesExpiring(),
24-
vb.keys.instanceKey(instance),
25-
vb.keys.pendingEventsKey(instance),
26-
vb.keys.historyKey(instance),
27-
vb.keys.payloadKey(instance),
28-
},
29-
Args: []string{
30-
nowStr,
31-
fmt.Sprintf("%.0f", expiration.Seconds()),
32-
expStr,
33-
instanceSegment(instance),
34-
},
35-
})
19+
err := expireWorkflowInstanceScript.Exec(ctx, vb.client, []string{
20+
vb.keys.instancesByCreation(),
21+
vb.keys.instancesExpiring(),
22+
vb.keys.instanceKey(instance),
23+
vb.keys.pendingEventsKey(instance),
24+
vb.keys.historyKey(instance),
25+
vb.keys.payloadKey(instance),
26+
}, []string{
27+
nowStr,
28+
fmt.Sprintf("%.0f", expiration.Seconds()),
29+
expStr,
30+
instanceSegment(instance),
31+
}).Error()
3632

3733
return err
3834
}

0 commit comments

Comments
 (0)