1
0
Fork 0
forked from forgejo/forgejo

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

@ -7,6 +7,7 @@ import (
"context"
"fmt"
"reflect"
"strings"
"sync"
"code.gitea.io/gitea/models/db"
@ -176,8 +177,7 @@ func RegisterTask(name string, config Config, fun func(context.Context, *user_mo
if config.IsEnabled() {
// We cannot use the entry return as there is no way to lock it
if _, err = c.AddJob(name, config.GetSchedule(), task); err != nil {
log.Error("Unable to register cron task with name: %s Error: %v", name, err)
if err := addTaskToScheduler(task); err != nil {
return err
}
}
@ -199,3 +199,21 @@ func RegisterTaskFatal(name string, config Config, fun func(context.Context, *us
log.Fatal("Unable to register cron task %s Error: %v", name, err)
}
}
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 {
log.Error("Unable to register cron task with name: %s Error: %v", task.Name, err)
return err
}
return nil
}
func scheduleHasSeconds(schedule string) bool {
return len(strings.Fields(schedule)) >= 6
}