Files
Atay-Makhzan/services/cron/cron.go
T

126 lines
2.9 KiB
Go
Raw Normal View History

2014-04-12 21:30:09 -04:00
// Copyright 2014 The Gogs Authors. All rights reserved.
2019-07-16 08:13:03 +08:00
// Copyright 2019 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2014-04-12 21:30:09 -04:00
package cron
import (
"context"
"runtime/pprof"
2014-06-13 13:01:52 -04:00
"time"
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/process"
2019-07-16 08:13:03 +08:00
"code.gitea.io/gitea/modules/sync"
2022-06-26 16:19:22 +02:00
"code.gitea.io/gitea/modules/translation"
2019-07-16 08:13:03 +08:00
"github.com/go-co-op/gocron"
2019-07-16 08:13:03 +08:00
)
var scheduler = gocron.NewScheduler(time.Local)
2016-02-20 15:58:09 -05:00
2019-07-16 08:13:03 +08:00
// Prevent duplicate running tasks.
var taskStatusTable = sync.NewStatusTable()
2016-11-25 16:19:24 +08:00
// NewContext 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) {
defer pprof.SetGoroutineLabels(original)
_, _, finished := process.GetManager().AddTypedContext(graceful.GetManager().ShutdownContext(), "Service: Cron", process.SystemProcessType, true)
initBasicTasks()
initExtendedTasks()
2023-01-31 09:45:19 +08:00
initActionsTasks()
lock.Lock()
for _, task := range tasks {
if task.IsEnabled() && task.DoRunAtStart() {
go task.Run()
}
}
scheduler.StartAsync()
started = true
lock.Unlock()
graceful.GetManager().RunAtShutdown(context.Background(), func() {
scheduler.Stop()
lock.Lock()
started = false
lock.Unlock()
finished()
})
}
// TaskTableRow represents a task row in the tasks table
type TaskTableRow struct {
Name string
Spec string
Next time.Time
Prev time.Time
Status string
LastMessage string
LastDoer string
ExecTimes int64
task *Task
}
2022-06-26 16:19:22 +02:00
func (t *TaskTableRow) FormatLastMessage(locale translation.Locale) string {
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 13:01:52 -04:00
}
// TaskTable represents a table of tasks
type TaskTable []*TaskTableRow
2016-02-20 15:58:09 -05:00
// ListTasks returns all running cron tasks.
func ListTasks() TaskTable {
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 := "-"
var (
next time.Time
prev time.Time
)
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{
Name: task.Name,
Spec: spec,
Next: next,
Prev: prev,
ExecTimes: task.ExecTimes,
LastMessage: task.LastMessage,
Status: task.Status,
LastDoer: task.LastDoer,
task: task,
})
task.lock.Unlock()
}
return tTable
2014-04-12 21:30:09 -04:00
}