1
0
Fork 0

make cron task configurable

This commit is contained in:
Unknwon 2015-08-18 02:19:29 +08:00
parent d17f102339
commit b1696665bd
10 changed files with 123 additions and 80 deletions

View file

@ -281,21 +281,34 @@ DRIVER =
; Based on xorm, e.g.: root:root@localhost/gogs?charset=utf8 ; Based on xorm, e.g.: root:root@localhost/gogs?charset=utf8
CONN = CONN =
[cron]
; Enable running cron tasks periodically.
ENABLED = true
; Run cron tasks when Gogs starts.
RUN_AT_START = false
; Update mirrors
[cron.update_mirrors]
SCHEDULE = @every 1h
; Repository health check
[cron.repo_health_check]
SCHEDULE = @every 24h
; Arguments for command 'git fsck', e.g.: "--unreachable --tags"
; see more on http://git-scm.com/docs/git-fsck/1.7.5
ARGS =
; Check repository statistics
[cron.check_repo_stats]
RUN_AT_START = true
SCHEDULE = @every 24h
[git] [git]
MAX_GIT_DIFF_LINES = 10000 MAX_GIT_DIFF_LINES = 10000
; Arguments for command 'git gc', e.g.: "--aggressive --auto" ; Arguments for command 'git gc', e.g.: "--aggressive --auto"
; see more on http://git-scm.com/docs/git-gc/1.7.5 ; see more on http://git-scm.com/docs/git-gc/1.7.5
GC_ARGS = GC_ARGS =
; Git health check.
[git.fsck]
ENABLE = true
; Execution interval in hours. Default is 24.
INTERVAL = 24
; Arguments for command 'git fsck', e.g.: "--unreachable --tags"
; see more on http://git-scm.com/docs/git-fsck/1.7.5
ARGS =
[i18n] [i18n]
LANGS = en-US,zh-CN,zh-HK,de-DE,fr-FR,nl-NL,lv-LV,ru-RU,ja-JP,es-ES,pt-BR,pl-PL,bg-BG,it-IT LANGS = en-US,zh-CN,zh-HK,de-DE,fr-FR,nl-NL,lv-LV,ru-RU,ja-JP,es-ES,pt-BR,pl-PL,bg-BG,it-IT
NAMES = English,简体中文,繁體中文,Deutsch,Français,Nederlands,Latviešu,Русский,日本語,Español,Português do Brasil,Polski,български,Italiano NAMES = English,简体中文,繁體中文,Deutsch,Français,Nederlands,Latviešu,Русский,日本語,Español,Português do Brasil,Polski,български,Italiano

View file

@ -17,7 +17,7 @@ import (
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
) )
const APP_VER = "0.6.5.0817 Beta" const APP_VER = "0.6.5.0818 Beta"
func init() { func init() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())

40
models/cron/cron.go Normal file
View file

@ -0,0 +1,40 @@
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package cron
import (
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/cron"
"github.com/gogits/gogs/modules/setting"
)
var c = cron.New()
func NewCronContext() {
if setting.Cron.UpdateMirror.Enabled {
c.AddFunc("Update mirrors", setting.Cron.UpdateMirror.Schedule, models.MirrorUpdate)
if setting.Cron.UpdateMirror.RunAtStart {
go models.MirrorUpdate()
}
}
if setting.Cron.RepoHealthCheck.Enabled {
c.AddFunc("Repository health check", setting.Cron.RepoHealthCheck.Schedule, models.GitFsck)
if setting.Cron.RepoHealthCheck.RunAtStart {
go models.GitFsck()
}
}
if setting.Cron.CheckRepoStats.Enabled {
c.AddFunc("Check repository statistics", setting.Cron.CheckRepoStats.Schedule, models.CheckRepoStats)
if setting.Cron.CheckRepoStats.RunAtStart {
go models.CheckRepoStats()
}
}
c.Start()
}
// ListTasks returns all running cron tasks.
func ListTasks() []*cron.Entry {
return c.Entries()
}

View file

@ -1108,7 +1108,7 @@ func RewriteRepositoryUpdateHook() error {
} }
var ( var (
// Prevent duplicate tasks. // Prevent duplicate running tasks.
isMirrorUpdating = false isMirrorUpdating = false
isGitFscking = false isGitFscking = false
isCheckingRepos = false isCheckingRepos = false
@ -1164,7 +1164,7 @@ func GitFsck() {
isGitFscking = true isGitFscking = true
defer func() { isGitFscking = false }() defer func() { isGitFscking = false }()
args := append([]string{"fsck"}, setting.Git.Fsck.Args...) args := append([]string{"fsck"}, setting.Cron.RepoHealthCheck.Args...)
if err := x.Where("id > 0").Iterate(new(Repository), if err := x.Where("id > 0").Iterate(new(Repository),
func(idx int, bean interface{}) error { func(idx int, bean interface{}) error {
repo := bean.(*Repository) repo := bean.(*Repository)
@ -1216,7 +1216,7 @@ func CheckRepoStats() {
log.Error(4, "select repository check 'watch': %v", err) log.Error(4, "select repository check 'watch': %v", err)
} }
for _, repo_id := range results_watch { for _, repo_id := range results_watch {
log.Info("updating repository count 'watch'") log.Trace("updating repository count 'watch'")
repoID := com.StrTo(repo_id["id"]).MustInt64() repoID := com.StrTo(repo_id["id"]).MustInt64()
_, err := x.Exec("UPDATE `repository` SET num_watches=(SELECT count(*) FROM `watch` WHERE repo_id=?) WHERE id=?", repoID, repoID) _, err := x.Exec("UPDATE `repository` SET num_watches=(SELECT count(*) FROM `watch` WHERE repo_id=?) WHERE id=?", repoID, repoID)
if err != nil { if err != nil {
@ -1230,7 +1230,7 @@ func CheckRepoStats() {
log.Error(4, "select repository check 'star': %v", err) log.Error(4, "select repository check 'star': %v", err)
} }
for _, repo_id := range results_star { for _, repo_id := range results_star {
log.Info("updating repository count 'star'") log.Trace("updating repository count 'star'")
repoID := com.StrTo(repo_id["id"]).MustInt64() repoID := com.StrTo(repo_id["id"]).MustInt64()
_, err := x.Exec("UPDATE `repository` SET .num_stars=(SELECT count(*) FROM `star` WHERE repo_id=?) WHERE id=?", repoID, repoID) _, err := x.Exec("UPDATE `repository` SET .num_stars=(SELECT count(*) FROM `star` WHERE repo_id=?) WHERE id=?", repoID, repoID)
if err != nil { if err != nil {

File diff suppressed because one or more lines are too long

View file

@ -1,27 +0,0 @@
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package cron
import (
"fmt"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/setting"
)
var c = New()
func NewCronContext() {
c.AddFunc("Update mirrors", "@every 1h", models.MirrorUpdate)
if setting.Git.Fsck.Enable {
c.AddFunc("Repository health check", fmt.Sprintf("@every %dh", setting.Git.Fsck.Interval), models.GitFsck)
}
c.AddFunc("Check repository statistics", "@every 24h", models.CheckRepoStats)
c.Start()
}
func ListEntries() []*Entry {
return c.Entries()
}

View file

@ -126,11 +126,26 @@ var (
Git struct { Git struct {
MaxGitDiffLines int MaxGitDiffLines int
GcArgs []string `delim:" "` GcArgs []string `delim:" "`
Fsck struct { }
Enable bool
Interval int // Cron tasks.
Cron struct {
UpdateMirror struct {
Enabled bool
RunAtStart bool
Schedule string
} `ini:"cron.update_mirrors"`
RepoHealthCheck struct {
Enabled bool
RunAtStart bool
Schedule string
Args []string `delim:" "` Args []string `delim:" "`
} `ini:"git.fsck"` } `ini:"cron.repo_health_check"`
CheckRepoStats struct {
Enabled bool
RunAtStart bool
Schedule string
} `ini:"cron.check_repo_stats"`
} }
// I18n settings. // I18n settings.
@ -361,6 +376,8 @@ func NewConfigContext() {
if err = Cfg.Section("git").MapTo(&Git); err != nil { if err = Cfg.Section("git").MapTo(&Git); err != nil {
log.Fatal(4, "Fail to map Git settings: %v", err) log.Fatal(4, "Fail to map Git settings: %v", err)
} else if Cfg.Section("cron").MapTo(&Cron); err != nil {
log.Fatal(4, "Fail to map Cron settings: %v", err)
} }
Langs = Cfg.Section("i18n").Key("LANGS").Strings(",") Langs = Cfg.Section("i18n").Key("LANGS").Strings(",")

View file

@ -14,8 +14,8 @@ import (
"github.com/Unknwon/macaron" "github.com/Unknwon/macaron"
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models"
"github.com/gogits/gogs/models/cron"
"github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/cron"
"github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/middleware"
"github.com/gogits/gogs/modules/process" "github.com/gogits/gogs/modules/process"
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
@ -229,6 +229,6 @@ func Monitor(ctx *middleware.Context) {
ctx.Data["PageIsAdmin"] = true ctx.Data["PageIsAdmin"] = true
ctx.Data["PageIsAdminMonitor"] = true ctx.Data["PageIsAdminMonitor"] = true
ctx.Data["Processes"] = process.Processes ctx.Data["Processes"] = process.Processes
ctx.Data["Entries"] = cron.ListEntries() ctx.Data["Entries"] = cron.ListTasks()
ctx.HTML(200, MONITOR) ctx.HTML(200, MONITOR)
} }

View file

@ -18,9 +18,9 @@ import (
"gopkg.in/ini.v1" "gopkg.in/ini.v1"
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models"
"github.com/gogits/gogs/models/cron"
"github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/cron"
"github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/mailer" "github.com/gogits/gogs/modules/mailer"
"github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/middleware"

View file

@ -1 +1 @@
0.6.5.0817 Beta 0.6.5.0818 Beta