@@ -138,6 +138,30 @@ func GetRunJobsByRunID(ctx context.Context, runID int64) (ActionJobList, error)
138138 return jobs , nil
139139}
140140
141+ func UpdateRunStatus (ctx context.Context , repoID , runID int64 ) error {
142+ // Other goroutines may aggregate the status of the run and update it too.
143+ // So we need load the run and its jobs before updating the run.
144+ run , err := GetRunByRepoAndID (ctx , repoID , runID )
145+ if err != nil {
146+ return err
147+ }
148+ jobs , err := GetRunJobsByRunID (ctx , runID )
149+ if err != nil {
150+ return err
151+ }
152+ run .Status = AggregateJobStatus (jobs )
153+ if run .Started .IsZero () && run .Status .IsRunning () {
154+ run .Started = timeutil .TimeStampNow ()
155+ }
156+ if run .Stopped .IsZero () && run .Status .IsDone () {
157+ run .Stopped = timeutil .TimeStampNow ()
158+ }
159+ if err := UpdateRun (ctx , run , "status" , "started" , "stopped" ); err != nil {
160+ return fmt .Errorf ("update run %d: %w" , run .ID , err )
161+ }
162+ return nil
163+ }
164+
141165func UpdateRunJob (ctx context.Context , job * ActionRunJob , cond builder.Cond , cols ... string ) (int64 , error ) {
142166 e := db .GetEngine (ctx )
143167
@@ -173,27 +197,8 @@ func UpdateRunJob(ctx context.Context, job *ActionRunJob, cond builder.Cond, col
173197 }
174198 }
175199
176- {
177- // Other goroutines may aggregate the status of the run and update it too.
178- // So we need load the run and its jobs before updating the run.
179- run , err := GetRunByRepoAndID (ctx , job .RepoID , job .RunID )
180- if err != nil {
181- return 0 , err
182- }
183- jobs , err := GetRunJobsByRunID (ctx , job .RunID )
184- if err != nil {
185- return 0 , err
186- }
187- run .Status = AggregateJobStatus (jobs )
188- if run .Started .IsZero () && run .Status .IsRunning () {
189- run .Started = timeutil .TimeStampNow ()
190- }
191- if run .Stopped .IsZero () && run .Status .IsDone () {
192- run .Stopped = timeutil .TimeStampNow ()
193- }
194- if err := UpdateRun (ctx , run , "status" , "started" , "stopped" ); err != nil {
195- return 0 , fmt .Errorf ("update run %d: %w" , run .ID , err )
196- }
200+ if err := UpdateRunStatus (ctx , job .RepoID , job .RunID ); err != nil {
201+ return 0 , err
197202 }
198203
199204 return affected , nil
0 commit comments