Replace gogs/cron with go-co-op/gocron (#25977)

Replace `github.com/gogs/cron` with `github.com/go-co-op/gocron` as the
former package is not maintained for many years.

---------

Co-authored-by: delvh <dev.lh@web.de>
This commit is contained in:
Chongyi Zheng 2023-07-23 23:13:41 -05:00 committed by GitHub
parent f3d41c61eb
commit f2138d6968
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 123 additions and 22 deletions

View file

@ -14,10 +14,10 @@ import (
"code.gitea.io/gitea/modules/sync"
"code.gitea.io/gitea/modules/translation"
"github.com/gogs/cron"
"github.com/go-co-op/gocron"
)
var c = cron.New()
var scheduler = gocron.NewScheduler(time.Local)
// Prevent duplicate running tasks.
var taskStatusTable = sync.NewStatusTable()
@ -39,11 +39,11 @@ func NewContext(original context.Context) {
}
}
c.Start()
scheduler.StartAsync()
started = true
lock.Unlock()
graceful.GetManager().RunAtShutdown(context.Background(), func() {
c.Stop()
scheduler.Stop()
lock.Lock()
started = false
lock.Unlock()
@ -77,13 +77,20 @@ type TaskTable []*TaskTableRow
// ListTasks returns all running cron tasks.
func ListTasks() TaskTable {
entries := c.Entries()
eMap := map[string]*cron.Entry{}
for _, e := range entries {
eMap[e.Description] = e
jobs := scheduler.Jobs()
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
}
lock.Lock()
defer lock.Unlock()
tTable := make([]*TaskTableRow, 0, len(tasks))
for _, task := range tasks {
spec := "-"
@ -91,10 +98,13 @@ func ListTasks() TaskTable {
next time.Time
prev time.Time
)
if e, ok := eMap[task.Name]; ok {
spec = e.Spec
next = e.Next
prev = e.Prev
if e, ok := jobMap[task.Name]; ok {
tags := e.Tags()
if len(tags) > 1 {
spec = tags[1] // the second tag is the task spec
}
next = e.NextRun()
prev = e.PreviousRun()
}
task.lock.Lock()
tTable = append(tTable, &TaskTableRow{