Skip to content

Commit 07ba4d9

Browse files
authored
Fix incorrect action duration time when rerun the job before executed once (#28364)
Fix #28323 Reason was mentioned here: #28323 (comment) ### Changes: (maybe breaking) We can rerun jobs in Gitea, so there will be some problems in calculating duration time. In this PR, I use the exist `Started` and `Stopped` column to record the last run time instead of the total time, and add a new `PreviousDuration` column to record the previous duration time. You can also check the cost time of last run: ![image](https://github.com/go-gitea/gitea/assets/18380374/2ca39145-2c92-401a-b78b-43164f7ae061)
1 parent 461d8b5 commit 07ba4d9

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

models/actions/run.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ type ActionRun struct {
4646
TriggerEvent string // the trigger event defined in the `on` configuration of the triggered workflow
4747
Status Status `xorm:"index"`
4848
Version int `xorm:"version default 0"` // Status could be updated concomitantly, so an optimistic lock is needed
49-
Started timeutil.TimeStamp
50-
Stopped timeutil.TimeStamp
51-
Created timeutil.TimeStamp `xorm:"created"`
52-
Updated timeutil.TimeStamp `xorm:"updated"`
49+
// Started and Stopped is used for recording last run time, if rerun happened, they will be reset to 0
50+
Started timeutil.TimeStamp
51+
Stopped timeutil.TimeStamp
52+
// PreviousDuration is used for recording previous duration
53+
PreviousDuration time.Duration
54+
Created timeutil.TimeStamp `xorm:"created"`
55+
Updated timeutil.TimeStamp `xorm:"updated"`
5356
}
5457

5558
func init() {
@@ -118,7 +121,7 @@ func (run *ActionRun) LoadAttributes(ctx context.Context) error {
118121
}
119122

120123
func (run *ActionRun) Duration() time.Duration {
121-
return calculateDuration(run.Started, run.Stopped, run.Status)
124+
return calculateDuration(run.Started, run.Stopped, run.Status) + run.PreviousDuration
122125
}
123126

124127
func (run *ActionRun) GetPushEventPayload() (*api.PushPayload, error) {

models/migrations/migrations.go

+2
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,8 @@ var migrations = []Migration{
554554
NewMigration("Add combined Index to issue_user.uid and issue_id", v1_22.AddCombinedIndexToIssueUser),
555555
// v284 -> v285
556556
NewMigration("Add ignore stale approval column on branch table", v1_22.AddIgnoreStaleApprovalsColumnToProtectedBranchTable),
557+
// v285 -> v286
558+
NewMigration("Add PreviousDuration to ActionRun", v1_22.AddPreviousDurationToActionRun),
557559
}
558560

559561
// GetCurrentDBVersion returns the current db version

models/migrations/v1_22/v285.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_22 //nolint
5+
6+
import (
7+
"time"
8+
9+
"xorm.io/xorm"
10+
)
11+
12+
func AddPreviousDurationToActionRun(x *xorm.Engine) error {
13+
type ActionRun struct {
14+
PreviousDuration time.Duration
15+
}
16+
17+
return x.Sync(&ActionRun{})
18+
}

routers/web/repo/actions/view.go

+11
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,17 @@ func Rerun(ctx *context_module.Context) {
279279
return
280280
}
281281

282+
// reset run's start and stop time when it is done
283+
if run.Status.IsDone() {
284+
run.PreviousDuration = run.Duration()
285+
run.Started = 0
286+
run.Stopped = 0
287+
if err := actions_model.UpdateRun(ctx, run, "started", "stopped", "previous_duration"); err != nil {
288+
ctx.Error(http.StatusInternalServerError, err.Error())
289+
return
290+
}
291+
}
292+
282293
job, jobs := getRunJobs(ctx, runIndex, jobIndex)
283294
if ctx.Written() {
284295
return

0 commit comments

Comments
 (0)