mirror of
https://github.com/go-gitea/gitea
synced 2025-12-07 13:28:25 +00:00
feat: stop zombie and endless tasks
This commit is contained in:
@@ -37,8 +37,8 @@ type Task struct {
|
||||
Attempt int64
|
||||
RunnerID int64 `xorm:"index"`
|
||||
Result runnerv1.Result
|
||||
Status Status `xorm:"index"`
|
||||
Started timeutil.TimeStamp
|
||||
Status Status `xorm:"index"`
|
||||
Started timeutil.TimeStamp `xorm:"index"`
|
||||
Stopped timeutil.TimeStamp
|
||||
|
||||
Token string `xorm:"-"`
|
||||
@@ -54,7 +54,7 @@ type Task struct {
|
||||
LogExpired bool // files that are too old will be deleted
|
||||
|
||||
Created timeutil.TimeStamp `xorm:"created"`
|
||||
Updated timeutil.TimeStamp `xorm:"updated"`
|
||||
Updated timeutil.TimeStamp `xorm:"updated index"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -412,6 +412,57 @@ func UpdateTaskByState(state *runnerv1.TaskState) (*Task, error) {
|
||||
return task, nil
|
||||
}
|
||||
|
||||
func StopTask(ctx context.Context, task *Task, result runnerv1.Result) (*Task, error) {
|
||||
ctx, commiter, err := db.TxContext()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer commiter.Close()
|
||||
|
||||
e := db.GetEngine(ctx)
|
||||
|
||||
now := timeutil.TimeStampNow()
|
||||
task.Result = result
|
||||
if task.Result != runnerv1.Result_RESULT_UNSPECIFIED {
|
||||
task.Status = Status(task.Result)
|
||||
task.Stopped = now
|
||||
if _, err := UpdateRunJob(ctx, &RunJob{
|
||||
ID: task.JobID,
|
||||
Status: task.Status,
|
||||
Stopped: task.Stopped,
|
||||
}, nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := e.ID(task.ID).Update(task); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := task.LoadAttributes(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, step := range task.Steps {
|
||||
if step.Result == runnerv1.Result_RESULT_UNSPECIFIED {
|
||||
step.Result = result
|
||||
if step.Started == 0 {
|
||||
step.Started = now
|
||||
}
|
||||
step.Stopped = now
|
||||
}
|
||||
if _, err := e.ID(step.ID).Update(step); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if err := commiter.Commit(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return task, nil
|
||||
}
|
||||
|
||||
func isSubset(set, subset []string) bool {
|
||||
m := make(map[string]struct{}, len(set))
|
||||
for _, v := range set {
|
||||
|
||||
50
models/bots/task_list.go
Normal file
50
models/bots/task_list.go
Normal file
@@ -0,0 +1,50 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package bots
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
type TaskList []*Task
|
||||
|
||||
type FindTaskOptions struct {
|
||||
db.ListOptions
|
||||
Status Status
|
||||
UpdatedBefore timeutil.TimeStamp
|
||||
StartedBefore timeutil.TimeStamp
|
||||
}
|
||||
|
||||
func (opts FindTaskOptions) toConds() builder.Cond {
|
||||
cond := builder.NewCond()
|
||||
if opts.Status > StatusUnknown {
|
||||
cond = cond.And(builder.Eq{"status": opts.Status})
|
||||
}
|
||||
if opts.UpdatedBefore > 0 {
|
||||
cond = cond.And(builder.Lt{"updated": opts.UpdatedBefore})
|
||||
}
|
||||
if opts.StartedBefore > 0 {
|
||||
cond = cond.And(builder.Lt{"started": opts.StartedBefore})
|
||||
}
|
||||
return cond
|
||||
}
|
||||
|
||||
func FindTasks(ctx context.Context, opts FindTaskOptions) (TaskList, int64, error) {
|
||||
e := db.GetEngine(ctx).Where(opts.toConds())
|
||||
if opts.PageSize > 0 && opts.Page >= 1 {
|
||||
e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
|
||||
}
|
||||
var tasks TaskList
|
||||
total, err := e.FindAndCount(&tasks)
|
||||
return tasks, total, err
|
||||
}
|
||||
|
||||
func CountTasks(ctx context.Context, opts FindTaskOptions) (int64, error) {
|
||||
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(Task))
|
||||
}
|
||||
Reference in New Issue
Block a user