You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
1.9 KiB
95 lines
1.9 KiB
11 years ago
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||
6 years ago
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||
11 years ago
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package cron
|
||
|
|
||
|
import (
|
||
5 years ago
|
"context"
|
||
11 years ago
|
"time"
|
||
|
|
||
5 years ago
|
"code.gitea.io/gitea/modules/graceful"
|
||
6 years ago
|
"code.gitea.io/gitea/modules/sync"
|
||
|
|
||
|
"github.com/gogs/cron"
|
||
|
)
|
||
|
|
||
9 years ago
|
var c = cron.New()
|
||
|
|
||
6 years ago
|
// Prevent duplicate running tasks.
|
||
|
var taskStatusTable = sync.NewStatusTable()
|
||
|
|
||
8 years ago
|
// NewContext begins cron tasks
|
||
5 years ago
|
// Each cron task is run within the shutdown context as a running server
|
||
|
// AtShutdown the cron server is stopped
|
||
9 years ago
|
func NewContext() {
|
||
5 years ago
|
initBasicTasks()
|
||
|
initExtendedTasks()
|
||
5 years ago
|
|
||
5 years ago
|
lock.Lock()
|
||
|
for _, task := range tasks {
|
||
|
if task.IsEnabled() && task.DoRunAtStart() {
|
||
|
go task.Run()
|
||
|
}
|
||
5 years ago
|
}
|
||
|
|
||
9 years ago
|
c.Start()
|
||
5 years ago
|
started = true
|
||
|
lock.Unlock()
|
||
|
graceful.GetManager().RunAtShutdown(context.Background(), func() {
|
||
|
c.Stop()
|
||
|
lock.Lock()
|
||
|
started = false
|
||
|
lock.Unlock()
|
||
|
})
|
||
|
|
||
|
}
|
||
|
|
||
|
// TaskTableRow represents a task row in the tasks table
|
||
|
type TaskTableRow struct {
|
||
|
Name string
|
||
|
Spec string
|
||
|
Next time.Time
|
||
|
Prev time.Time
|
||
|
ExecTimes int64
|
||
11 years ago
|
}
|
||
|
|
||
5 years ago
|
// TaskTable represents a table of tasks
|
||
|
type TaskTable []*TaskTableRow
|
||
|
|
||
9 years ago
|
// ListTasks returns all running cron tasks.
|
||
5 years ago
|
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{
|
||
|
Name: task.Name,
|
||
|
Spec: spec,
|
||
|
Next: next,
|
||
|
Prev: prev,
|
||
|
ExecTimes: task.ExecTimes,
|
||
|
})
|
||
|
task.lock.Unlock()
|
||
|
}
|
||
|
|
||
|
return tTable
|
||
11 years ago
|
}
|