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.

64 lines
1.6 KiB

  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. "github.com/gogits/cron"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. var c = cron.New()
  13. // NewContext begins cron tasks
  14. func NewContext() {
  15. var (
  16. entry *cron.Entry
  17. err error
  18. )
  19. if setting.Cron.UpdateMirror.Enabled {
  20. entry, err = c.AddFunc("Update mirrors", setting.Cron.UpdateMirror.Schedule, models.MirrorUpdate)
  21. if err != nil {
  22. log.Fatal(4, "Cron[Update mirrors]: %v", err)
  23. }
  24. if setting.Cron.UpdateMirror.RunAtStart {
  25. entry.Prev = time.Now()
  26. entry.ExecTimes++
  27. go models.MirrorUpdate()
  28. }
  29. }
  30. if setting.Cron.RepoHealthCheck.Enabled {
  31. entry, err = c.AddFunc("Repository health check", setting.Cron.RepoHealthCheck.Schedule, models.GitFsck)
  32. if err != nil {
  33. log.Fatal(4, "Cron[Repository health check]: %v", err)
  34. }
  35. if setting.Cron.RepoHealthCheck.RunAtStart {
  36. entry.Prev = time.Now()
  37. entry.ExecTimes++
  38. go models.GitFsck()
  39. }
  40. }
  41. if setting.Cron.CheckRepoStats.Enabled {
  42. entry, err = c.AddFunc("Check repository statistics", setting.Cron.CheckRepoStats.Schedule, models.CheckRepoStats)
  43. if err != nil {
  44. log.Fatal(4, "Cron[Check repository statistics]: %v", err)
  45. }
  46. if setting.Cron.CheckRepoStats.RunAtStart {
  47. entry.Prev = time.Now()
  48. entry.ExecTimes++
  49. go models.CheckRepoStats()
  50. }
  51. }
  52. c.Start()
  53. }
  54. // ListTasks returns all running cron tasks.
  55. func ListTasks() []*cron.Entry {
  56. return c.Entries()
  57. }