2014-04-13 01:30:09 +00:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2019-07-16 00:13:03 +00:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2014-04-13 01:30:09 +00:00
|
|
|
|
|
|
|
package cron
|
|
|
|
|
|
|
|
import (
|
2019-12-15 09:51:28 +00:00
|
|
|
"context"
|
2022-03-31 17:01:43 +00:00
|
|
|
"runtime/pprof"
|
2014-06-13 17:01:52 +00:00
|
|
|
"time"
|
|
|
|
|
2019-12-15 09:51:28 +00:00
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2022-03-31 17:01:43 +00:00
|
|
|
"code.gitea.io/gitea/modules/process"
|
2019-07-16 00:13:03 +00:00
|
|
|
"code.gitea.io/gitea/modules/sync"
|
2022-06-26 14:19:22 +00:00
|
|
|
"code.gitea.io/gitea/modules/translation"
|
2019-07-16 00:13:03 +00:00
|
|
|
|
|
|
|
"github.com/gogs/cron"
|
|
|
|
)
|
|
|
|
|
2016-02-20 20:58:09 +00:00
|
|
|
var c = cron.New()
|
|
|
|
|
2019-07-16 00:13:03 +00:00
|
|
|
// Prevent duplicate running tasks.
|
|
|
|
var taskStatusTable = sync.NewStatusTable()
|
|
|
|
|
2016-11-25 08:19:24 +00:00
|
|
|
// NewContext begins cron tasks
|
2019-12-15 09:51:28 +00:00
|
|
|
// Each cron task is run within the shutdown context as a running server
|
|
|
|
// AtShutdown the cron server is stopped
|
2022-03-31 17:01:43 +00:00
|
|
|
func NewContext(original context.Context) {
|
|
|
|
defer pprof.SetGoroutineLabels(original)
|
|
|
|
_, _, finished := process.GetManager().AddTypedContext(graceful.GetManager().ShutdownContext(), "Service: Cron", process.SystemProcessType, true)
|
2020-05-16 23:31:38 +00:00
|
|
|
initBasicTasks()
|
|
|
|
initExtendedTasks()
|
2019-10-14 06:10:42 +00:00
|
|
|
|
2020-05-16 23:31:38 +00:00
|
|
|
lock.Lock()
|
|
|
|
for _, task := range tasks {
|
|
|
|
if task.IsEnabled() && task.DoRunAtStart() {
|
|
|
|
go task.Run()
|
|
|
|
}
|
2019-10-14 06:10:42 +00:00
|
|
|
}
|
|
|
|
|
2016-02-20 20:58:09 +00:00
|
|
|
c.Start()
|
2020-05-16 23:31:38 +00:00
|
|
|
started = true
|
|
|
|
lock.Unlock()
|
|
|
|
graceful.GetManager().RunAtShutdown(context.Background(), func() {
|
|
|
|
c.Stop()
|
|
|
|
lock.Lock()
|
|
|
|
started = false
|
|
|
|
lock.Unlock()
|
2022-03-31 17:01:43 +00:00
|
|
|
finished()
|
2020-05-16 23:31:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// TaskTableRow represents a task row in the tasks table
|
|
|
|
type TaskTableRow struct {
|
2022-03-29 01:31:07 +00:00
|
|
|
Name string
|
|
|
|
Spec string
|
|
|
|
Next time.Time
|
|
|
|
Prev time.Time
|
|
|
|
Status string
|
|
|
|
LastMessage string
|
|
|
|
LastDoer string
|
|
|
|
ExecTimes int64
|
|
|
|
task *Task
|
|
|
|
}
|
|
|
|
|
2022-06-26 14:19:22 +00:00
|
|
|
func (t *TaskTableRow) FormatLastMessage(locale translation.Locale) string {
|
2022-03-29 01:31:07 +00:00
|
|
|
if t.Status == "finished" {
|
|
|
|
return t.task.GetConfig().FormatMessage(locale, t.Name, t.Status, t.LastDoer)
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.task.GetConfig().FormatMessage(locale, t.Name, t.Status, t.LastDoer, t.LastMessage)
|
2014-06-13 17:01:52 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 23:31:38 +00:00
|
|
|
// TaskTable represents a table of tasks
|
|
|
|
type TaskTable []*TaskTableRow
|
|
|
|
|
2016-02-20 20:58:09 +00:00
|
|
|
// ListTasks returns all running cron tasks.
|
2020-05-16 23:31:38 +00:00
|
|
|
func ListTasks() TaskTable {
|
|
|
|
entries := c.Entries()
|
|
|
|
eMap := map[string]*cron.Entry{}
|
|
|
|
for _, e := range entries {
|
|
|
|
eMap[e.Description] = e
|
|
|
|
}
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
tTable := make([]*TaskTableRow, 0, len(tasks))
|
|
|
|
for _, task := range tasks {
|
|
|
|
spec := "-"
|
|
|
|
var (
|
|
|
|
next time.Time
|
|
|
|
prev time.Time
|
|
|
|
)
|
|
|
|
if e, ok := eMap[task.Name]; ok {
|
|
|
|
spec = e.Spec
|
|
|
|
next = e.Next
|
|
|
|
prev = e.Prev
|
|
|
|
}
|
|
|
|
task.lock.Lock()
|
|
|
|
tTable = append(tTable, &TaskTableRow{
|
2022-03-29 01:31:07 +00:00
|
|
|
Name: task.Name,
|
|
|
|
Spec: spec,
|
|
|
|
Next: next,
|
|
|
|
Prev: prev,
|
|
|
|
ExecTimes: task.ExecTimes,
|
|
|
|
LastMessage: task.LastMessage,
|
|
|
|
Status: task.Status,
|
|
|
|
LastDoer: task.LastDoer,
|
|
|
|
task: task,
|
2020-05-16 23:31:38 +00:00
|
|
|
})
|
|
|
|
task.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
return tTable
|
2014-04-13 01:30:09 +00:00
|
|
|
}
|