cron.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package cron
  5. import (
  6. "time"
  7. log "gopkg.in/clog.v1"
  8. "github.com/gogits/cron"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/modules/setting"
  11. )
  12. var c = cron.New()
  13. func NewContext() {
  14. var (
  15. entry *cron.Entry
  16. err error
  17. )
  18. if setting.Cron.UpdateMirror.Enabled {
  19. entry, err = c.AddFunc("Update mirrors", setting.Cron.UpdateMirror.Schedule, models.MirrorUpdate)
  20. if err != nil {
  21. log.Fatal(4, "Cron[Update mirrors]: %v", err)
  22. }
  23. if setting.Cron.UpdateMirror.RunAtStart {
  24. entry.Prev = time.Now()
  25. entry.ExecTimes++
  26. go models.MirrorUpdate()
  27. }
  28. }
  29. if setting.Cron.RepoHealthCheck.Enabled {
  30. entry, err = c.AddFunc("Repository health check", setting.Cron.RepoHealthCheck.Schedule, models.GitFsck)
  31. if err != nil {
  32. log.Fatal(4, "Cron[Repository health check]: %v", err)
  33. }
  34. if setting.Cron.RepoHealthCheck.RunAtStart {
  35. entry.Prev = time.Now()
  36. entry.ExecTimes++
  37. go models.GitFsck()
  38. }
  39. }
  40. if setting.Cron.CheckRepoStats.Enabled {
  41. entry, err = c.AddFunc("Check repository statistics", setting.Cron.CheckRepoStats.Schedule, models.CheckRepoStats)
  42. if err != nil {
  43. log.Fatal(4, "Cron[Check repository statistics]: %v", err)
  44. }
  45. if setting.Cron.CheckRepoStats.RunAtStart {
  46. entry.Prev = time.Now()
  47. entry.ExecTimes++
  48. go models.CheckRepoStats()
  49. }
  50. }
  51. c.Start()
  52. }
  53. // ListTasks returns all running cron tasks.
  54. func ListTasks() []*cron.Entry {
  55. return c.Entries()
  56. }