@@ -352,78 +352,70 @@ func UpdateTaskByState(ctx context.Context, runnerID int64, state *runnerv1.Task
352352 stepStates [v .Id ] = v
353353 }
354354
355- ctx , committer , err := db .TxContext (ctx )
356- if err != nil {
357- return nil , err
358- }
359- defer committer .Close ()
360-
361- e := db .GetEngine (ctx )
362-
363- task := & ActionTask {}
364- if has , err := e .ID (state .Id ).Get (task ); err != nil {
365- return nil , err
366- } else if ! has {
367- return nil , util .ErrNotExist
368- } else if runnerID != task .RunnerID {
369- return nil , errors .New ("invalid runner for task" )
370- }
371-
372- if task .Status .IsDone () {
373- // the state is final, do nothing
374- return task , nil
375- }
355+ return db .WithTx2 (ctx , func (ctx context.Context ) (* ActionTask , error ) {
356+ e := db .GetEngine (ctx )
376357
377- // state.Result is not unspecified means the task is finished
378- if state .Result != runnerv1 .Result_RESULT_UNSPECIFIED {
379- task .Status = Status (state .Result )
380- task .Stopped = timeutil .TimeStamp (state .StoppedAt .AsTime ().Unix ())
381- if err := UpdateTask (ctx , task , "status" , "stopped" ); err != nil {
358+ task := & ActionTask {}
359+ if has , err := e .ID (state .Id ).Get (task ); err != nil {
382360 return nil , err
361+ } else if ! has {
362+ return nil , util .ErrNotExist
363+ } else if runnerID != task .RunnerID {
364+ return nil , errors .New ("invalid runner for task" )
383365 }
384- if _ , err := UpdateRunJob (ctx , & ActionRunJob {
385- ID : task .JobID ,
386- Status : task .Status ,
387- Stopped : task .Stopped ,
388- }, nil ); err != nil {
389- return nil , err
390- }
391- } else {
392- // Force update ActionTask.Updated to avoid the task being judged as a zombie task
393- task .Updated = timeutil .TimeStampNow ()
394- if err := UpdateTask (ctx , task , "updated" ); err != nil {
395- return nil , err
396- }
397- }
398-
399- if err := task .LoadAttributes (ctx ); err != nil {
400- return nil , err
401- }
402366
403- for _ , step := range task .Steps {
404- var result runnerv1.Result
405- if v , ok := stepStates [step .Index ]; ok {
406- result = v .Result
407- step .LogIndex = v .LogIndex
408- step .LogLength = v .LogLength
409- step .Started = convertTimestamp (v .StartedAt )
410- step .Stopped = convertTimestamp (v .StoppedAt )
367+ if task .Status .IsDone () {
368+ // the state is final, do nothing
369+ return task , nil
411370 }
412- if result != runnerv1 .Result_RESULT_UNSPECIFIED {
413- step .Status = Status (result )
414- } else if step .Started != 0 {
415- step .Status = StatusRunning
371+
372+ // state.Result is not unspecified means the task is finished
373+ if state .Result != runnerv1 .Result_RESULT_UNSPECIFIED {
374+ task .Status = Status (state .Result )
375+ task .Stopped = timeutil .TimeStamp (state .StoppedAt .AsTime ().Unix ())
376+ if err := UpdateTask (ctx , task , "status" , "stopped" ); err != nil {
377+ return nil , err
378+ }
379+ if _ , err := UpdateRunJob (ctx , & ActionRunJob {
380+ ID : task .JobID ,
381+ Status : task .Status ,
382+ Stopped : task .Stopped ,
383+ }, nil ); err != nil {
384+ return nil , err
385+ }
386+ } else {
387+ // Force update ActionTask.Updated to avoid the task being judged as a zombie task
388+ task .Updated = timeutil .TimeStampNow ()
389+ if err := UpdateTask (ctx , task , "updated" ); err != nil {
390+ return nil , err
391+ }
416392 }
417- if _ , err := e .ID (step .ID ).Update (step ); err != nil {
393+
394+ if err := task .LoadAttributes (ctx ); err != nil {
418395 return nil , err
419396 }
420- }
421397
422- if err := committer .Commit (); err != nil {
423- return nil , err
424- }
398+ for _ , step := range task .Steps {
399+ var result runnerv1.Result
400+ if v , ok := stepStates [step .Index ]; ok {
401+ result = v .Result
402+ step .LogIndex = v .LogIndex
403+ step .LogLength = v .LogLength
404+ step .Started = convertTimestamp (v .StartedAt )
405+ step .Stopped = convertTimestamp (v .StoppedAt )
406+ }
407+ if result != runnerv1 .Result_RESULT_UNSPECIFIED {
408+ step .Status = Status (result )
409+ } else if step .Started != 0 {
410+ step .Status = StatusRunning
411+ }
412+ if _ , err := e .ID (step .ID ).Update (step ); err != nil {
413+ return nil , err
414+ }
415+ }
425416
426- return task , nil
417+ return task , nil
418+ })
427419}
428420
429421func StopTask (ctx context.Context , taskID int64 , status Status ) error {
0 commit comments