@@ -15,41 +15,60 @@ import (
15
15
)
16
16
17
17
func (rb * redisBackend ) CreateWorkflowInstance (ctx context.Context , instance * workflow.Instance , event * history.Event ) error {
18
- activeInstance , err := readActiveInstanceExecution (ctx , rb .rdb , instance .InstanceID )
19
- if err != nil && err != backend .ErrInstanceNotFound {
20
- return err
21
- }
18
+ keyInfo := rb .workflowQueue .Keys ()
22
19
23
- if activeInstance != nil {
24
- return backend .ErrInstanceAlreadyExists
20
+ instanceState , err := json .Marshal (& instanceState {
21
+ Instance : instance ,
22
+ State : core .WorkflowInstanceStateActive ,
23
+ Metadata : event .Attributes .(* history.ExecutionStartedAttributes ).Metadata ,
24
+ CreatedAt : time .Now (),
25
+ })
26
+ if err != nil {
27
+ return fmt .Errorf ("marshaling instance state: %w" , err )
25
28
}
26
29
27
- p := rb .rdb .TxPipeline ()
28
-
29
- if err := createInstanceP (ctx , p , instance , event .Attributes .(* history.ExecutionStartedAttributes ).Metadata ); err != nil {
30
- return err
30
+ activeInstance , err := json .Marshal (instance )
31
+ if err != nil {
32
+ return fmt .Errorf ("marshaling instance: %w" , err )
31
33
}
32
34
33
- // Create event stream with initial event
34
- if err := addEventPayloadsP ( ctx , p , instance , [] * history. Event { event }); err != nil {
35
- return fmt .Errorf ("adding event payloads : %w" , err )
35
+ eventData , err := marshalEventWithoutAttributes ( event )
36
+ if err != nil {
37
+ return fmt .Errorf ("marshaling event: %w" , err )
36
38
}
37
39
38
- if err := addEventToStreamP (ctx , p , pendingEventsKey (instance ), event ); err != nil {
39
- return fmt .Errorf ("adding event to stream: %w" , err )
40
+ payloadData , err := json .Marshal (event .Attributes )
41
+ if err != nil {
42
+ return fmt .Errorf ("marshaling event payload: %w" , err )
40
43
}
41
44
42
- // Queue workflow instance task
43
- if err := rb .workflowQueue .Enqueue (ctx , p , instanceSegment (instance ), nil ); err != nil {
44
- return fmt .Errorf ("queueing workflow task: %w" , err )
45
- }
45
+ _ , err = createWorkflowInstanceCmd .Run (ctx , rb .rdb , []string {
46
+ instanceKey (instance ),
47
+ activeInstanceExecutionKey (instance .InstanceID ),
48
+ pendingEventsKey (instance ),
49
+ payloadKey (instance ),
50
+ instancesActive (),
51
+ keyInfo .SetKey ,
52
+ keyInfo .StreamKey ,
53
+ },
54
+ instanceSegment (instance ),
55
+ string (instanceState ),
56
+ string (activeInstance ),
57
+ event .ID ,
58
+ eventData ,
59
+ payloadData ,
60
+ ).Result ()
61
+
62
+ if err != nil {
63
+ if _ , ok := err .(redis.Error ); ok {
64
+ if err .Error () == "ERR InstanceAlreadyExists" {
65
+ return backend .ErrInstanceAlreadyExists
66
+ }
67
+ }
46
68
47
- if _ , err := p .Exec (ctx ); err != nil {
48
69
return fmt .Errorf ("creating workflow instance: %w" , err )
49
70
}
50
71
51
- rb .options .Logger .Debug ("Created new workflow instance" )
52
-
53
72
return nil
54
73
}
55
74
0 commit comments