2020-05-17 05:01:38 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2020-05-17 05:01:38 +05:30
|
|
|
|
|
|
|
package cron
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
|
2021-11-10 10:43:16 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-10-17 04:59:26 +05:30
|
|
|
system_model "code.gitea.io/gitea/models/system"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-05-17 05:01:38 +05:30
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/process"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-02-07 13:13:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/translation"
|
2020-05-17 05:01:38 +05:30
|
|
|
)
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
var (
|
|
|
|
lock = sync.Mutex{}
|
|
|
|
started = false
|
|
|
|
tasks = []*Task{}
|
|
|
|
tasksMap = map[string]*Task{}
|
|
|
|
)
|
2020-05-17 05:01:38 +05:30
|
|
|
|
|
|
|
// Task represents a Cron task
|
|
|
|
type Task struct {
|
2022-03-29 07:01:07 +05:30
|
|
|
lock sync.Mutex
|
|
|
|
Name string
|
|
|
|
config Config
|
|
|
|
fun func(context.Context, *user_model.User, Config) error
|
|
|
|
Status string
|
|
|
|
LastMessage string
|
|
|
|
LastDoer string
|
|
|
|
ExecTimes int64
|
2020-05-17 05:01:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// DoRunAtStart returns if this task should run at the start
|
|
|
|
func (t *Task) DoRunAtStart() bool {
|
|
|
|
return t.config.DoRunAtStart()
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEnabled returns if this task is enabled as cron task
|
|
|
|
func (t *Task) IsEnabled() bool {
|
|
|
|
return t.config.IsEnabled()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetConfig will return a copy of the task's config
|
|
|
|
func (t *Task) GetConfig() Config {
|
|
|
|
if reflect.TypeOf(t.config).Kind() == reflect.Ptr {
|
|
|
|
// Pointer:
|
|
|
|
return reflect.New(reflect.ValueOf(t.config).Elem().Type()).Interface().(Config)
|
|
|
|
}
|
|
|
|
// Not pointer:
|
|
|
|
return reflect.New(reflect.TypeOf(t.config)).Elem().Interface().(Config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run will run the task incrementing the cron counter with no user defined
|
|
|
|
func (t *Task) Run() {
|
2021-11-24 15:19:20 +05:30
|
|
|
t.RunWithUser(&user_model.User{
|
2020-05-17 05:01:38 +05:30
|
|
|
ID: -1,
|
|
|
|
Name: "(Cron)",
|
|
|
|
LowerName: "(cron)",
|
|
|
|
}, t.config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunWithUser will run the task incrementing the cron counter at the time with User
|
2021-11-24 15:19:20 +05:30
|
|
|
func (t *Task) RunWithUser(doer *user_model.User, config Config) {
|
2020-05-17 05:01:38 +05:30
|
|
|
if !taskStatusTable.StartIfNotRunning(t.Name) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.lock.Lock()
|
|
|
|
if config == nil {
|
|
|
|
config = t.config
|
|
|
|
}
|
|
|
|
t.ExecTimes++
|
|
|
|
t.lock.Unlock()
|
|
|
|
defer func() {
|
|
|
|
taskStatusTable.Stop(t.Name)
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
// Recover a panic within the
|
|
|
|
combinedErr := fmt.Errorf("%s\n%s", err, log.Stack(2))
|
|
|
|
log.Error("PANIC whilst running task: %s Value: %v", t.Name, combinedErr)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
graceful.GetManager().RunWithShutdownContext(func(baseCtx context.Context) {
|
|
|
|
pm := process.GetManager()
|
2022-03-29 07:01:07 +05:30
|
|
|
doerName := ""
|
|
|
|
if doer != nil && doer.ID != -1 {
|
|
|
|
doerName = doer.Name
|
|
|
|
}
|
|
|
|
|
2022-06-26 19:49:22 +05:30
|
|
|
ctx, _, finished := pm.AddContext(baseCtx, config.FormatMessage(translation.NewLocale("en-US"), t.Name, "process", doerName))
|
2021-12-01 01:36:32 +05:30
|
|
|
defer finished()
|
|
|
|
|
2020-05-17 05:01:38 +05:30
|
|
|
if err := t.fun(ctx, doer, config); err != nil {
|
2022-03-29 07:01:07 +05:30
|
|
|
var message string
|
|
|
|
var status string
|
2021-11-10 10:43:16 +05:30
|
|
|
if db.IsErrCancelled(err) {
|
2022-03-29 07:01:07 +05:30
|
|
|
status = "cancelled"
|
|
|
|
message = err.(db.ErrCancelled).Message
|
|
|
|
} else {
|
|
|
|
status = "error"
|
|
|
|
message = err.Error()
|
2020-05-17 05:01:38 +05:30
|
|
|
}
|
2022-03-29 07:01:07 +05:30
|
|
|
|
|
|
|
t.lock.Lock()
|
|
|
|
t.LastMessage = message
|
|
|
|
t.Status = status
|
|
|
|
t.LastDoer = doerName
|
|
|
|
t.lock.Unlock()
|
|
|
|
|
2022-10-17 04:59:26 +05:30
|
|
|
if err := system_model.CreateNotice(ctx, system_model.NoticeTask, config.FormatMessage(translation.NewLocale("en-US"), t.Name, "cancelled", doerName, message)); err != nil {
|
2020-05-17 05:01:38 +05:30
|
|
|
log.Error("CreateNotice: %v", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2022-03-29 07:01:07 +05:30
|
|
|
|
|
|
|
t.lock.Lock()
|
|
|
|
t.Status = "finished"
|
|
|
|
t.LastMessage = ""
|
|
|
|
t.LastDoer = doerName
|
|
|
|
t.lock.Unlock()
|
|
|
|
|
2020-08-06 02:10:36 +05:30
|
|
|
if config.DoNoticeOnSuccess() {
|
2022-10-17 04:59:26 +05:30
|
|
|
if err := system_model.CreateNotice(ctx, system_model.NoticeTask, config.FormatMessage(translation.NewLocale("en-US"), t.Name, "finished", doerName)); err != nil {
|
2020-08-06 02:10:36 +05:30
|
|
|
log.Error("CreateNotice: %v", err)
|
|
|
|
}
|
2020-05-17 05:01:38 +05:30
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTask gets the named task
|
|
|
|
func GetTask(name string) *Task {
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
log.Info("Getting %s in %v", name, tasksMap[name])
|
|
|
|
|
|
|
|
return tasksMap[name]
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterTask allows a task to be registered with the cron service
|
2021-11-24 15:19:20 +05:30
|
|
|
func RegisterTask(name string, config Config, fun func(context.Context, *user_model.User, Config) error) error {
|
2020-05-17 05:01:38 +05:30
|
|
|
log.Debug("Registering task: %s", name)
|
2022-02-07 13:13:53 +05:30
|
|
|
|
|
|
|
i18nKey := "admin.dashboard." + name
|
2022-06-26 19:49:22 +05:30
|
|
|
if value := translation.NewLocale("en-US").Tr(i18nKey); value == i18nKey {
|
2022-02-07 13:13:53 +05:30
|
|
|
return fmt.Errorf("translation is missing for task %q, please add translation for %q", name, i18nKey)
|
|
|
|
}
|
|
|
|
|
2020-05-17 05:01:38 +05:30
|
|
|
_, err := setting.GetCronSettings(name, config)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to register cron task with name: %s Error: %v", name, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
task := &Task{
|
|
|
|
Name: name,
|
|
|
|
config: config,
|
|
|
|
fun: fun,
|
|
|
|
}
|
|
|
|
lock.Lock()
|
|
|
|
locked := true
|
|
|
|
defer func() {
|
|
|
|
if locked {
|
|
|
|
lock.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if _, has := tasksMap[task.Name]; has {
|
|
|
|
log.Error("A task with this name: %s has already been registered", name)
|
|
|
|
return fmt.Errorf("duplicate task with name: %s", task.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks = append(tasks, task)
|
|
|
|
tasksMap[task.Name] = task
|
|
|
|
if started && config.IsEnabled() && config.DoRunAtStart() {
|
|
|
|
lock.Unlock()
|
|
|
|
locked = false
|
|
|
|
task.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterTaskFatal will register a task but if there is an error log.Fatal
|
2021-11-24 15:19:20 +05:30
|
|
|
func RegisterTaskFatal(name string, config Config, fun func(context.Context, *user_model.User, Config) error) {
|
2020-05-17 05:01:38 +05:30
|
|
|
if err := RegisterTask(name, config, fun); err != nil {
|
|
|
|
log.Fatal("Unable to register cron task %s Error: %v", name, err)
|
|
|
|
}
|
|
|
|
}
|