4
4
"context"
5
5
"database/sql"
6
6
_ "embed"
7
+ "encoding/json"
7
8
"errors"
8
9
"fmt"
9
10
"strings"
@@ -69,7 +70,7 @@ func (b *mysqlBackend) CreateWorkflowInstance(ctx context.Context, instance *wor
69
70
defer tx .Rollback ()
70
71
71
72
// Create workflow instance
72
- if err := createInstance (ctx , tx , instance , false ); err != nil {
73
+ if err := createInstance (ctx , tx , instance , metadata , false ); err != nil {
73
74
return err
74
75
}
75
76
@@ -204,7 +205,7 @@ func (b *mysqlBackend) GetWorkflowInstanceState(ctx context.Context, instance *w
204
205
return backend .WorkflowStateActive , nil
205
206
}
206
207
207
- func createInstance (ctx context.Context , tx * sql.Tx , wfi * workflow.Instance , ignoreDuplicate bool ) error {
208
+ func createInstance (ctx context.Context , tx * sql.Tx , wfi * workflow.Instance , metadata * workflow. Metadata , ignoreDuplicate bool ) error {
208
209
var parentInstanceID * string
209
210
var parentEventID * int64
210
211
if wfi .SubWorkflow () {
@@ -215,13 +216,19 @@ func createInstance(ctx context.Context, tx *sql.Tx, wfi *workflow.Instance, ign
215
216
parentEventID = & n
216
217
}
217
218
219
+ metadataJson , err := json .Marshal (metadata )
220
+ if err != nil {
221
+ return fmt .Errorf ("marshaling metadata: %w" , err )
222
+ }
223
+
218
224
res , err := tx .ExecContext (
219
225
ctx ,
220
- "INSERT IGNORE INTO `instances` (instance_id, execution_id, parent_instance_id, parent_schedule_event_id) VALUES (?, ?, ?, ?)" ,
226
+ "INSERT IGNORE INTO `instances` (instance_id, execution_id, parent_instance_id, parent_schedule_event_id, metadata ) VALUES (?, ?, ?, ?, ?)" ,
221
227
wfi .InstanceID ,
222
228
wfi .ExecutionID ,
223
229
parentInstanceID ,
224
230
parentEventID ,
231
+ string (metadataJson ),
225
232
)
226
233
if err != nil {
227
234
return fmt .Errorf ("inserting workflow instance: %w" , err )
@@ -278,7 +285,7 @@ func (b *mysqlBackend) GetWorkflowTask(ctx context.Context) (*task.Workflow, err
278
285
now := time .Now ()
279
286
row := tx .QueryRowContext (
280
287
ctx ,
281
- `SELECT i.id, i.instance_id, i.execution_id, i.parent_instance_id, i.parent_schedule_event_id, i.sticky_until
288
+ `SELECT i.id, i.instance_id, i.execution_id, i.parent_instance_id, i.parent_schedule_event_id, i.metadata, i. sticky_until
282
289
FROM instances i
283
290
INNER JOIN pending_events pe ON i.instance_id = pe.instance_id
284
291
WHERE
@@ -298,8 +305,9 @@ func (b *mysqlBackend) GetWorkflowTask(ctx context.Context) (*task.Workflow, err
298
305
var instanceID , executionID string
299
306
var parentInstanceID * string
300
307
var parentEventID * int64
308
+ var metadataJson sql.NullString
301
309
var stickyUntil * time.Time
302
- if err := row .Scan (& id , & instanceID , & executionID , & parentInstanceID , & parentEventID , & stickyUntil ); err != nil {
310
+ if err := row .Scan (& id , & instanceID , & executionID , & parentInstanceID , & parentEventID , & metadataJson , & stickyUntil ); err != nil {
303
311
if err == sql .ErrNoRows {
304
312
return nil , nil
305
313
}
@@ -334,9 +342,17 @@ func (b *mysqlBackend) GetWorkflowTask(ctx context.Context) (*task.Workflow, err
334
342
wfi = core .NewWorkflowInstance (instanceID , executionID )
335
343
}
336
344
345
+ var metadata * core.WorkflowInstanceMetadata
346
+ if metadataJson .Valid {
347
+ if err := json .Unmarshal ([]byte (metadataJson .String ), & metadata ); err != nil {
348
+ return nil , fmt .Errorf ("parsing workflow metadata: %w" , err )
349
+ }
350
+ }
351
+
337
352
t := & task.Workflow {
338
353
ID : wfi .InstanceID ,
339
354
WorkflowInstance : wfi ,
355
+ Metadata : metadata ,
340
356
NewEvents : []history.Event {},
341
357
}
342
358
@@ -494,20 +510,18 @@ func (b *mysqlBackend) CompleteWorkflowTask(
494
510
}
495
511
496
512
// Insert new workflow events
497
- groupedEvents := make (map [* workflow.Instance ][]history.Event )
498
- for _ , m := range workflowEvents {
499
- if _ , ok := groupedEvents [m .WorkflowInstance ]; ! ok {
500
- groupedEvents [m .WorkflowInstance ] = []history.Event {}
501
- }
502
-
503
- groupedEvents [m .WorkflowInstance ] = append (groupedEvents [m .WorkflowInstance ], m .HistoryEvent )
504
- }
513
+ groupedEvents := history .EventsByWorkflowInstance (workflowEvents )
505
514
506
515
for targetInstance , events := range groupedEvents {
507
- if targetInstance .InstanceID != instance .InstanceID {
508
- // Create new instance
509
- if err := createInstance (ctx , tx , targetInstance , true ); err != nil {
510
- return err
516
+ for _ , event := range events {
517
+ if event .Type == history .EventType_WorkflowExecutionStarted {
518
+ a := event .Attributes .(* history.ExecutionStartedAttributes )
519
+ // Create new instance
520
+ if err := createInstance (ctx , tx , targetInstance , a .Metadata , true ); err != nil {
521
+ return err
522
+ }
523
+
524
+ break
511
525
}
512
526
}
513
527
@@ -566,8 +580,9 @@ func (b *mysqlBackend) GetActivityTask(ctx context.Context) (*task.Activity, err
566
580
now := time .Now ()
567
581
res := tx .QueryRowContext (
568
582
ctx ,
569
- `SELECT id, activity_id, instance_id, execution_id, event_type, timestamp, schedule_event_id, attributes, visible_at
583
+ `SELECT id, activity_id, instance_id, execution_id, instances.metadata, event_type, timestamp, schedule_event_id, attributes, visible_at
570
584
FROM activities
585
+ INNER JOIN instances ON activities.instance_id = instances.instance_id
571
586
WHERE locked_until IS NULL OR locked_until < ?
572
587
LIMIT 1
573
588
FOR UPDATE SKIP LOCKED` ,
@@ -577,16 +592,24 @@ func (b *mysqlBackend) GetActivityTask(ctx context.Context) (*task.Activity, err
577
592
var id int64
578
593
var instanceID , executionID string
579
594
var attributes []byte
595
+ var metadataJson sql.NullString
580
596
event := history.Event {}
581
597
582
- if err := res .Scan (& id , & event .ID , & instanceID , & executionID , & event .Type , & event .Timestamp , & event .ScheduleEventID , & attributes , & event .VisibleAt ); err != nil {
598
+ if err := res .Scan (
599
+ & id , & event .ID , & instanceID , & executionID , & metadataJson , & event .Type ,
600
+ & event .Timestamp , & event .ScheduleEventID , & attributes , & event .VisibleAt ); err != nil {
583
601
if err == sql .ErrNoRows {
584
602
return nil , nil
585
603
}
586
604
587
605
return nil , fmt .Errorf ("finding activity task to lock: %w" , err )
588
606
}
589
607
608
+ var metadata * workflow.Metadata
609
+ if err := json .Unmarshal ([]byte (metadataJson .String ), & metadata ); err != nil {
610
+ return nil , fmt .Errorf ("unmarshaling metadata: %w" , err )
611
+ }
612
+
590
613
a , err := history .DeserializeAttributes (event .Type , attributes )
591
614
if err != nil {
592
615
return nil , fmt .Errorf ("deserializing attributes: %w" , err )
@@ -607,6 +630,7 @@ func (b *mysqlBackend) GetActivityTask(ctx context.Context) (*task.Activity, err
607
630
t := & task.Activity {
608
631
ID : event .ID ,
609
632
WorkflowInstance : core .NewWorkflowInstance (instanceID , executionID ),
633
+ WorkflowMetadata : metadata ,
610
634
Event : event ,
611
635
}
612
636
0 commit comments