feat: vendor gitea 1.16.2
This commit is contained in:
@@ -10,22 +10,27 @@ import (
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/graceful"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/process"
|
||||
"code.gitea.io/gitea/modules/sync"
|
||||
"code.gitea.io/gitea/modules/translation"
|
||||
|
||||
"github.com/go-co-op/gocron"
|
||||
"github.com/go-co-op/gocron/v2"
|
||||
)
|
||||
|
||||
var scheduler = gocron.NewScheduler(time.Local)
|
||||
var scheduler gocron.Scheduler
|
||||
|
||||
// Prevent duplicate running tasks.
|
||||
var taskStatusTable = sync.NewStatusTable()
|
||||
func init() {
|
||||
var err error
|
||||
scheduler, err = gocron.NewScheduler(gocron.WithLocation(time.Local))
|
||||
if err != nil {
|
||||
log.Fatal("Unable to create cron scheduler: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// NewContext begins cron tasks
|
||||
// Init begins cron tasks
|
||||
// Each cron task is run within the shutdown context as a running server
|
||||
// AtShutdown the cron server is stopped
|
||||
func NewContext(original context.Context) {
|
||||
func Init(original context.Context) {
|
||||
defer pprof.SetGoroutineLabels(original)
|
||||
_, _, finished := process.GetManager().AddTypedContext(graceful.GetManager().ShutdownContext(), "Service: Cron", process.SystemProcessType, true)
|
||||
initBasicTasks()
|
||||
@@ -39,11 +44,13 @@ func NewContext(original context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
scheduler.StartAsync()
|
||||
scheduler.Start()
|
||||
started = true
|
||||
lock.Unlock()
|
||||
graceful.GetManager().RunAtShutdown(context.Background(), func() {
|
||||
scheduler.Stop()
|
||||
if err := scheduler.Shutdown(); err != nil {
|
||||
log.Error("Unable to shutdown cron scheduler: %v", err)
|
||||
}
|
||||
lock.Lock()
|
||||
started = false
|
||||
lock.Unlock()
|
||||
@@ -78,14 +85,14 @@ type TaskTable []*TaskTableRow
|
||||
// ListTasks returns all running cron tasks.
|
||||
func ListTasks() TaskTable {
|
||||
jobs := scheduler.Jobs()
|
||||
jobMap := map[string]*gocron.Job{}
|
||||
jobMap := map[string]gocron.Job{}
|
||||
for _, job := range jobs {
|
||||
// the first tag is the task name
|
||||
tags := job.Tags()
|
||||
if len(tags) == 0 { // should never happen
|
||||
continue
|
||||
}
|
||||
jobMap[job.Tags()[0]] = job
|
||||
jobMap[tags[0]] = job
|
||||
}
|
||||
|
||||
lock.Lock()
|
||||
@@ -103,8 +110,8 @@ func ListTasks() TaskTable {
|
||||
if len(tags) > 1 {
|
||||
spec = tags[1] // the second tag is the task spec
|
||||
}
|
||||
next = e.NextRun()
|
||||
prev = e.PreviousRun()
|
||||
next, _ = e.NextRun()
|
||||
prev, _ = e.LastRun()
|
||||
}
|
||||
|
||||
task.lock.Lock()
|
||||
|
||||
@@ -14,11 +14,14 @@ import (
|
||||
"code.gitea.io/gitea/models/db"
|
||||
system_model "code.gitea.io/gitea/models/system"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/globallock"
|
||||
"code.gitea.io/gitea/modules/graceful"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/process"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/translation"
|
||||
|
||||
"github.com/go-co-op/gocron/v2"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -71,20 +74,30 @@ func (t *Task) Run() {
|
||||
}, t.config)
|
||||
}
|
||||
|
||||
func getCronTaskLockKey(name string) string {
|
||||
return "cron_task:" + name
|
||||
}
|
||||
|
||||
// RunWithUser will run the task incrementing the cron counter at the time with User
|
||||
func (t *Task) RunWithUser(doer *user_model.User, config Config) {
|
||||
if !taskStatusTable.StartIfNotRunning(t.Name) {
|
||||
locked, releaser, err := globallock.TryLock(graceful.GetManager().ShutdownContext(), getCronTaskLockKey(t.Name))
|
||||
if err != nil {
|
||||
log.Error("Failed to acquire lock for cron task %q: %v", t.Name, err)
|
||||
return
|
||||
}
|
||||
if !locked {
|
||||
log.Trace("a cron task %q is already running", t.Name)
|
||||
return
|
||||
}
|
||||
defer releaser()
|
||||
|
||||
t.lock.Lock()
|
||||
if config == nil {
|
||||
config = t.config
|
||||
}
|
||||
t.ExecTimes++
|
||||
t.lock.Unlock()
|
||||
defer func() {
|
||||
taskStatusTable.Stop(t.Name)
|
||||
}()
|
||||
|
||||
graceful.GetManager().RunWithShutdownContext(func(baseCtx context.Context) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
@@ -213,12 +226,13 @@ func RegisterTaskFatal(name string, config Config, fun func(context.Context, *us
|
||||
|
||||
func addTaskToScheduler(task *Task) error {
|
||||
tags := []string{task.Name, task.config.GetSchedule()} // name and schedule can't be get from job, so we add them as tag
|
||||
if scheduleHasSeconds(task.config.GetSchedule()) {
|
||||
scheduler = scheduler.CronWithSeconds(task.config.GetSchedule())
|
||||
} else {
|
||||
scheduler = scheduler.Cron(task.config.GetSchedule())
|
||||
}
|
||||
if _, err := scheduler.Tag(tags...).Do(task.Run); err != nil {
|
||||
withSeconds := scheduleHasSeconds(task.config.GetSchedule())
|
||||
_, err := scheduler.NewJob(
|
||||
gocron.CronJob(task.config.GetSchedule(), withSeconds),
|
||||
gocron.NewTask(task.Run),
|
||||
gocron.WithTags(tags...),
|
||||
)
|
||||
if err != nil {
|
||||
log.Error("Unable to register cron task with name: %s Error: %v", task.Name, err)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ func registerRepoHealthCheck() {
|
||||
RunAtStart: false,
|
||||
Schedule: "@midnight",
|
||||
},
|
||||
Timeout: time.Duration(setting.Git.Timeout.Default) * time.Second,
|
||||
Timeout: time.Duration(setting.Git.Timeout.GC) * time.Second,
|
||||
Args: []string{},
|
||||
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
||||
rhcConfig := config.(*RepoHealthCheckConfig)
|
||||
|
||||
@@ -13,7 +13,11 @@ import (
|
||||
|
||||
func TestAddTaskToScheduler(t *testing.T) {
|
||||
assert.Empty(t, scheduler.Jobs())
|
||||
defer scheduler.Clear()
|
||||
defer func() {
|
||||
for _, j := range scheduler.Jobs() {
|
||||
_ = scheduler.RemoveJob(j.ID())
|
||||
}
|
||||
}()
|
||||
|
||||
// no seconds
|
||||
err := addTaskToScheduler(&Task{
|
||||
|
||||
Reference in New Issue
Block a user