@@ -218,7 +218,9 @@ func createInstance(ctx context.Context, tx *sql.Tx, wfi workflow.Instance) erro
218
218
219
219
// SignalWorkflow signals a running workflow instance
220
220
func (b * mysqlBackend ) SignalWorkflow (ctx context.Context , instanceID string , event history.Event ) error {
221
- tx , err := b .db .BeginTx (ctx , nil )
221
+ tx , err := b .db .BeginTx (ctx , & sql.TxOptions {
222
+ Isolation : sql .LevelReadCommitted ,
223
+ })
222
224
if err != nil {
223
225
return err
224
226
}
@@ -233,7 +235,9 @@ func (b *mysqlBackend) SignalWorkflow(ctx context.Context, instanceID string, ev
233
235
234
236
// GetWorkflowInstance returns a pending workflow task or nil if there are no pending worflow executions
235
237
func (b * mysqlBackend ) GetWorkflowTask (ctx context.Context ) (* task.Workflow , error ) {
236
- tx , err := b .db .BeginTx (ctx , nil )
238
+ tx , err := b .db .BeginTx (ctx , & sql.TxOptions {
239
+ Isolation : sql .LevelReadCommitted ,
240
+ })
237
241
if err != nil {
238
242
return nil , err
239
243
}
@@ -243,19 +247,20 @@ func (b *mysqlBackend) GetWorkflowTask(ctx context.Context) (*task.Workflow, err
243
247
now := time .Now ()
244
248
row := tx .QueryRowContext (
245
249
ctx ,
246
- `SELECT i.id, i.instance_id, i.execution_id, i.parent_instance_id, i.parent_schedule_event_id, i.sticky_until FROM instances i
250
+ `SELECT i.id, i.instance_id, i.execution_id, i.parent_instance_id, i.parent_schedule_event_id, i.sticky_until
251
+ FROM instances i
247
252
INNER JOIN pending_events pe ON i.instance_id = pe.instance_id
248
253
WHERE
249
- (i.locked_until IS NULL OR i.locked_until < ?)
250
- AND (i.sticky_until IS NULL OR i.sticky_until < ? OR i.worker = ?)
251
- AND i.completed_at IS NULL
254
+ i.completed_at IS NULL
252
255
AND (pe.visible_at IS NULL OR pe.visible_at <= ?)
256
+ AND (i.locked_until IS NULL OR i.locked_until < ?)
257
+ AND (i.sticky_until IS NULL OR i.sticky_until < ? OR i.worker = ?)
253
258
LIMIT 1
254
- FOR UPDATE SKIP LOCKED` ,
259
+ FOR UPDATE OF i SKIP LOCKED` ,
260
+ now , // event.visible_at
255
261
now , // locked_until
256
262
now , // sticky_until
257
263
b .workerName , // worker
258
- now , // event.visible_at
259
264
)
260
265
261
266
var id int
@@ -271,6 +276,8 @@ func (b *mysqlBackend) GetWorkflowTask(ctx context.Context) (*task.Workflow, err
271
276
return nil , errors .Wrap (err , "could not scan workflow instance" )
272
277
}
273
278
279
+ // log.Println("Acquired workflow instance", instanceID)
280
+
274
281
res , err := tx .ExecContext (
275
282
ctx ,
276
283
`UPDATE instances i
@@ -431,7 +438,9 @@ func (b *mysqlBackend) GetWorkflowTask(ctx context.Context) (*task.Workflow, err
431
438
func (b * mysqlBackend ) CompleteWorkflowTask (
432
439
ctx context.Context ,
433
440
instance workflow.Instance ,
441
+ state backend.WorkflowState ,
434
442
executedEvents []history.Event ,
443
+ activityEvents []history.Event ,
435
444
workflowEvents []history.WorkflowEvent ,
436
445
) error {
437
446
tx , err := b .db .BeginTx (ctx , & sql.TxOptions {
@@ -443,10 +452,17 @@ func (b *mysqlBackend) CompleteWorkflowTask(
443
452
defer tx .Rollback ()
444
453
445
454
// Unlock instance, but keep it sticky to the current worker
455
+ var completedAt * time.Time
456
+ if state == backend .WorkflowStateFinished {
457
+ t := time .Now ()
458
+ completedAt = & t
459
+ }
460
+
446
461
res , err := tx .ExecContext (
447
462
ctx ,
448
- `UPDATE instances SET locked_until = NULL, sticky_until = ? WHERE instance_id = ? AND execution_id = ? AND worker = ?` ,
463
+ `UPDATE instances SET locked_until = NULL, sticky_until = ?, completed_at = ? WHERE instance_id = ? AND execution_id = ? AND worker = ?` ,
449
464
time .Now ().Add (b .options .StickyTimeout ),
465
+ completedAt ,
450
466
instance .GetInstanceID (),
451
467
instance .GetExecutionID (),
452
468
b .workerName ,
@@ -484,18 +500,10 @@ func (b *mysqlBackend) CompleteWorkflowTask(
484
500
return errors .Wrap (err , "could not insert new history events" )
485
501
}
486
502
487
- workflowCompleted := false
488
-
489
503
// Schedule activities
490
- for _ , e := range executedEvents {
491
- switch e .Type {
492
- case history .EventType_ActivityScheduled :
493
- if err := scheduleActivity (ctx , tx , instance .GetInstanceID (), instance .GetExecutionID (), e ); err != nil {
494
- return errors .Wrap (err , "could not schedule activity" )
495
- }
496
-
497
- case history .EventType_WorkflowExecutionFinished :
498
- workflowCompleted = true
504
+ for _ , e := range activityEvents {
505
+ if err := scheduleActivity (ctx , tx , instance .GetInstanceID (), instance .GetExecutionID (), e ); err != nil {
506
+ return errors .Wrap (err , "could not schedule activity" )
499
507
}
500
508
}
501
509
@@ -522,20 +530,8 @@ func (b *mysqlBackend) CompleteWorkflowTask(
522
530
}
523
531
}
524
532
525
- if workflowCompleted {
526
- if _ , err := tx .ExecContext (
527
- ctx ,
528
- "UPDATE instances SET completed_at = ? WHERE instance_id = ? AND execution_id = ?" ,
529
- time .Now (),
530
- instance .GetInstanceID (),
531
- instance .GetExecutionID (),
532
- ); err != nil {
533
- return errors .Wrap (err , "could not mark instance as completed" )
534
- }
535
- }
536
-
537
533
if err := tx .Commit (); err != nil {
538
- return err
534
+ return errors . Wrap ( err , "could not commit complete workflow transaction" )
539
535
}
540
536
541
537
return nil
@@ -572,7 +568,9 @@ func (b *mysqlBackend) ExtendWorkflowTask(ctx context.Context, instance workflow
572
568
573
569
// GetActivityTask returns a pending activity task or nil if there are no pending activities
574
570
func (b * mysqlBackend ) GetActivityTask (ctx context.Context ) (* task.Activity , error ) {
575
- tx , err := b .db .BeginTx (ctx , nil )
571
+ tx , err := b .db .BeginTx (ctx , & sql.TxOptions {
572
+ Isolation : sql .LevelReadCommitted ,
573
+ })
576
574
if err != nil {
577
575
return nil , err
578
576
}
@@ -635,7 +633,9 @@ func (b *mysqlBackend) GetActivityTask(ctx context.Context) (*task.Activity, err
635
633
636
634
// CompleteActivityTask completes a activity task retrieved using GetActivityTask
637
635
func (b * mysqlBackend ) CompleteActivityTask (ctx context.Context , instance workflow.Instance , id string , event history.Event ) error {
638
- tx , err := b .db .BeginTx (ctx , nil )
636
+ tx , err := b .db .BeginTx (ctx , & sql.TxOptions {
637
+ Isolation : sql .LevelReadCommitted ,
638
+ })
639
639
if err != nil {
640
640
return err
641
641
}
0 commit comments