@@ -9,11 +9,12 @@ import (
9
9
"github.com/cschleiden/go-workflows/backend"
10
10
"github.com/cschleiden/go-workflows/internal/core"
11
11
"github.com/cschleiden/go-workflows/internal/history"
12
+ "github.com/cschleiden/go-workflows/workflow"
12
13
"github.com/go-redis/redis/v8"
13
14
)
14
15
15
- func (rb * redisBackend ) CreateWorkflowInstance (ctx context.Context , event history.WorkflowEvent ) error {
16
- state , err := readInstance (ctx , rb .rdb , event . WorkflowInstance .InstanceID )
16
+ func (rb * redisBackend ) CreateWorkflowInstance (ctx context.Context , instance * workflow. Instance , metadata * workflow. Metadata , event history.Event ) error {
17
+ state , err := readInstance (ctx , rb .rdb , instance .InstanceID )
17
18
if err != nil && err != backend .ErrInstanceNotFound {
18
19
return err
19
20
}
@@ -24,29 +25,27 @@ func (rb *redisBackend) CreateWorkflowInstance(ctx context.Context, event histor
24
25
25
26
p := rb .rdb .TxPipeline ()
26
27
27
- if err := createInstanceP (ctx , p , event . WorkflowInstance , false ); err != nil {
28
+ if err := createInstanceP (ctx , p , instance , metadata , false ); err != nil {
28
29
return err
29
30
}
30
31
31
32
// Create event stream
32
- eventData , err := json .Marshal (event . HistoryEvent )
33
+ eventData , err := json .Marshal (event )
33
34
if err != nil {
34
35
return err
35
36
}
36
37
37
38
p .XAdd (ctx , & redis.XAddArgs {
38
- Stream : pendingEventsKey (event . WorkflowInstance .InstanceID ),
39
+ Stream : pendingEventsKey (instance .InstanceID ),
39
40
ID : "*" ,
40
41
Values : map [string ]interface {}{
41
42
"event" : string (eventData ),
42
43
},
43
44
})
44
45
45
46
// Queue workflow instance task
46
- if err := rb .workflowQueue .Enqueue (ctx , p , event .WorkflowInstance .InstanceID , nil ); err != nil {
47
- if err != errTaskAlreadyInQueue {
48
- return fmt .Errorf ("queueing workflow task: %w" , err )
49
- }
47
+ if err := rb .workflowQueue .Enqueue (ctx , p , instance .InstanceID , nil ); err != nil {
48
+ return fmt .Errorf ("queueing workflow task: %w" , err )
50
49
}
51
50
52
51
if _ , err := p .Exec (ctx ); err != nil {
@@ -105,21 +104,26 @@ func (rb *redisBackend) CancelWorkflowInstance(ctx context.Context, instance *co
105
104
}
106
105
107
106
type instanceState struct {
108
- Instance * core.WorkflowInstance `json:"instance,omitempty"`
109
- State backend.WorkflowState `json:"state,omitempty"`
110
- CreatedAt time.Time `json:"created_at,omitempty"`
111
- CompletedAt * time.Time `json:"completed_at,omitempty"`
112
- LastSequenceID int64 `json:"last_sequence_id,omitempty"`
107
+ Instance * core.WorkflowInstance `json:"instance,omitempty"`
108
+ State backend.WorkflowState `json:"state,omitempty"`
109
+
110
+ Metadata * core.WorkflowInstanceMetadata `json:"metadata,omitempty"`
111
+
112
+ CreatedAt time.Time `json:"created_at,omitempty"`
113
+ CompletedAt * time.Time `json:"completed_at,omitempty"`
114
+
115
+ LastSequenceID int64 `json:"last_sequence_id,omitempty"`
113
116
}
114
117
115
- func createInstanceP (ctx context.Context , p redis.Pipeliner , instance * core.WorkflowInstance , ignoreDuplicate bool ) error {
118
+ func createInstanceP (ctx context.Context , p redis.Pipeliner , instance * core.WorkflowInstance , metadata * core. WorkflowInstanceMetadata , ignoreDuplicate bool ) error {
116
119
key := instanceKey (instance .InstanceID )
117
120
118
121
createdAt := time .Now ()
119
122
120
123
b , err := json .Marshal (& instanceState {
121
124
Instance : instance ,
122
125
State : backend .WorkflowStateActive ,
126
+ Metadata : metadata ,
123
127
CreatedAt : createdAt ,
124
128
})
125
129
if err != nil {
0 commit comments